json c# PostAsync部署IIS时发送请求时出错

yacmzcpb  于 2022-11-19  发布在  C#
关注(0)|答案(1)|浏览(208)

我正在使用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);
}
vptzau2j

vptzau2j1#

请尝试:

var response = client.PostAsync(client.BaseAddress,content).GetAwaiter().GetResult();

相关问题