HttpWebRequest fails to connect to the web server on first attempt

  • Thread starter Thread starter safdarhhaider
  • Start date Start date
S

safdarhhaider

Hi,
I'm using HttpWebRequest to get some data from an ASP page off a
Windows 2000 Professional machine running IIS.
I start my application in debug mode (running on Visual Studio .NET
2003 + PocketPC 2002 Emulator) and execute the follwing code...

private void WeRequestTest()
{
string responseData = "";

try
{
// Create the WebRequest object and set the URL
HttpWebRequest webReq =
(HttpWebRequest)WebRequest.Create("http://mymachine/test.asp");
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.Method = "POST";

string postData = "TestParam=123" ;

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytePostData = encoding.GetBytes(postData);
webReq.ContentLength = bytePostData.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(bytePostData, 0, bytePostData.Length);
newStream.Close();

// Get the result sent by the Server
WebResponse webResp = webReq.GetResponse();
Stream respStream = webResp.GetResponseStream();
StreamReader streamReader = new StreamReader(respStream,
Encoding.ASCII);
responseData = streamReader.ReadToEnd();
streamReader.Close();
webResp.Close();
MessageBox.Show(responseData);
}
catch(Exception excp)
{
MessageBox.Show(excp.Message);
}

}

It just sits on the line...
Stream newStream = webReq.GetRequestStream();
and finally errors out saying "unable to connect to the remote server"

I stop the application, start it again and run the code again and it
works fine this time arround and also on subsequest attempts. Its just
the first time that it does'nt work.

Now when things are working fine, if I close the application, come back
say after an hour or so, try it again and guess what? yeah, it fails
again on first attempt, but again on the subsequest attempts it works
fine.

This behaviour is very consistent and i'm always able to reproduce it.

I wonder if somebody has had similar problem with HttpWebRequest and
was able to resolve it.

I have modified the URL to simplify it here in the sample code, rest of
the code is exactly the same. The real URL points to a test asp page on
my computer and i'm using my computer's name instead of "localhost" in
the url.

I'll greatly appreciate any thoughts that can help me resolve this
issue, Thanks.

SAFDAR

-- Some people are wise, others are otherwise ;)
 
Thanks for your advice Alex.
I intalled the .NET Compact Framework 1.0 SP2 Developer
Redistributable (Re-release)
(netcf.core.ppc3.x86.cab) on the PocketPC 2002 Emulator but it did'nt
resolve the problem.
While I was installing the SP2 on the emulator it prompted me saying
the framework is already installed.
I'm not sure if it means that i already had SP2 on my emulator or not.
I installed it anyway though.
 
Thanks for your advice Alex.
I intalled the .NET Compact Framework 1.0 SP2 Developer
Redistributable (Re-release)
(netcf.core.ppc3.x86.cab) on the PocketPC 2002 Emulator but it did'nt
resolve the problem.
While I was installing the SP2 on the emulator it prompted me saying
the framework is already installed.
I'm not sure if it means that i already had SP2 on my emulator or not.
I installed it anyway though.
 
The message is expected. The behavior is not. The only thing I can suggest
is testing it on a real device (or you can first try PPC 2003 emulator)
 
Installing PPC 2003 Emulator with CF 1.0 SP2 finally resolved this
issue.
Thank you very very much for your valuable suggestions.

Regards,
Safdar
 
Back
Top