By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,266 Members | 1,862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,266 IT Pros & Developers. It's quick & easy.

HttpWebRequest.Abort() does not stop network traffic

P: n/a
Hi,

I have a background worker in my C# app that makes a synchronous
HttpWebRequest.GetResponse() call. The idea is to POST a file to a
server on the internet. When I call HttpWebRequest.Abort() on the
request object on another thread, GetResponse() returns with an
exception as expected. However, when I monitor the network traffic, it
does not seem to stop, but to continue to be active and to upload the
file. The network is active even after I close down my application
completely and the file will be posted to the server even if that takes
another few minutes.

My expectation was that with calling Abort(), GetResponse() would return
with an exception and all network traffic that relates to this upload
would stop, but it seems that I am wrong here. Has anyone an idea why
this is the case?

Thanks and best wishes,

Marc.
Aug 1 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Marc Bartsch" <m.*******@reading.ac.ukwrote in message
news:4i*****************@newsfe5-win.ntli.net...
Hi,

I have a background worker in my C# app that makes a synchronous
HttpWebRequest.GetResponse() call. The idea is to POST a file to a
server on the internet. When I call HttpWebRequest.Abort() on the request
object on another thread, GetResponse() returns with an exception as
expected. However, when I monitor the network traffic, it does not seem to
stop, but to continue to be active and to upload the file. The network is
active even after I close down my application completely and the file will
be posted to the server even if that takes another few minutes.

My expectation was that with calling Abort(), GetResponse() would return
with an exception and all network traffic that relates to this upload
would stop, but it seems that I am wrong here. Has anyone an idea why this
is the case?
If I understand you correctly, you've already POSTed a file to the server,
and you've issued the GetResponse, yet when you abort it, the server keeps
sending you data?

Why should it stop? Answer: it should not stop, as nobody has told it to
stop. No message traveled from the client to the server saying: stop.

In fact, I don't believe that there _is_ such a message that can be sent. At
best, the TCP connection could be closed, but the server may not detect that
until it does a read from the connection.

Please confirm my understanding of your issue, and provide more detail of
the configuration. In particular, what software is running on client and
server, what versions, etc.
--
John Saunders [MVP]

Aug 1 '07 #2

P: n/a
Hi,

I just wanted to add that I tried everything in an asynchronous manner
and I got the same problem. The request will be aborted (in fact
exceptions are not thrown - I think I was wrong about that in my earlier
post), but the network is still busy uploading the file.

Marc.

Marc Bartsch schrieb:
Hi,

I have a background worker in my C# app that makes a synchronous
HttpWebRequest.GetResponse() call. The idea is to POST a file to a
server on the internet. When I call HttpWebRequest.Abort() on the
request object on another thread, GetResponse() returns with an
exception as expected. However, when I monitor the network traffic, it
does not seem to stop, but to continue to be active and to upload the
file. The network is active even after I close down my application
completely and the file will be posted to the server even if that takes
another few minutes.

My expectation was that with calling Abort(), GetResponse() would return
with an exception and all network traffic that relates to this upload
would stop, but it seems that I am wrong here. Has anyone an idea why
this is the case?

Thanks and best wishes,

Marc.
Aug 1 '07 #3

P: n/a
Hi John,

Thanks a lot for your post. My configuration is: .NET 2.0.50727, Visual
Studio 2005 and I am using C#. I don't know anything about the server
other than I can POST files to a URI. Also, the way I am using
HttpWebRequest is taken from the MSDN pages. The actual Abort() in my
application works ok, but the upload does not stop.

I am not quite sure what you mean by:
If I understand you correctly, you've already POSTed a file to the
server, and you've issued the GetResponse, yet when you abort it, ...
My understanding so far was that calling GetResponse starts the actual
POSTing of the file, because this is when I see the network becoming
quite busy, but I may be wrong here. From your sentence above I gather
that POSTing to the server occurs before calling GetResponse, maybe when
I write to the request stream? And GetResponse only waits for the
response from the server. Is that correct?

I think your understanding of my problem is correct, but it is not the
server that keeps sending data, it seems that my machine keeps sending
data to the server. When I start my app and I abort the upload a few
seconds later, a 1.6 MB file will still be uploaded.

Here is a short code snippet. This is an async version. The scenario is:
My Background worker thread calls Upload and then the GUI thread calls
Abort. BeginGetResponse will be aborted correctly, but the file keeps on
being uploaded.

public void Abort()
{
this.req.Abort();
}

public void Upload(Uri uri)
{
try
{
this.req = (WebRequest.Create(uri) as HttpWebRequest);
this.req.Method = "POST";

this.postData = new MemoryStream();

/* Create data to POST */

this.req.ContentLength = this.postData.Length;
this.req.BeginGetRequestStream(
new AsyncCallback(ReadCallback), this.req);

allDone.WaitOne();
allDone.Reset();

IAsyncResult rest = this.req.BeginGetResponse(new
AsyncCallback(RespCallback), this.req);

allDone.WaitOne();
}
catch (Exception exc)
{
Console.WriteLine("WebRequest aborted");
Console.WriteLine(exc.Message);
}
}

// First Callback
private void ReadCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

using (Stream s = request.EndGetRequestStream(asynchronousResult))
this.postData.WriteTo(s);
this.postData.Close();

allDone.Set();
}

// Second Callback
private void RespCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request =
(HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response =
(HttpWebResponse)request.EndGetResponse(asynchrono usResult);

StreamReader sr = new StreamReader(response.GetResponseStream());
string strResp = sr.ReadToEnd().Trim();

allDone.Set();
}
catch (WebException e)
{
Debug.WriteLine("\nRespCallback Exception raised!");
Debug.WriteLine("\nMessage:{0}", e.Message);
}
}

Thanks again for your help.

Best wishes,

Marc.

John Saunders [MVP] schrieb:
"Marc Bartsch" <m.*******@reading.ac.ukwrote in message
news:4i*****************@newsfe5-win.ntli.net...
>Hi,

I have a background worker in my C# app that makes a synchronous
HttpWebRequest.GetResponse() call. The idea is to POST a file to a
server on the internet. When I call HttpWebRequest.Abort() on the
request object on another thread, GetResponse() returns with an
exception as expected. However, when I monitor the network traffic, it
does not seem to stop, but to continue to be active and to upload the
file. The network is active even after I close down my application
completely and the file will be posted to the server even if that
takes another few minutes.

My expectation was that with calling Abort(), GetResponse() would
return with an exception and all network traffic that relates to this
upload would stop, but it seems that I am wrong here. Has anyone an
idea why this is the case?

If I understand you correctly, you've already POSTed a file to the
server, and you've issued the GetResponse, yet when you abort it, the
server keeps sending you data?

Why should it stop? Answer: it should not stop, as nobody has told it to
stop. No message traveled from the client to the server saying: stop.

In fact, I don't believe that there _is_ such a message that can be
sent. At best, the TCP connection could be closed, but the server may
not detect that until it does a read from the connection.

Please confirm my understanding of your issue, and provide more detail
of the configuration. In particular, what software is running on client
and server, what versions, etc.
Aug 1 '07 #4

P: n/a
"Marc Bartsch" <m.*******@reading.ac.ukwrote in message
news:u_******************@newsfe6-gui.ntli.net...
Hi John,

Thanks a lot for your post. My configuration is: .NET 2.0.50727, Visual
Studio 2005 and I am using C#. I don't know anything about the server
other than I can POST files to a URI. Also, the way I am using
HttpWebRequest is taken from the MSDN pages. The actual Abort() in my
application works ok, but the upload does not stop.

I am not quite sure what you mean by:
If I understand you correctly, you've already POSTed a file to the
server, and you've issued the GetResponse, yet when you abort it, ...

My understanding so far was that calling GetResponse starts the actual
POSTing of the file, because this is when I see the network becoming quite
busy, but I may be wrong here. From your sentence above I gather that
POSTing to the server occurs before calling GetResponse, maybe when I
write to the request stream? And GetResponse only waits for the response
from the server. Is that correct?

I think your understanding of my problem is correct, but it is not the
server that keeps sending data, it seems that my machine keeps sending
data to the server. When I start my app and I abort the upload a few
seconds later, a 1.6 MB file will still be uploaded.

Here is a short code snippet. This is an async version. The scenario is:
My Background worker thread calls Upload and then the GUI thread calls
Abort. BeginGetResponse will be aborted correctly, but the file keeps on
being uploaded.
Thanks for posting the code. I think I understand your problem now.

You are mixing asynchronous and synchronous operations. Although you use the
async methods on the HttpWebRequest/Response, you are writing your data
synchronously.

By the time you abort the response, you've already passed your data to .NET!

First, try aborting the Request, and not the response. That probably won't
work. I would then suggest going completely asynchronous, using a series of
async BeginWrite calls to write the file. Set a flag in your class instance
to indicate when it's time to abort. Have your completion routine for the
BeginWrite check the flag before starting another write.
--
John Saunders [MVP]

Aug 1 '07 #5

P: n/a
Hi John,

Well, the penny has finally dropped. I think your solution seems the
only way to go. I did not realise that I should write to the request
stream in chunks and check for an abort in between.

Thanks for helping to solve this problem and also for clearing up my
misunderstanding of HTTP POST and HttpWebRequest.

Best wishes,

Marc.

John Saunders [MVP] schrieb:
>
Thanks for posting the code. I think I understand your problem now.

You are mixing asynchronous and synchronous operations. Although you use
the async methods on the HttpWebRequest/Response, you are writing your
data synchronously.

By the time you abort the response, you've already passed your data to
.NET!

First, try aborting the Request, and not the response. That probably
won't work. I would then suggest going completely asynchronous, using a
series of async BeginWrite calls to write the file. Set a flag in your
class instance to indicate when it's time to abort. Have your completion
routine for the BeginWrite check the flag before starting another write.
Aug 2 '07 #6

P: n/a
"Marc Bartsch" <m.*******@reading.ac.ukwrote in message
news:ZH******************@newsfe5-win.ntli.net...
Hi John,

Well, the penny has finally dropped. I think your solution seems the only
way to go. I did not realise that I should write to the request stream in
chunks and check for an abort in between.
Glad to help.

BTW, is this abort scenario something that will happen often? If not, you
might consider leaving the code as-is.
--
John Saunders [MVP]

Aug 2 '07 #7

P: n/a
John Saunders [MVP] schrieb:
"Marc Bartsch" <m.*******@reading.ac.ukwrote in message
news:ZH******************@newsfe5-win.ntli.net...
>Hi John,

Well, the penny has finally dropped. I think your solution seems the
only way to go. I did not realise that I should write to the request
stream in chunks and check for an abort in between.

Glad to help.

BTW, is this abort scenario something that will happen often? If not,
you might consider leaving the code as-is.
Hi John,

the abort scenario will only occur if the user decides to stop a file
upload maybe when the file is too large or so. This will not occur too
often I guess.

Thanks again,

Marc.
Aug 2 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.