C# .NET - an exception occurred during a webclient request
Asked By manish soni on 03-Feb-11 08:17 AM
string webAddress = @"http://test-web-site/nbf/";
WebClient webClient = new System.Net.WebClient();
webClient.Credentials = new NetworkCredential("username", "password");
WebRequest serverRequest = WebRequest.Create(webAddress);
WebResponse serverResponse;
serverResponse = serverRequest.GetResponse();
serverResponse.Close();
webClient.UploadFile(webAddress, "PUT", savefilename);
webClient.Dispose();
webClient = null;
this code generate Exception: "an exception occurred during a webclient request "
Peter Bromberg replied to manish soni on 03-Feb-11 08:47 AM
You have to wrap any code that might generate an exception in a try / catch / finally block if you want to become a professional. In the catch block, you can place a breakpoint and examine the exception and its properties. In particular, every exception instance has an InnerException which often holds more specific details.
You are attempting a PUT verb and it is possible that that webserver where you want to upload the file is not enabled for WEBDAV and denies a PUT. The default method for UploadFile is "POST".
olvin j replied to manish soni on 03-Feb-11 09:28 AM
Hi manish
Try
{
string webAddress = @"http://test-web-site/nbf/";
WebClient webClient = new System.Net.WebClient();
webClient.Credentials = new NetworkCredential("username", "password");
WebRequest serverRequest = WebRequest.Create(webAddress);
WebResponse serverResponse;
serverResponse = serverRequest.GetResponse();
serverResponse.Close();
webClient.UploadFile(webAddress, "PUT", savefilename);
webClient.Dispose();
webClient = null;
}
catch(Exception ex)
{
throw ex;
}
Thanks