[英]I have web api which is hoted on azure. get request working from postman and simple get request. But it not working from c# http get call
I have a web api https://itcportalapi.azurewebsites.net/portal/api/User/GetSecurityQuestions 我有一个Web API https://itcportalapi.azurewebsites.net/portal/api/User/GetSecurityQuestions
which is hosted on azure web app. 托管在azure Web应用程序上。 now it working in simple get call through the browser and postman.
现在,它可以通过浏览器和邮递员以简单的get呼叫方式工作。 but it not working thorough the http async get call.
但无法通过http异步get调用正常工作。
Remort server debug but same problem. Remort服务器调试,但同样的问题。 previously in some other azure account same code is working.
以前在其他一些Azure帐户中,相同的代码可以正常工作。 same web API code running in local is able to consume by the c# HTTP get the asynce code.
在本地运行的相同Web API代码能够由c#HTTP使用以获取异步代码。
public static async Task<string> ApiRequest(string url)
{
HttpResponseMessage responseMessage = null;
string responseObj = string.Empty;
using (HttpClient client = new HttpClient())
{
try
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Constants.JSONContentType));
responseMessage = await client.GetAsync(url);
if (responseMessage.IsSuccessStatusCode)
{
responseObj = responseMessage.Content.ReadAsStringAsync().Result;
}
else
{
log.Error("ApiRequest -the current request URL " + url + " and Status Code:" + responseMessage.StatusCode + responseMessage.ReasonPhrase);
}
}
catch (Exception ex)
{
log.Error("ApiRequest:" + ex.Message + ex.StackTrace);
}
return responseObj;
}
}
public async Task<ActionResult> ForgotPassword()
{
UserSecurityQuestion model = new Models.UserSecurityQuestion();
model.SQuestions = await this.GetSecurityQuestions();
return View(model);
}
below exception is coming which is hosted on the azure web app. 下面的异常即将到来,该异常托管在azure Web应用程序上。 now it working in simple get call through the browser and postman.
现在,它可以通过浏览器和邮递员以简单的get呼叫方式工作。 but it not working through the HTTP async get call.
但它无法通过HTTP异步get调用工作。
Here's a working example, although I wouldn't use HttpClient, use IHttpClientFactory so read up on that. 这是一个工作示例,尽管我不会使用HttpClient,但请使用IHttpClientFactory,请继续阅读。
var client = new HttpClient();
client.BaseAddress = new Uri("https://itcportalapi.azurewebsites.net/");
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var request = new HttpRequestMessage(HttpMethod.Get, "portal/api/User/GetSecurityQuestions");
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (var response = await client.SendAsync(request))
{
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
}
else
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.