0

Im calling a Rest API using C#. The URL is working from browser but when i call it from c# an exception is called

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

This is my code

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

        }

Im new to C# and Rest Api. Please help me, I read many answers but none of them are working. Please help me thank you.

Ferooz Khan
  • 93
  • 3
  • 11
  • @CodeCaster Unable to connect to the remote server. This is the catch message – Ferooz Khan Jul 06 '15 at 14:33
  • @CodeCaster I couldnt find much on that exception – Ferooz Khan Jul 06 '15 at 14:46
  • using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = httpClient.GetAsync(url).Result; string result = response.Content.ReadAsStringAsync().Result.ToString(); return result; } – Prashant Jul 06 '15 at 14:54
  • @CodeCaster {"An attempt was made to access a socket in a way forbidden by its access permissions 54.169.4.245:8090"} This is the inner exception. And how to check whether machine has proxy server – Ferooz Khan Jul 06 '15 at 14:57
  • this line is error .i sure that..."HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.150.1:8090/api/storeLayout/kinectToRacks/42");" – Jagadeesh Govindaraj Jul 06 '15 at 14:59
  • Is it a Windows 8 app? – CodeCaster Jul 06 '15 at 15:00
  • you need check the url and permission for client side also server side ...my knowledge the response is not sending the right proto type – Jagadeesh Govindaraj Jul 06 '15 at 15:00
  • @JagadeeshGovindaraj the exception occurs here WebResponse webResponse = request.GetResponse(); The URL is working in Browser – Ferooz Khan Jul 06 '15 at 15:01
  • @CodeCaster Which one are you asking. I'm running the code on Visual Studio 2013. It is a WPF application – Ferooz Khan Jul 06 '15 at 15:03
  • yes man i mentioned above comment the "my knowledge the response is not sending the right proto type" ... i faced the similar issue. – Jagadeesh Govindaraj Jul 06 '15 at 15:03
  • 1
    @Jagadeesh "the response is not sending the right prototype" does not make any sense. Please don't confuse OP. This is a permissions issue, either on the machine, the firewall or the proxy. – CodeCaster Jul 06 '15 at 15:04
  • @CodeCaster , First chance exceptions are not errors. These are usually exceptions. i know that but some firewall issue are opend this type issue.. – Jagadeesh Govindaraj Jul 06 '15 at 15:06
  • Do one thing allow the remote machine to open port ..this error will be gone. – Jagadeesh Govindaraj Jul 06 '15 at 15:07
  • @Jagadeesh what exactly does _"allow the remote machine to open port"_ mean and how does one do that? Especially noting OP says it works when he visits the URL using a browser? – CodeCaster Jul 06 '15 at 15:08
  • @CodeCaster ..that what iam saying my english is low..i apologies for that.. – Jagadeesh Govindaraj Jul 06 '15 at 15:08
  • @CodeCaster To mention, if i change the URL to local it is working. It couldnt connect to the remote server. – Ferooz Khan Jul 06 '15 at 15:11
  • Try this ..."[DebuggerNonUserCode]public static void getInputFromTeam1(){}"...http://stackoverflow.com/questions/948590/suppress-first-chance-exceptions – Jagadeesh Govindaraj Jul 06 '15 at 15:16
  • 1
    That again points to a proxy server, which I pointed out before. See [Change proxy server settings in Internet Explorer](http://windows.microsoft.com/en-us/windows/change-internet-explorer-proxy-server-settings#1TC=windows-7) to check your machine's proxy settings. If a proxy is configured for your machine, your application is likely to need the proxy too, in order to make outbound HTTP requests. See [how to use http post with proxy support in c#](http://stackoverflow.com/questions/1372519/how-to-use-http-post-with-proxy-support-in-c-sharp) for that. – CodeCaster Jul 06 '15 at 15:16
  • 2
    @Jagadeesh please stop confusing OP. That attribute is not relevant at all here. If you don't have a clue what the question is about, wait until you have more experience. – CodeCaster Jul 06 '15 at 15:19

2 Answers2

0

I would recommend you to have a look at the high-level WebClient-class instead. You must still pay close attention to the exception message.

        var serviceRequest = new WebClient();
        string response = serviceRequest.DownloadString(new Uri(url));
flindeberg
  • 4,887
  • 1
  • 24
  • 37
Justin
  • 21
  • 2
  • An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: Unable to connect to the remote server @Justin – Ferooz Khan Jul 06 '15 at 14:30
-1

You can use this code.

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