Re: "The request was aborted: The connection was closed unexpectedly"

J

John Saunders

Dave said:
Hi,

I'm trying to simulate a form post using the WebRequest class. But I
can't find this error (see note below) documented on MSDN.
string strId = "someid";
string strName = "somename";

ASCIIEncoding encoding=new ASCIIEncoding();

string postData="userid="+strId;
postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);

Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://secure.somecontentwepayfor.com");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;

Stream newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length); <---**ERROR OCCURS HERE**.
newStream.Close();
WebResponse resp = myRequest.GetResponse();

Throw a try block around that code and display the full exception in the
catch. In addition, if it's a WebException, you may be able to display the
Status property:

try
{
string strId = "someid";
string strName = "somename";

ASCIIEncoding encoding=new ASCIIEncoding();

string postData="userid="+strId;
postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);

Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("https://secure.somecontentwepayfor.com");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;

Stream newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length); <---**ERROR OCCURS HERE**.
newStream.Close();
WebResponse resp = myRequest.GetResponse();
}
catch (WebException wex)
{
Page.Trace.Write(wex.ToString());
Page.Trace.Write(wex.Status);
}
catch (Exception ex)
{
Page.Trace.Write(ex.ToString());
}
 
K

Kyril Magnos

What is the actual exception that is being thrown? That might help us figure
out what is going on.
--
HTH

Kyril Magnos

Question of the day:
What is Mono?
A) Disease where the lymph nodes become swollen.
B) A single sound
C) A synonym for one
D) A port of .NET meant to royally irritate MSFT
E) All of the above.

| Thanks. I did all that but it doesn't really give me any insight as to
why it's happening.
|
| "John Saunders" wrote:
|
| > | > > Hi,
| > >
| > > I'm trying to simulate a form post using the WebRequest class. But I
| > can't find this error (see note below) documented on MSDN.
| > >
| > > string strId = "someid";
| > > string strName = "somename";
| > >
| > > ASCIIEncoding encoding=new ASCIIEncoding();
| > >
| > > string postData="userid="+strId;
| > > postData += ("&username="+strName);
| > > byte[] data = encoding.GetBytes(postData);
| > >
| > > Prepare web request...
| > > HttpWebRequest myRequest =
| >
(HttpWebRequest)WebRequest.Create("https://secure.somecontentwepayfor.com");
| > > myRequest.Method = "POST";
| > > myRequest.ContentType="application/x-www-form-urlencoded";
| > > myRequest.ContentLength = data.Length;
| > >
| > > Stream newStream=myRequest.GetRequestStream();
| > > newStream.Write(data,0,data.Length); <---**ERROR OCCURS HERE**.
| > > newStream.Close();
| > > WebResponse resp = myRequest.GetResponse();
| >
| > Throw a try block around that code and display the full exception in the
| > catch. In addition, if it's a WebException, you may be able to display
the
| > Status property:
| >
| > try
| > {
| > string strId = "someid";
| > string strName = "somename";
| >
| > ASCIIEncoding encoding=new ASCIIEncoding();
| >
| > string postData="userid="+strId;
| > postData += ("&username="+strName);
| > byte[] data = encoding.GetBytes(postData);
| >
| > Prepare web request...
| > HttpWebRequest myRequest =
| >
(HttpWebRequest)WebRequest.Create("https://secure.somecontentwepayfor.com");
| > myRequest.Method = "POST";
| > myRequest.ContentType="application/x-www-form-urlencoded";
| > myRequest.ContentLength = data.Length;
| >
| > Stream newStream=myRequest.GetRequestStream();
| > newStream.Write(data,0,data.Length); <---**ERROR OCCURS HERE**.
| > newStream.Close();
| > WebResponse resp = myRequest.GetResponse();
| > }
| > catch (WebException wex)
| > {
| > Page.Trace.Write(wex.ToString());
| > Page.Trace.Write(wex.Status);
| > }
| > catch (Exception ex)
| > {
| > Page.Trace.Write(ex.ToString());
| > }
| > --
| > John Saunders
| > johnwsaundersiii at hotmail
| >
| >
| >
 
J

John Saunders

Please provide the stack trace...

--
John Saunders
johnwsaundersiii at hotmail


Dave said:
Thanks. I did all that but it doesn't really give me any insight as to why it's happening.

John Saunders said:
Dave said:
Hi,

I'm trying to simulate a form post using the WebRequest class. But I
can't find this error (see note below) documented on MSDN.
string strId = "someid";
string strName = "somename";

ASCIIEncoding encoding=new ASCIIEncoding();

string postData="userid="+strId;
postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);

Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("https://secure.somecontentwepayfor.com");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;

Stream newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length); <---**ERROR OCCURS HERE**.
newStream.Close();
WebResponse resp = myRequest.GetResponse();

Throw a try block around that code and display the full exception in the
catch. In addition, if it's a WebException, you may be able to display the
Status property:

try
{
string strId = "someid";
string strName = "somename";

ASCIIEncoding encoding=new ASCIIEncoding();

string postData="userid="+strId;
postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);

Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("https://secure.somecontentwepayfor.com");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;

Stream newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length); <---**ERROR OCCURS HERE**.
newStream.Close();
WebResponse resp = myRequest.GetResponse();
}
catch (WebException wex)
{
Page.Trace.Write(wex.ToString());
Page.Trace.Write(wex.Status);
}
catch (Exception ex)
{
Page.Trace.Write(ex.ToString());
}
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top