【问题标题】:An error occurred while sending the request, post发送请求时出错,post
【发布时间】:2020-11-14 07:07:49
【问题描述】:

我想从 Web API 获取令牌宽度窗口控制台,并且有我的代码,但我发生了错误:

发送请求时出错,

我使用 Visual Studio 2013,.Net FrameWork 4.5,

private static async Task<string> GetAccessToken()
    {
        string baseUrl = "http://my.strategy.maskan";
        string _ClientId = "abc";
        string _ClientSecret = "password123";
        string _Scope = "api1";

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(baseUrl);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();
            postData.Add(new KeyValuePair<string, string>("ClientId", _ClientId));
            postData.Add(new KeyValuePair<string, string>("ClientSecret", _ClientSecret));
            postData.Add(new KeyValuePair<string, string>("Scope", _Scope));


            FormUrlEncodedContent content = new FormUrlEncodedContent(postData);
            HttpResponseMessage response = new HttpResponseMessage();
            try
            {
                response = await client.PostAsync("IDENTITYSERVER", content);
            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine(ex.Message);
            }
            string jsonString = await response.Content.ReadAsStringAsync();
            return jsonString;
            
        }
    }

response = await client.PostAsync("IDENTITYSERVER", content); 中产生异常。 如何从 httpClient.PostAsync 修复此异常。

【问题讨论】:

  • 您需要提供更多信息。 HttpRequestException ex 中的内部异常与 ex.StatusCode 一样有帮助
  • 你需要看InnerException
  • 发送请求时发生错误之后还有其他内容:那是什么?连接被远程端点关闭?发布完整的异常。 -- 这段代码在哪里运行,什么系统?
  • 我解决了这个问题,我在我组织的错误 LAN 网络中。所以我在正确的域服务器中执行它。

标签: c# console client token webapi


【解决方案1】:

发生此错误的原因是,我在组织中的错误域服务器中发布了请求。 我 ping 我的地址服务器,然后发现没有任何响应给我,所以我将它执行到另一个可以访问此域名服务器的客户端,然后我得到了来自服务器的响应。

ping my.strategy.maskan

来自错误主机的响应:Ping request could not find host -----. please check the name and try again

来自正确主机的响应:reply from -.-.-.- bytes=32 time&lt;1ms TTL=62

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 2015-01-25
    • 2022-08-03
    • 1970-01-01
    相关资源
    最近更新 更多