I am trying to do a plain multipart form upload using System.Net.WebClient and Basic Authentication.
I have had some trouble with this and have been using Fiddler2 for debugging some 401 errors I have been having with the service.
I have arrived at the code below, which succeeds while Fiddler2 is running, but fails when it does not, with the following error:
"Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host."
Code is as follows:
var wc = new MyWebClient();
var cc = new System.Net.CredentialCache();
cc.Add(new Uri(uri), "Basic", new System.Net.NetworkCredential(user, pass));
wc.Credentials = cc;
System.Net.ServicePointManager.Expect100Continue = false;
wc.UploadFile(uri + folder, file);
wc.DownloadString(uri + folder).Dump();
return;
class MyWebClient : System.Net.WebClient
{
protected override System.Net.WebRequest GetWebRequest(Uri address)
{
System.Net.WebRequest request = base.GetWebRequest(address);
if (request is System.Net.HttpWebRequest)
{
(request as System.Net.HttpWebRequest).KeepAlive = false;
}
return request;
}
}