473,734 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HttpWebRequest. Abort() does not stop network traffic

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
7 7688
"Marc Bartsch" <m.*******@read ing.ac.ukwrote in message
news:4i******** *********@newsf e5-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
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
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. BeginGetRespons e 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.Cre ate(uri) as HttpWebRequest) ;
this.req.Method = "POST";

this.postData = new MemoryStream();

/* Create data to POST */

this.req.Conten tLength = this.postData.L ength;
this.req.BeginG etRequestStream (
new AsyncCallback(R eadCallback), this.req);

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

IAsyncResult rest = this.req.BeginG etResponse(new
AsyncCallback(R espCallback), this.req);

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

// First Callback
private void ReadCallback(IA syncResult asynchronousRes ult)
{
HttpWebRequest request = (HttpWebRequest )asynchronousRe sult.AsyncState ;

using (Stream s = request.EndGetR equestStream(as ynchronousResul t))
this.postData.W riteTo(s);
this.postData.C lose();

allDone.Set();
}

// Second Callback
private void RespCallback(IA syncResult asynchronousRes ult)
{
try
{
HttpWebRequest request =
(HttpWebRequest )asynchronousRe sult.AsyncState ;
HttpWebResponse response =
(HttpWebRespons e)request.EndGe tResponse(async hronousResult);

StreamReader sr = new StreamReader(re sponse.GetRespo nseStream());
string strResp = sr.ReadToEnd(). Trim();

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

Thanks again for your help.

Best wishes,

Marc.

John Saunders [MVP] schrieb:
"Marc Bartsch" <m.*******@read ing.ac.ukwrote in message
news:4i******** *********@newsf e5-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
"Marc Bartsch" <m.*******@read ing.ac.ukwrote in message
news:u_******** **********@news fe6-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. BeginGetRespons e 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
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
misunderstandin g 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
"Marc Bartsch" <m.*******@read ing.ac.ukwrote in message
news:ZH******** **********@news fe5-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
John Saunders [MVP] schrieb:
"Marc Bartsch" <m.*******@read ing.ac.ukwrote in message
news:ZH******** **********@news fe5-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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7390
by: Stephan Steiner | last post by:
Hi I'm having some weird threading issues.. almost at random, if I dare change a line of my code, the shutdown sequence gets messed up. I'm using a thread to receive data from the network, that I suspend and resume whenver needed. In order to properly shut down the program every thread has to be aborted. So, I override OnClosing(CancelEventArgs e) in my main GUI program, and have it perform the following on the thread: if...
7
3292
by: Morris | last post by:
I want to abort a running thread, so I call MyThread.abort() function. My problem is this thread runs "almost" like a while(true) loop and I don't want the Abort() function interrupts the thread at any point in the thread. In fact, I have a section of code needs to be "protected" from being interrupted. How can I make sure Abort() will not land anywhere winthin this block? In other words, the Abort() must wait until this block of code is done...
5
3139
by: [Yosi] | last post by:
Why I can't abot a susspended thread. Who can terminat a thread imediatly without consider to its stat or execution?
2
4820
by: Keith Patrick | last post by:
I'm trying to programmatically post data to another page within my ASP.Net app. Not POSTing is not an option (I can't store this data in my session, context, hidden fields, or anything else...I've exhausted all my other options, so I have to get this scenario working). Basically, if I POST the data normally, it works fine, except the target page has a new session ID, so I lost the session data that I *do* transfer around. However, if I...
1
2606
by: sfoxover | last post by:
Hi, Could someone please give me some suggestions on how to make this class robust. I need to be able to handle around 20 similtanious requests to this class which causes a web browser to display a waiting message while it requests XML data from a third party server. Some requests can take around 30 seconds. Usually the first time I do the request it always works. If I try again without closing the web browser it will fail sometimes....
0
1447
by: a_newcomb | last post by:
I have an application which polls and connects to a webserver on a background thread. When the application is supposed to exit, I call abort on the HttpWebRequest object. I have observed that when the PDT has no network connections ( i.e. configured for dhcp on the wireless interface, and the wireless settings aren't configured ), and the HttpWebRequest is blocking on GetRequestStream, abort doesn't throw an exception and the...
13
2431
by: Shailesh Humbad | last post by:
Here is an advanced PHP question. Can anyone think of a way to detect the number of bytes written to output when a script is aborted? I am sending a large file to the client, and I want to record how many bytes are actually sent. I can detect abort of the script using a shutdown handler. In the shutdown handler, I tried ob_get_length, but it returns false. I tried to read the server's log file, but it is does not contain the...
6
5479
by: Joe HM | last post by:
Hello - I have a function that calls Thread.Abort() to stop a thread in a _Closed() Method of a GUI. The thread contains a blocking call on a TCP socket and that is the easiest way to stop that. This thread is also outputting strings in a RichTextBox and in some rare instances I get a System.NullReferenceException when I exit the GUI. It seems like the _Closed() Method calls Thread.Abort() and then continues closing down/disposing...
7
7783
by: raids51 | last post by:
Hello, i have a program that downloads a file using the httpwebrequest/response, and it usually works, but sometimes it will freeze at a random part of the download without an error. here is the code: 'Creating the request and getting the response Dim theResponse As HttpWebResponse Dim theRequest As HttpWebRequest Try 'Checks if the file exist theRequest = WebRequest.Create(Me.Filelocation) ...
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.