HttpWebResponse.GetResponse() hangs my program.

M

Mahernoz

Hi Friends,

I have this code in a C# console application which calls a URL on my
website(Asp.net/C#) with Querystrings. (I have also tried without
querystrings).

The problem is my program gets hanged. Even no error message is
displayed.

HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(CaptureURLForScheduler +
strQueryString);
req.Method = "POST";

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(strQueryString);
req.ContentLength = byte1.Length;

//This line hangs my program
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
//This line hangs my program

Can you tell me what's going on and how can i fix this?

Regards,
Mahernoz
 
M

Mahernoz

Is the URL well-formed? Have you tried pasting the eventual URL Into your
browser's address bar and requesting the "page"?
You could try the simplified WebClient class with
string s= WebClient.DownloadString(url)

and see if you still have a problem.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

Mahernoz said:
Hi Friends,
I have this code in a C# console application which calls a URL on my
website(Asp.net/C#) with Querystrings. (I have also tried without
querystrings).
The problem is my program gets hanged. Even no error message is
displayed.
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(CaptureURLForScheduler +
strQueryString);
req.Method = "POST";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(strQueryString);
req.ContentLength = byte1.Length;
//This line hangs my program
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
//This line hangs my program
Can you tell me what's going on and how can i fix this?
Regards,
Mahernoz

Hi,

yes, the url is welformed.

i do get a response when i tried pasting the eventual URL Into my
browser's address bar. But the GetResponse method is taking
lots of time (it doesn't seem to complete, i guess an infinite loop,
deadlock ,etc).

Regards,
Mahernoz.
 
A

Anthony Jones

Mahernoz said:
Hi Friends,

I have this code in a C# console application which calls a URL on my
website(Asp.net/C#) with Querystrings. (I have also tried without
querystrings).

The problem is my program gets hanged. Even no error message is
displayed.

HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(CaptureURLForScheduler +
strQueryString);
req.Method = "POST";

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(strQueryString);
req.ContentLength = byte1.Length;

//This line hangs my program
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
//This line hangs my program

Can you tell me what's going on and how can i fix this?

I think you're a little confused about the difference between the GET method
and the POST method and what content is. The strQueryString isn't content.
It forms part of the URL (that is the ID of the resource you are
requesting). If you really wanted to send content using a POST you should
be retrieving the RequestStream from the WebRequest and saving data to the
stream.

Ditch the content length and change POST to GET.

Does it work now?
 
C

Chris Shepherd

Mahernoz said:
yes, the url is welformed.

i do get a response when i tried pasting the eventual URL Into my
browser's address bar. But the GetResponse method is taking
lots of time (it doesn't seem to complete, i guess an infinite loop,
deadlock ,etc).

When you load it in the browser, does it actually complete loading?
What is the page you're hitting on the server? A ASP/PHP/CGI script?

I've seen instances where a lot of data being pushed to the client from
a web-based script without any kind of buffering will exhibit this kind
of behaviour. It effectively keeps streaming the data to the client
until it's loaded, and if there's several hundred records it could take
the script a while to do that, depending on how complex they are,
language used, server hardware, etc...

In reality the problem clears itself up if you let it go because it's
not really "hung" in the sense of crashed, but it's just waiting for the
webserver to finish serving it the page before returning your value.

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top