简体   繁体   English

我有在天蓝色上烫过的网络API。 从邮递员那里得到请求,然后简单地得到请求。 但它不能从C#http get调用

[英]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.

相关问题 C# - 无法让这个看似简单的 Web 请求正常工作 - C# - Can't get this seemingly simple web request working 具有基本身份验证的 GET 请求来自 Postman,但不是来自 C# - GET request with basic auth works from Postman but not from C# 使用邮递员到 web api 2 c# 放置请求不起作用 - Put request is not working using postman to web api 2 c# HTTP Web请求GET C# - HTTP web request GET C# 简单的http Web发布请求无法通过c#代码正常运行,而来自REST客户端的相同内容可以正常运行 - Simple http web post request not working properly from c# code, whereas same content from REST client is working fine 如何从另一个 Controller 调用 HTTP POST 方法,它在 WEB 中有一个 HTTP GET 方法 API 2 - How to call HTTP POST method from another Controller which Have a HTTP GET method in WEB API 2 C#HttpTriggered Azure函数从请求获取内容作为接口 - C# HttpTriggered Azure funtion get content as interface from request C# - 如何从 http 请求中获取 HTTP 状态代码 - C# - How to I get the HTTP Status Code from a http request C# 从 HTTP POST 请求中获取答案 - C# Get answer from HTTP POST Request 从Web请求中获取c#中的JSON对象 - Get JSON object in c# from a web-request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM