file upload error
-
(sorry I ask again, not resolved for a week before.) Hello everyone, Here is my code at both client side and server side. My code is simple, just upload a file to an ASP.Net web site. My client code throws exception when it works on Vista (x64, Enterprise, SP1), but works fine on Windows Server 2003. Any ideas? 10.10.12.162 is my server address.
Client:
static void Main(string\[\] args) { Console.Write("\\nPlease enter the URI to post data to : "); String uriString = Console.ReadLine(); WebClient myWebClient = new WebClient(); Console.WriteLine("\\nPlease enter the fully qualified path of the file to be uploaded to the URI"); string fileName = Console.ReadLine(); Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString); DateTime begin = DateTime.Now; byte\[\] responseArray = null; try { responseArray = myWebClient.UploadFile(uriString, fileName); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.ToString()); } DateTime end = DateTime.Now; Console.WriteLine("Elapsed time is: {0}", (end - begin).TotalMilliseconds); }
Server:
public partial class FileUploadHandler : System.Web.UI.Page { protected void Page\_Load(object sender, EventArgs e) { foreach (string f in Request.Files.AllKeys) { HttpPostedFile file = Request.Files\[f\]; file.SaveAs("D:\\\\UploadFile\\\\UploadedFiles\\\\" + file.FileName); } } }
Exception from client side:
Unable to connect to the remote server
System.Net.WebException: Unable to connect to the remote server ---> System.Net.
Sockets.SocketException: No connection could be made because the target machine
actively refused it 10.10.12.162:1031
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
IAsyncResult asyncResult, Int32 timeout, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.WebClient.UploadFile(Uri address, String method, String fileNam
e)
at FileUploadClien -
(sorry I ask again, not resolved for a week before.) Hello everyone, Here is my code at both client side and server side. My code is simple, just upload a file to an ASP.Net web site. My client code throws exception when it works on Vista (x64, Enterprise, SP1), but works fine on Windows Server 2003. Any ideas? 10.10.12.162 is my server address.
Client:
static void Main(string\[\] args) { Console.Write("\\nPlease enter the URI to post data to : "); String uriString = Console.ReadLine(); WebClient myWebClient = new WebClient(); Console.WriteLine("\\nPlease enter the fully qualified path of the file to be uploaded to the URI"); string fileName = Console.ReadLine(); Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString); DateTime begin = DateTime.Now; byte\[\] responseArray = null; try { responseArray = myWebClient.UploadFile(uriString, fileName); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.ToString()); } DateTime end = DateTime.Now; Console.WriteLine("Elapsed time is: {0}", (end - begin).TotalMilliseconds); }
Server:
public partial class FileUploadHandler : System.Web.UI.Page { protected void Page\_Load(object sender, EventArgs e) { foreach (string f in Request.Files.AllKeys) { HttpPostedFile file = Request.Files\[f\]; file.SaveAs("D:\\\\UploadFile\\\\UploadedFiles\\\\" + file.FileName); } } }
Exception from client side:
Unable to connect to the remote server
System.Net.WebException: Unable to connect to the remote server ---> System.Net.
Sockets.SocketException: No connection could be made because the target machine
actively refused it 10.10.12.162:1031
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
IAsyncResult asyncResult, Int32 timeout, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.WebClient.UploadFile(Uri address, String method, String fileNam
e)
at FileUploadClienThe stack trace doesn't match the code you've posted - look at the overload of
UploadFile
. Anyway, one suggestion: are you puttinghttp://
in theuriString
? WebClient can use different protocols and may be getting confused. The stack trace shows a failed connection to port 1031 which is being blocked by the firewall on your server. Nick---------------------------------- Be excellent to each other :)
-
The stack trace doesn't match the code you've posted - look at the overload of
UploadFile
. Anyway, one suggestion: are you puttinghttp://
in theuriString
? WebClient can use different protocols and may be getting confused. The stack trace shows a failed connection to port 1031 which is being blocked by the firewall on your server. Nick---------------------------------- Be excellent to each other :)
Thanks Nick, I have tried adding http:// is not working. I have posted my stack error and my code again. Hope it matches. :-) Any ideas what is wrong?
class Program { static void Main(string\[\] args) { Console.Write("\\nPlease enter the URI to post data to : "); String uriString = Console.ReadLine(); WebClient myWebClient = new WebClient(); Console.WriteLine("\\nPlease enter the fully qualified path of the file to be uploaded to the URI"); string fileName = Console.ReadLine(); Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString); DateTime begin = DateTime.Now; byte\[\] responseArray = null; try { responseArray = myWebClient.UploadFile(uriString, fileName); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.ToString()); } DateTime end = DateTime.Now; Console.WriteLine("Elapsed time is: {0}", (end - begin).TotalMilliseconds); } }
Unable to connect to the remote server
System.Net.WebException: Unable to connect to the remote server ---> System.Net.
Sockets.SocketException: No connection could be made because the target machine
actively refused it 10.10.12.162:1031
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
IAsyncResult asyncResult, Int32 timeout, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.WebClient.UploadFile(Uri address, String method, String fileNam
e)
at FileUploadClient.Program.Main(String[] args) in C:\software\UploadFile\Net
work_Monitor_Demo\FileUploadClient\Program.cs:line 27
Elapsed time is: 4579regards, George
-
Thanks Nick, I have tried adding http:// is not working. I have posted my stack error and my code again. Hope it matches. :-) Any ideas what is wrong?
class Program { static void Main(string\[\] args) { Console.Write("\\nPlease enter the URI to post data to : "); String uriString = Console.ReadLine(); WebClient myWebClient = new WebClient(); Console.WriteLine("\\nPlease enter the fully qualified path of the file to be uploaded to the URI"); string fileName = Console.ReadLine(); Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString); DateTime begin = DateTime.Now; byte\[\] responseArray = null; try { responseArray = myWebClient.UploadFile(uriString, fileName); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.ToString()); } DateTime end = DateTime.Now; Console.WriteLine("Elapsed time is: {0}", (end - begin).TotalMilliseconds); } }
Unable to connect to the remote server
System.Net.WebException: Unable to connect to the remote server ---> System.Net.
Sockets.SocketException: No connection could be made because the target machine
actively refused it 10.10.12.162:1031
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
IAsyncResult asyncResult, Int32 timeout, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.WebClient.UploadFile(Uri address, String method, String fileNam
e)
at FileUploadClient.Program.Main(String[] args) in C:\software\UploadFile\Net
work_Monitor_Demo\FileUploadClient\Program.cs:line 27
Elapsed time is: 4579regards, George
The stack trace is still a bit off - are you using a Release build? You could try a Debug build to help track this down. I don't understand why
WebClient
is trying to connect on port 1031. It should be using an HTTP POST on port 80. Could you catch theWebException
explicitly and print theStatus
code? Also, 4.5 seconds is a long time accross a LAN. How big is the file you are sending? IIS has a limit of 4MB, but you can set this using MaxRequestLength[^] in your web.config:<system.web>
<httpRuntime maxRequestLength="10240" />If none of that works, you could try using
UploadFileAsync
and hooking theUploadProgressChanged
event to see if anything is being sent. Nick---------------------------------- Be excellent to each other :)
-
The stack trace is still a bit off - are you using a Release build? You could try a Debug build to help track this down. I don't understand why
WebClient
is trying to connect on port 1031. It should be using an HTTP POST on port 80. Could you catch theWebException
explicitly and print theStatus
code? Also, 4.5 seconds is a long time accross a LAN. How big is the file you are sending? IIS has a limit of 4MB, but you can set this using MaxRequestLength[^] in your web.config:<system.web>
<httpRuntime maxRequestLength="10240" />If none of that works, you could try using
UploadFileAsync
and hooking theUploadProgressChanged
event to see if anything is being sent. Nick---------------------------------- Be excellent to each other :)
Hi Nick! Thanks for so many good ideas! I have followed your advice and here is my results. 1. The port 1031 is the Visual Studio's internal web server address port number -- i.e. when I press F5 in Visual Studio 2008 for the ASP.Net project. I have tried when I publish the ASP.Net project at server side to IIS and using port 80, everything is fine, but when pressing F5 to run in Visual Studio, there is such error. Any ideas? 2. I have tracked the Status property for the WebException, the value is "ConnectFailure". Any ideas? 3. The file is very small, about 1k and is a text file. From my 3 points above, do you have any ideas why it fails to use Visual Studio internal web server and success to use IIS? regards, George
-
Hi Nick! Thanks for so many good ideas! I have followed your advice and here is my results. 1. The port 1031 is the Visual Studio's internal web server address port number -- i.e. when I press F5 in Visual Studio 2008 for the ASP.Net project. I have tried when I publish the ASP.Net project at server side to IIS and using port 80, everything is fine, but when pressing F5 to run in Visual Studio, there is such error. Any ideas? 2. I have tracked the Status property for the WebException, the value is "ConnectFailure". Any ideas? 3. The file is very small, about 1k and is a text file. From my 3 points above, do you have any ideas why it fails to use Visual Studio internal web server and success to use IIS? regards, George
So, you've been entering "http://10.10.12.162:1031" for uriString ??? This will be blocked by the firewall on the machine running VS. This means that if you run the client on the same machine, it will work because you're not crossing the firewall. If you run the client on a different machine, it will try to go through the firewall on port 1031 and ( quite rightly ) be blocked => ConnectFailure. It works on IIS using port 80 because this port is open on that machine's firewall - port 80 is the well known port for HTTP. Your code is fine - it's a network admin problem. Nick
---------------------------------- Be excellent to each other :)
-
(sorry I ask again, not resolved for a week before.) Hello everyone, Here is my code at both client side and server side. My code is simple, just upload a file to an ASP.Net web site. My client code throws exception when it works on Vista (x64, Enterprise, SP1), but works fine on Windows Server 2003. Any ideas? 10.10.12.162 is my server address.
Client:
static void Main(string\[\] args) { Console.Write("\\nPlease enter the URI to post data to : "); String uriString = Console.ReadLine(); WebClient myWebClient = new WebClient(); Console.WriteLine("\\nPlease enter the fully qualified path of the file to be uploaded to the URI"); string fileName = Console.ReadLine(); Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString); DateTime begin = DateTime.Now; byte\[\] responseArray = null; try { responseArray = myWebClient.UploadFile(uriString, fileName); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.ToString()); } DateTime end = DateTime.Now; Console.WriteLine("Elapsed time is: {0}", (end - begin).TotalMilliseconds); }
Server:
public partial class FileUploadHandler : System.Web.UI.Page { protected void Page\_Load(object sender, EventArgs e) { foreach (string f in Request.Files.AllKeys) { HttpPostedFile file = Request.Files\[f\]; file.SaveAs("D:\\\\UploadFile\\\\UploadedFiles\\\\" + file.FileName); } } }
Exception from client side:
Unable to connect to the remote server
System.Net.WebException: Unable to connect to the remote server ---> System.Net.
Sockets.SocketException: No connection could be made because the target machine
actively refused it 10.10.12.162:1031
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
IAsyncResult asyncResult, Int32 timeout, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.WebClient.UploadFile(Uri address, String method, String fileNam
e)
at FileUploadClienAnd see File Uploading in ASP.NET Using C#[^] for more info. :)
Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )