473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Timeout problem with WC.UploadData

I use a WinForm app for users to upload a file to my server. Basically one
line of code;

Response = WC.UploadData(MyUrl, "PUT", MyByteArray)

Works fine except for larger files. After about 104 seconds I get a
System.Net.WebException in System.dll. The message is "The operation has
timed out".

On my server, I have modified the machine.config file for the httpRuntime
executionTimeout and have changed it from 90 seconds to 900 seconds. Even
after rebooting the server, this has no effect.

Anyone got a clue as to what timer is causing the problem and how to fix it.
Depending on the connection speed this severly limits the size of file that
may be uploaded.

Interesting in that if you just ignore the exception, the upload sometimes
continues to completion.

Thanks
Russell Stevens
Nov 20 '05 #1
6 4294
Hi Russell,

I think the WebClient doesnot support the timeout properties, you may try
to use the HttpWebRequest to achieve your aim. Here goes the sample code.

I converted from C# code in the link below
http://www.netacademia.net/tudastar/...t.asp?upid=554

Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Namespace HttpCopy
Class App
Private args() As String
Private cred As ICredentials
Public Shared Sub Main(ByVal args() As String)
Dim app As New App
app.args = args
app.Run()
End Sub 'Main
Private Sub Run()
If args.Length < 2 Then
Usage()
Return
End If
Dim [source] As String = args(0)
Dim dest As String = args(1)
If CheckArgs() Then
cred = MakeCredentials()

If Directory.Exists([source]) Then
'Source is a folder, we have to enumerate
'all the files (not recursive)
If Not dest.EndsWith("/") Then
Console.WriteLine(("If source is a folder
destination must be" + " a folder too (/ at the end of it)."))
Return
End If

Dim file As String
For Each file In Directory.GetFiles([source])
CopyFileWithProgress(file, dest +
Path.GetFileName(file))
Next file
Else
'Only a file
If dest.EndsWith("/") Then
dest += Path.GetFileName([source])
End If
CopyFileWithProgress([source], dest)
End If
End If
End Sub 'Run

Private Sub CopyFileWithProgress(ByVal [source] As String, ByVal
dest As String)
Dim wt As TimeSpan

If CopyFile([source], dest, wt) Then
Console.WriteLine("Successfully copied in {0}.{1} sec.",
wt.Seconds, wt.Milliseconds)
End If
End Sub 'CopyFileWithProgress
Private Function CopyFile(ByVal [source] As String, ByVal dest As
String, ByRef workingTime As TimeSpan) As Boolean
Dim startTime As System.DateTime = DateTime.Now

Dim completed As Boolean = False

Dim response As String = Nothing
Try
Dim uploader As HttpWebRequest =
CType(WebRequest.Create(New Uri(dest)), HttpWebRequest)
uploader.Timeout = 60000000
uploader.Method = "PUT"
uploader.ContentLength = New FileInfo([source]).Length
uploader.Credentials = cred

Dim sourceStream As New FileStream([source], FileMode.Open,
FileAccess.Read, FileShare.ReadWrite)

Console.WriteLine("Copying {0} to {1}",
Path.GetFullPath([source]), dest)
Dim requestStream As Stream = uploader.GetRequestStream()
Dim buff(4095) As Byte

Dim pos As Integer = 0
pos = sourceStream.Read(buff, 0, buff.Length)
While pos <> 0 'ToDo: Unsupported feature: assignment
within expression.
requestStream.Write(buff, 0, pos)
pos = sourceStream.Read(buff, 0, buff.Length)
End While
Dim resp As HttpWebResponse = CType(uploader.GetResponse(),
HttpWebResponse)
Dim respReader As New StreamReader(resp.GetResponseStream())
response = respReader.ReadToEnd()
completed = True
Catch ex As Exception
Console.WriteLine("Error while uploading file {0}, a hiba
oka:" + ControlChars.Lf + " {1}", [source], ex)
Console.WriteLine(ControlChars.Lf + "Http response:" +
ControlChars.Lf + " {0}", IIf(response Is Nothing, "No Response", response))
Finally
workingTime = System.DateTime.op_Subtraction(DateTime.Now,
startTime)
End Try
Return completed
End Function 'CopyFile
Private Function MakeCredentials() As ICredentials
Dim user, pass, domain As String
user = GetSwitchValue("user")
pass = GetSwitchValue("password")
domain = GetSwitchValue("domain")
If domain Is Nothing Then
domain = ""
End If
If Not (user Is Nothing) Then
Return New NetworkCredential(user, pass, domain)
Else
Return CredentialCache.DefaultCredentials
End If
End Function 'MakeCredentials
Private Function GetSwitchValue(ByVal switchName As String) As
String
switchName = switchName.ToLower()
Dim arg As String
For Each arg In args
If arg.ToLower().StartsWith(("/" + switchName + ":")) Then
Return arg.Split(":".ToCharArray())(1).Trim()
End If
Next arg
Return Nothing
End Function 'GetSwitchValue
Private Function CheckArgs() As Boolean
'is dest a valid uri?
Try
Dim destUri As New Uri(args(1))
Return True
Catch
Console.WriteLine("Invalid destination URL: {0}", args(1))
Return False
End Try
End Function 'CheckArgs
Private Sub Usage()
Dim usage As String = "httpcopy: copies local files to http
server via HTTP PUT method." + ControlChars.Cr + ControlChars.Lf +
"Copyright (C) NetAcademia 1900-2003. No right reserved." + ControlChars.Cr
+ ControlChars.Lf + "Author: zs*********@netacademia.net" + ControlChars.Cr
+ ControlChars.Lf + "Usage:" + ControlChars.Cr + ControlChars.Lf +
"httpcopy sourceFile destFile" + " [/user:uname, /password:pass,
/domain:dname]" + ControlChars.Cr + ControlChars.Lf + "Examples:" +
ControlChars.Cr + ControlChars.Lf + "httpcopy a.txt
http://www.netacademia.net/xxx/b.txt" + ControlChars.Cr + ControlChars.Lf +
"httpcopy a.txt http://www.netacademia.net/xxx/" + ControlChars.Cr +
ControlChars.Lf + "httpcopy c:\temp http://www.netacademia.net/xxx/" +
ControlChars.Cr + ControlChars.Lf
Console.WriteLine(usage)
End Sub 'Usage
End Class 'App
End Namespace 'HttpCopy
If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #2
Peter,

Thanks - using HttpWebRequest does get around the timeout problem with
WebClient.

MICROSOFT - both HttpWebRequest and WebClient STILL upload each byte THREE
times when you use basic authentication - ie - a 3 MB file requires you to
upload 9 MB. This was reported years ago - when is it going to be fixed?

Also, the fact that WebClient cannot be used as documented to upload a file
that takes more than 100 seconds makes is pretty useless. Figure out how
large a file can be uploaded in 100 seconds over a dialup connection. Then
since most uploads will require at least basic authentication, divide that
by three (unless you can figure out how to send your own headers which is
not documented anywhere). You are only left with the ability to upload very
small files - please fix this mess.

Thanks
Russell Stevens
Nov 20 '05 #3
HI Russell,
MICROSOFT - both HttpWebRequest and WebClient STILL upload each byte THREE
times when you use basic authentication - ie - a 3 MB file requires you to
upload 9 MB. This was reported years ago - when is it going to be fixed?


I am sorry, but what do you mean both HttpWebRequest and WebClient STILL
upload each byte THREE times when you use basic authentication. Based on my
test, if I provided the username and password, the packet will be directed
accepted by the webserver. ( I trace by using NetMon)

So can you provide the steps for me to reproduce the problem.

If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #4
Peter,

<<So can you provide the steps for me to reproduce the problem.>>

When using WC.UploadData with credentials for basic authentication, every
packet is sent 3 times. ie - every packet is authenticated instead of
authenticating once, and then uploading the entire file. Microsoft verified
that this is indeed a problem (SRX030305602529). The work around is to NOT
use credentials but to send the authentication information in custom
headers.

So, WC.UploadData is limited to 100 seconds (not documented and not
adjustable), and when used as documented with credentials for basic
authentication, sends every byte three times, so the maximum file that it
may upload is what you can get on a normal dialup for 33 seconds. ie - 49
Kbps for 33 seconds is 202 KBytes. In other words, it is USELESS - Microsoft
needs to fix this.

If you want to verify it all you need to do is use WC.UploadData with
credentials for basic authentication and upload to a site that requires
basic authentication and look at the bytes sent in Task Manager. Uploading a
1 MB file will result in sending 3 MB (plus change for overhead). Note that
this was NOT a problem in VB6 using XMLHTTP.

Thanks
Russ Stevens
Nov 20 '05 #5
Peter,

<<So can you provide the steps for me to reproduce the problem.>>

When using WC.UploadData with credentials for basic authentication, every
packet is sent 3 times. ie - every packet is authenticated instead of
authenticating once, and then uploading the entire file. Microsoft verified
that this is indeed a problem (SRX030305602529). The work around is to NOT
use credentials but to send the authentication information in custom
headers.

So, WC.UploadData is limited to 100 seconds (not documented and not
adjustable), and when used as documented with credentials for basic
authentication, sends every byte three times, so the maximum file that it
may upload is what you can get on a normal dialup for 33 seconds. ie - 49
Kbps for 33 seconds is 202 KBytes. In other words, it is USELESS - Microsoft
needs to fix this.

If you want to verify it all you need to do is use WC.UploadData with
credentials for basic authentication and upload to a site that requires
basic authentication and look at the bytes sent in Task Manager. Uploading a
1 MB file will result in sending 3 MB (plus change for overhead). Note that
this was NOT a problem in VB6 using XMLHTTP.

Thanks
Russ Stevens
Nov 20 '05 #6
Hi Russell,

Thank you for using MSDN Newsgroup!

As for the problem that the Webclient will upload data three times to an
webserver with basic authentication, I have contacted the Case Owner of
SRX030305602529. If I get any updated information, I will update with you
as soon as possible.

If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #7

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

Similar topics

0
by: Dave R | last post by:
Hi all, I am facing a problem with UploadData function. Here is my code - WebClient wc = new WebClient(); wc.Headers.Add("Content-Type: application/"); rgBuffer =...
1
by: Raj | last post by:
I am using WebClient.UploadData(URL) to upload a strng to the particular URL. How do I read the uploaded data or where excatly the data is stored, so that I can use in my destination web page. ...
4
by: DavidS | last post by:
First: There are several ways to confuse one regarding session timeout. (1) web.config - <sessionState timeout="20"> (2) IIS Manager | Internet Information Services | ServerNode | Default Web Site...
2
by: Tim::.. | last post by:
Can someone please tell me why when I refer to a Sub in my User Control I get the following error... Error:.. Reference to a non-shared member requires an object reference For some reason I...
2
by: Tim::.. | last post by:
Can someone tell me why I get this error??? And how do I fix it?? Thanks Compiler Error Message: BC30408: Method 'Public Sub UploadData()' does not have the same signature as delegate...
3
by: bss2004 | last post by:
Help! I'm posting a PDF Doc to a remote server using WebClient UploadData and the following code. The DOC posts fine and the server returns a positive response. If I access the remote file in...
0
by: Mark | last post by:
I have a very strange problem that is driving me nuts. My application is using the webclient object to send data to a webserver running on a unix box. We use 2 different environments, 1 for...
22
by: Nick Craig-Wood | last post by:
Did anyone write a contextmanager implementing a timeout for python2.5? I'd love to be able to write something like with timeout(5.0) as exceeded: some_long_running_stuff() if exceeded:...
5
by: Masso | last post by:
Hallo to the group, my question is this: where i have to set timeout in a web application? If i set into sessionState with property Timeout="60", after 20 minutes the session is over. Always...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.