Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C调用restapi_C#_Rest_Httpwebrequest_Httpwebresponse - Fatal编程技术网

C# 使用C调用restapi

C# 使用C调用restapi,c#,rest,httpwebrequest,httpwebresponse,C#,Rest,Httpwebrequest,Httpwebresponse,我使用C调用RESTAPI。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 '

我使用C调用RESTAPI。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和RESTAPI的新手。请帮帮我,我读了很多答案,但没有一个有效。请帮助我,谢谢。

我建议您查看高级WebClient类。您仍然必须密切关注异常消息

        var serviceRequest = new WebClient();
        string response = serviceRequest.DownloadString(new Uri(url));
您可以使用此代码

            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();
                }
            }

System.dll中发生类型为“System.Net.WebException”的未处理异常。其他信息:无法连接到远程服务器@Justin@CodeCaster无法连接到远程服务器。这就是问题所在message@CodeCaster我在那上面找不到多少exception@Jagadeesh响应没有发送正确的原型没有任何意义。请不要混淆OP。这是一个权限问题,无论是在计算机、防火墙还是代理上。这再次指向代理服务器,我之前已经指出了这一点。请参阅以检查计算机的代理设置。如果为您的计算机配置了代理,您的应用程序可能也需要该代理,以便发出出站HTTP请求。请参阅。@Jagadeesh请停止混淆OP。该属性在这里根本不相关。如果你不知道这个问题是关于什么的,等你有更多的经验再说。同样的例外情况也会出现。我不认为答案是错误的。你可以检查cors设置。