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.

19
  • @CodeCaster Unable to connect to the remote server. This is the catch message Commented Jul 6, 2015 at 14:33
  • @CodeCaster I couldnt find much on that exception Commented Jul 6, 2015 at 14:46
  • 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. Commented Jul 6, 2015 at 15:04
  • 1
    That again points to a proxy server, which I pointed out before. See Change proxy server settings in Internet Explorer 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# for that. Commented Jul 6, 2015 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. Commented Jul 6, 2015 at 15:19

2 Answers 2

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));
Sign up to request clarification or add additional context in comments.

1 Comment

An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: Unable to connect to the remote server @Justin
-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();
                }
            }

2 Comments

Same exception is called
I do not think the answer is wrong.You can check cors settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.