HttpClient'Net返回404(Not found:无法找到控制器)
本文关键字:控制器 found Not Net 返回 HttpClient | 更新日期: 2023-09-27 17:53:00
我必须进行身份验证,然后在asp.net中请求Web Api。验证已正确完成。但是,当我将会话值传递给新请求以生成某个响应时,响应返回Controller not found错误。我试图实现它在Asp。. Net MVC应用程序。
下面是主索引的代码:public async Task<ActionResult> Index()
{
var result = "";
string json = " { '"username'": '"webservice'", '"password'": '"iIU2piH6wpBF92T'" }";
var request = (HttpWebRequest)WebRequest.Create("https://os.mcparcel.com/osapi/user/login.json");
request.Method = "POST";
request.UserAgent = "MCParcel Plugin";
request.ContentType = "application/json";
using (var s = request.GetRequestStream())
{
using (var stw = new StreamWriter(s))
{
stw.Write(json);
}
}
try
{
var response = (HttpWebResponse)request.GetResponse();
var data = new StreamReader(response.GetResponseStream());
result = data.ReadToEnd();//<-this gives the value for session id and name
string key = "WucHEwRuy7trUDE7u3agEqEWrUkajuCr";
string order_id = "MCParcel3";
int labels_number = 1;
var r = await Test(key, order_id, labels_number, result);
}
catch (WebException ex)
{
var errorData = new StreamReader(ex.Response.GetResponseStream());
var errorString = errorData.ReadToEnd();
}
return View();
}
下面是同一控制器内的其他函数,错误在:
<var响应/strong>
public static async Task<string> Test(string key, string order_id, int labels_number, dynamic results, int? shipment_id = 0)
{
var r = await GetShippingOptionRequest(key, order_id, labels_number, results);
Console.WriteLine(r);
return r;
}
public static async Task<string> GetShippingOptionRequest(string key, string order_id, int labels_number, dynamic results, int? shipment_id = 0)
{
string json = " { '"key'": '"" + key + "'", '"order_id'": '"" + order_id + "'", '"labels_no'": " + labels_number + ", '"shipment_id'": " + shipment_id + "";
var dynObj = JsonConvert.DeserializeObject<LoginResponse>(results);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Method", "POST");
client.DefaultRequestHeaders.Add("ContentType", "application/json");
client.DefaultRequestHeaders.Add("UserAgent", "MCParcel Plugin");
client.DefaultRequestHeaders.Add("sessid", dynObj.sessid);
client.DefaultRequestHeaders.Add("session_name", dynObj.session_name);
client.DefaultRequestHeaders.Add("key", key);
client.DefaultRequestHeaders.Add("order_id", order_id);
client.DefaultRequestHeaders.Add("labels_number", Convert.ToString(labels_number));
client.DefaultRequestHeaders.Add("shipment_id", Convert.ToString(shipment_id));
//the code below is the required response that is not returning values and returning 404 error
var response = await client.GetStringAsync("https://os.mcparcel.com/osapi/service_os_api/get_label_info.json");
return response;
}
}
响应应该返回类似以下内容:
{"source_labels":("https://os.mcparcel.com/sites/os.mcparcel.com/files/sourcepdflabels/labels_8c1e3033a8d23c632006639f39ef6964.pdf"),"跟踪":"https://os.mcparcel.com/track_and_trace/(J) JD0000900581338100135004","labels_file":"https://os.mcparcel.com/sites/os.mcparcel.com/files/pdf_labels/70994c37ad2a99d4047e0684c3e05c1f.pdf"}
任何帮助都将非常感激。提前感谢!!
你能击中其他控制器吗?如果是,那么你可以尝试创建另一个控制器,并开始用简单的json请求移动当前代码,并将其构建为复杂的json请求。也可能是你的路线。
如果没有,那么可能是您没有设置config.MapHttpAttributeRoutes()
。