System.Net.WebClient "Unable to write data to the transport connection"
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.WebReq开发者_如何转开发uest 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;
}
}
You are disabling keep alive while you are using client twice. I believe that is your problem.
What happens if you remove the download bit, only keeping upload?
UPDATE
I know that using non-basic/digest involves a round trip and will not work with keep-alive set to false but not sure about basic.
精彩评论