Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# httpwebrequest getResponse() freezes and hangs my program

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

like image 227
Rick Avatar asked Jul 09 '10 14:07

Rick


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

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.

like image 87
Jon Skeet Avatar answered Oct 25 '22 08:10

Jon Skeet


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

like image 37
Darin Dimitrov Avatar answered Oct 25 '22 09:10

Darin Dimitrov