0

I have class in c# which makes POST call to a REST API. BUt it gives me a 'connection time out' error with error code as 10060. In the POST call I am trying to make some transaction adjustments in the client's system. When I use Fiddler or Postman to make the api call, the request seems to go through but not from the c# class. Can you see where I am going wrong? Below is my sample code.

HttpWebRequest request = (HttpWebRequest)WebRequest.CreateHttp(clientURL);
byte[] data = Encoding.UTF8.GetBytes(urlParameters);

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;

        using (Stream stream = request.GetRequestStream()) **//exception occurs at this point**
        {
            stream.Write(data, 0, data.Length);
        }

Thanks.

Uma
  • 11
  • 1
  • 3

2 Answers2

0

You have to close your webrequest by using "using" or adding a call to the HttpWebRequest.Abort() method

Bilel Chaouadi
  • 903
  • 1
  • 10
  • 28
0

The default timeout for HttpWebRequest is 100 seconds. You can change that value by setting that property (in ms):

request.Timeout = 120000;
Niels V
  • 1,005
  • 8
  • 11
  • I did use the timeout parameter you mentioned above. But that did not solve the purpose. – Uma Sep 30 '15 at 21:24
  • So when you did try this... After how many seconds did the timeout occur? And can you connect to the server in the first place? You mentioned transactions... Do you have a timeout on the server instead? – Niels V Oct 01 '15 at 05:15
  • Searching SO yields possible duplicate question: http://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly – Niels V Oct 01 '15 at 05:23
  • I did look at the post you mentioned before and tried the method of closing the request stream, which is exactly what I have been doing in the code. I am using a time out limit of 100 milliseconds, but I am able to make a call to the API using chrome plugins like Postman or Fiddler. For some reason looks like something is blocking either from my code or from our servers that does not allow to call the API. – Uma Oct 01 '15 at 18:50
  • Well, 100 ms is relatively short. I hope you specified 100 seconds instead :) Did you try calling the Abort() method on the request? – Niels V Oct 01 '15 at 19:36
  • Sorry, you are right its 100 sec instead. When do I close the request method, after I read the stream or immediately after opening the request? – Uma Oct 01 '15 at 19:49
  • When you're done writing to the request stream, so outside its using statement I would say. – Niels V Oct 01 '15 at 19:57