我正在使用HttpClient PostAsync方法调用具有requestUri和内容的API,它在我的控制台开发中工作正常,但当部署到iis并运行页面时,InnerExceptions中出现错误“HttpRequestException发送请求时发生错误”。
因此,我尝试在服务器上用postman测试调用api,但它成功了,然后我的代码中缺少了什么,下面是mycode
[HttpPost]
public JsonResult TrackAPIRequest(MdlForm form)
{
Uri baseAddress = new Uri("myurlAPI");
HttpClient client = new HttpClient();
client.BaseAddress = baseAddress;
MdlForm RateMdl = RequestParam(form);
string BodyParam = JsonConvert.SerializeObject(RateMdl.trackRequest);
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(Username + ":" + Password);
string val = System.Convert.ToBase64String(plainTextBytes);
client.DefaultRequestHeaders.Add("Authorization", "Basic " + val);
StringContent content = new StringContent(BodyParam, Encoding.UTF8, "application/json");
var response = client.PostAsync(client.BaseAddress, content).Result;
var dataResponse = response.Content.ReadAsStringAsync();
return Json(dataResponse, JsonRequestBehavior.AllowGet);
}
1条答案
按热度按时间vptzau2j1#
请尝试: