473,748 Members | 4,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

WebRequest: underlying connection was closed - what the hell?

I'm having troubles with uploading files to the ftp server using
FtpWebRequest. I occasionally get an Exception saying: "The underlying
connection was closed: An unexpected error occurred on a receive". but
what's wired i get this exception just after upload is finished when i
try to close the stream that was used for upload. this happens only when
upload takes more that certain amount of time. note that upload itself
goes smooth no matter how long it takes. here is a code example:

FileInfo fi = ObtainFile(...) ;
FtpWebRequest ftp = ObtainRequest(. ..);

const int BufferSize = 2048;
byte[] content = new byte[BufferSize - 1 + 1];
int dataRead = 0;
//open file for reading
using (FileStream fs = fi.OpenRead())
{
try
{
//open request to send
using (Stream rs = ftp.GetRequestS tream())
{
do
{
dataRead = fs.Read(content , 0, BufferSize);
rs.Write(conten t, 0, dataRead);
bytesSent += dataRead;
} while (!(dataRead < BufferSize) || bytesSent < fiSize);
} // EXCEPTION IS THROWN HERE when stream is being closed.
}
catch (Exception....)
{
if (bytesSent == fiSize)
{ /* we need to ignore exception cause transfer was completed */ }
else
{ /* handle the exception */ }
}
}

any ideas how to prevent this? it seems that exception is thrown due to
the timeout because it takes quite long after upload finished and the
exception is thrown - program 'hangs' for some time after it leaves do
while loop until exception is thrown.
Dec 18 '06 #1
2 8057
Try changing your while condition to

} while (dataRead 1);

Not positive that will fix your situation, but it should be more
correct than what you're currently using and it might shed some light
on the issue.

Bruce Dunwiddie
http://www.csvreader.com

SharpCoderMP wrote:
I'm having troubles with uploading files to the ftp server using
FtpWebRequest. I occasionally get an Exception saying: "The underlying
connection was closed: An unexpected error occurred on a receive". but
what's wired i get this exception just after upload is finished when i
try to close the stream that was used for upload. this happens only when
upload takes more that certain amount of time. note that upload itself
goes smooth no matter how long it takes. here is a code example:

FileInfo fi = ObtainFile(...) ;
FtpWebRequest ftp = ObtainRequest(. ..);

const int BufferSize = 2048;
byte[] content = new byte[BufferSize - 1 + 1];
int dataRead = 0;
//open file for reading
using (FileStream fs = fi.OpenRead())
{
try
{
//open request to send
using (Stream rs = ftp.GetRequestS tream())
{
do
{
dataRead = fs.Read(content , 0, BufferSize);
rs.Write(conten t, 0, dataRead);
bytesSent += dataRead;
} while (!(dataRead < BufferSize) || bytesSent < fiSize);
} // EXCEPTION IS THROWN HERE when stream is being closed.
}
catch (Exception....)
{
if (bytesSent == fiSize)
{ /* we need to ignore exception cause transfer was completed */ }
else
{ /* handle the exception */ }
}
}

any ideas how to prevent this? it seems that exception is thrown due to
the timeout because it takes quite long after upload finished and the
exception is thrown - program 'hangs' for some time after it leaves do
while loop until exception is thrown.
Dec 18 '06 #2
but the problem is not the loop itself. loop condition has nothing to do
here. problem is with the connection that seems to close immediately
after last sent byte what causes an exception when code leaves 'using'
statement. at least that is what i 'think' :/

shriop wrote:
Try changing your while condition to

} while (dataRead 1);

Not positive that will fix your situation, but it should be more
correct than what you're currently using and it might shed some light
on the issue.

Bruce Dunwiddie
http://www.csvreader.com

SharpCoderMP wrote:
>I'm having troubles with uploading files to the ftp server using
FtpWebReques t. I occasionally get an Exception saying: "The underlying
connection was closed: An unexpected error occurred on a receive". but
what's wired i get this exception just after upload is finished when i
try to close the stream that was used for upload. this happens only when
upload takes more that certain amount of time. note that upload itself
goes smooth no matter how long it takes. here is a code example:

FileInfo fi = ObtainFile(...) ;
FtpWebReques t ftp = ObtainRequest(. ..);

const int BufferSize = 2048;
byte[] content = new byte[BufferSize - 1 + 1];
int dataRead = 0;
//open file for reading
using (FileStream fs = fi.OpenRead())
{
try
{
//open request to send
using (Stream rs = ftp.GetRequestS tream())
{
do
{
dataRead = fs.Read(content , 0, BufferSize);
rs.Write(conten t, 0, dataRead);
bytesSent += dataRead;
} while (!(dataRead < BufferSize) || bytesSent < fiSize);
} // EXCEPTION IS THROWN HERE when stream is being closed.
}
catch (Exception....)
{
if (bytesSent == fiSize)
{ /* we need to ignore exception cause transfer was completed */ }
else
{ /* handle the exception */ }
}
}

any ideas how to prevent this? it seems that exception is thrown due to
the timeout because it takes quite long after upload finished and the
exception is thrown - program 'hangs' for some time after it leaves do
while loop until exception is thrown.
Dec 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2742
by: Rachet? | last post by:
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. ...
1
1491
by: GSK | last post by:
This one has me stumped: I am using HttpWebRequest to resolve an external URL (that outputs an XML string). It works fine on my dev machine (W2K), and used to work on my production machine (W2K3). It seems that now, for some reason, the connection cannot be established. The error logged is: The underlying connection was closed: Unable to connect to the remote server." source="System" stack="at...
1
1167
by: Nelson R. | last post by:
Hi, i need to get some info from a website page that requires an certificate. Ive got the provided certificate installed in IE, and when accessing the website page, it shows a window to select the client certificate and then shows the page correctly. Im trying to do this by code (vs2003 C# aspnet), using webrequest.
26
16519
by: Rajeev Tipnis | last post by:
http://support.microsoft.com/default.aspx?scid=kb;en- us;819450 Questions: 1) Is this patch (fix) applicable to the .NET 1.1 Framework as well? That is, if we have Framework 1.1 (On Win2K3/IIS6.0), do we still need this patch? 2) Does it specifically solve only this error: The underlying connection was closed. An unexpected error
0
936
by: KSC | last post by:
Hello, I am trying to access an ethernet temperature probe in my app. The probe is properly configured. Using IE I can access the data using the IPaddress & "/temp". This returns a string containing data from a number of temperature probes attached to the hub. For example: input "http://192.168.1.110/temp" returns "Probe 1|75.5|Probe 2|78.9|Probe 3|-99.9|Probe 4|-99.9"
5
24762
by: Vijayakrishna Pondala | last post by:
Hi, We are using the following error randomly, when accessing a webservice method/servlet hosted on JBoss application server: The underlying connection was closed: An unexpected error occurred on a receive. We are using .NET v1.1.
0
1172
by: buccsailor | last post by:
Hell there, I tried to post a reply to a message thread created back in July 2006 regarding the override of WebRequest, Closed Conenctions and setting KeepAlive to false,but it's been over 60 days so I am creating a new topci in the hopes of an answer to a related issue. GhostAK suggested some vb.net code that should work to allow the setting of the Keep Alive property. yes, I am having connection close errors. The code suggested...
0
1641
by: =?Utf-8?B?T2xpdmllciBHSUw=?= | last post by:
Hello, I try to post an HTTP message containing an XML document, and I get the following exception : System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---System.IO.IOException: Unable to write data to the transport connection: Uma solicitação de envio ou recepção de dados não foi permitida porque o socket não está ligado e (durante o envio num socket de datagrama utilizando...
4
4578
by: Sin Jeong-hun | last post by:
I don't get the message so it's hard to debug that, but some of my clients report that they get "The underlying connection was closed unexpectedly" exception. According to this site (http:// www.dotnetspider.com/resources/2596-e-underlying-connection-was-closed-A-connection.aspx), it's a bug of .NET 2.0, and the author suggests that we use KeepAlive=false until Microsoft fixes it. It seems like almost 5 years have passes since the...
0
8987
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
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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
9316
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
9241
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
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
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
3
2211
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.