The underlying connection was closed: An unexpected error occurred on a receive.

T

TCook

Hello,

I am trying to programmatically post (i.e. login, redirect &
programmatically submit data) to a website not a webservice. I am receiving
the error that is shown in the subject line of this posting. Based upon the
MSDN article:

http://msdn2.microsoft.com/en-us/library/debx8sh9.aspx

I am using the following code in an attempt to simply login which is
attached to a button on a webform:


private void btnPost_Click(object sender, System.EventArgs e)
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create ("url_string");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "UserNameTB=" + HttpUtility.UrlEncode("name_string") +
"PasswordTB=" + HttpUtility.UrlEncode("password_string");
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();
}


Any ideas why this isn't working?

Thanks & Regards,

TC
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top