WebRequest from a Windows Forms app

R

Rachet?

I am getting "Error:The underlying connection was closed:Unable to
connect to the remote server." when I try to excute the following from
a Windows Forms app. I have no problem from a browser app. By the way
the site I actually use is different from the one shown here. The
error happens at GetRequestStream() call. Is it not allowed to
interact with a classic asp page from .NET? Or what did I miss here?
Any help is appreciated.


WebRequest httpReq =
(HttpWebRequest)WebRequest.Create("http://justtest.moc/test.asp");
httpReq.Method = "POST";
httpReq.ContentType = "application/x-www-form-urlencoded";
httpReq.Timeout = 100000;

byte[] encodedRequest = Encoding.UTF8.GetBytes(strPost);
httpReq.ContentLength = encodedRequest.Length;

using (Stream requestStream = httpReq.GetRequestStream())
{
requestStream.Write(encodedRequest, 0, encodedRequest.Length);
requestStream.Close();
}
 
J

Joerg Jooss

Rachet? said:
I am getting "Error:The underlying connection was closed:Unable to
connect to the remote server." when I try to excute the following from
a Windows Forms app. I have no problem from a browser app.

You don't specify a proxy in your code -- do you use one in your browser?

[...]
WebRequest httpReq =
(HttpWebRequest)WebRequest.Create("http://justtest.moc/test.asp");
httpReq.Method = "POST";
httpReq.ContentType = "application/x-www-form-urlencoded";
httpReq.Timeout = 100000;

byte[] encodedRequest = Encoding.UTF8.GetBytes(strPost);
httpReq.ContentLength = encodedRequest.Length;

using (Stream requestStream = httpReq.GetRequestStream())
{
requestStream.Write(encodedRequest, 0, encodedRequest.Length);
requestStream.Close();

Note that this particular statement is redundant, since the using block
takes care of properly disposing the stream instance.

Cheers,
 

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