【发布时间】: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