9

I was trying to use httpwebrequest to use a rest like service on a remote server and from the first execution itself, my code was hanging the program. Then I tried it as a console application to make sure it has nothing to do with the program itself but no luck!

        string credentialsJson = @"{""username"":""test"",
                                      ""password"":""test"" 
                                   }";

        int tmp = ServicePointManager.DefaultConnectionLimit;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://qrua.com/qr/service" + @"/auth/login");
        request.Method = "POST";
        request.KeepAlive = true;
        request.Timeout = 50000 ;
        request.CookieContainer = new CookieContainer();
        request.ContentType = "application/json";
        try
        {
            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(credentialsJson);
        }
        catch (Exception e)
        {
            Console.WriteLine("EXCEPTION:" + e.Message);
        }

        //WebResponse response = request.GetResponse();
        try
        {
            using (WebResponse response = (HttpWebResponse)request.GetResponse())
            {
                Console.WriteLine("request:\n" + request.ToString() + "\nresponse:\n" + response.ContentLength);
                response.Close();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("EXCEPTION: in sending http request:" + " \nError message:" + e.Message);
        }

Tried several things from different forums but it doesnt help. Even a simple console app with the above code hangs the console indefinitely! Any help would be great..

Thanks

Rick
  • 101
  • 1
  • 1
  • 3

3 Answers3

14

You're never closing the StreamWriter... so I suspect it's not being flushed. Admittedly I'd expect an error from the server instead of just a hang, but it's worth looking at.

Btw, you don't need to close the response and dispose it. Just the using statement is enough.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Why did you mark this as correct, and then in another place you say, "tried closing the writer and it didnt do much" ? – Stephen Oberauer Jul 18 '11 at 12:41
  • @Lost Hobbit: I suggest you add a comment to the *question* instead of to my answer - the OP won't get notification of this comment. (Not that the OP has been on Stack Overflow for 11 months anyway...) – Jon Skeet Jul 18 '11 at 13:04
  • 1
    Was using HtmlAgilityPack and following permanent redirects in that library didnt close the underlying response before creating a new. – Kenny Eliasson Nov 21 '11 at 21:57
  • This exactly worked for me on the exact problem I was just having. Thanks @JonSkeet. – jmrnet Dec 12 '13 at 17:03
  • Helped me a lot. Thanks. – karliwson Apr 19 '17 at 14:34
5

There's not much you can do if the remote server is not responding other than defining a Timeout and catch the exception as you did in order to inform the user that the operation cannot complete because the remote site didn't respond:

var request = (HttpWebRequest)WebRequest.Create("https://qrua.com/qr/service/auth/login");
request.Timeout = 5000;
// If the server doesn't respond within 5 seconds you might catch the timeout exception
using (var response = request.GetResponse())
{

}

If you don't want to freeze the UI you could use the async version: BeginGetResponse

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

Try specifying request.ContentLength.

Before doing:

        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write(credentialsJson);

Try something like this:

using (MemoryStream stream = new MemoryStream())
{
    using (var writer = StreamWriter writer = new StreamWriter(stream))
    {
        writer.Write(credentialsJson);
        writer.Close();
    }
    request.ContentLength = stream.Length;
}
Stephen Oberauer
  • 5,237
  • 6
  • 53
  • 75