【问题标题】:Calling REST Api using C#使用 C# 调用 REST Api
【发布时间】:2015-07-06 14:09:45
【问题描述】:

我正在使用 C# 调用 Rest API。该 URL 正在浏览器中工作,但是当我从 c# 调用它时,会调用一个异常

A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.WebException' occurred in System.dll
A first chance exception of type 'System.Net.WebException' occurred in System.dll
A first chance exception of type 'System.Net.WebException' occurred in System.dll

这是我的代码

public static void getInputFromTeam1()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.150.1:8090/api/storeLayout/kinectToRacks/42");
            request.Method = "GET";
            request.Accept = "application/json";


            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {
                            string response = responseReader.ReadToEnd();
                            Console.Out.WriteLine(response);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("-----------------");
                Console.Out.WriteLine(e.Message);
            }

        }

我是 C# 和 Rest Api 的新手。请帮助我,我阅读了很多答案,但没有一个有效。请帮帮我,谢谢。

【问题讨论】:

  • @CodeCaster 无法连接到远程服务器。这是捕获消息
  • @CodeCaster 我找不到太多关于该异常的信息
  • @Jagadeesh“响应没有发送正确的原型”没有任何意义。请不要混淆OP。这是机器、防火墙或代理上的权限问题。
  • 这又指向一个代理服务器,我之前指出过。请参阅Change proxy server settings in Internet Explorer 以检查您机器的代理设置。如果为您的机器配置了代理,您的应用程序可能也需要代理,以便发出出站 HTTP 请求。请参阅how to use http post with proxy support in c#
  • @Jagadeesh 请停止混淆 OP。该属性在这里根本不相关。如果您不知道问题是什么,请等到您有更多经验。

标签: c# rest httpwebrequest httpwebresponse


【解决方案1】:

我建议您改为查看高级WebClient-class。您仍然必须密切注意异常消息。

        var serviceRequest = new WebClient();
        string response = serviceRequest.DownloadString(new Uri(url));

【讨论】:

  • System.dll 中出现“System.Net.WebException”类型的未处理异常附加信息:无法连接到远程服务器@Justin
【解决方案2】:

您可以使用此代码。

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

                HttpResponseMessage response = client.GetAsync("api/storeLayout/kinectToRacks/42").Result;
                if (response.IsSuccessStatusCode)
                {
                    var str = response.Content.ReadAsStringAsync();
                }
            }

【讨论】:

  • 调用相同的异常
  • 我认为答案没有错。你可以检查cors的设置。
猜你喜欢
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多