tags:

views:

18

answers:

1

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;
    }
}
A: 

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.

Aliostad
Hi Aliostad, the download bit works fine. The upload doesn't work whether the download is there or not. I'm just trying to automate an upload to a simple form, slightly embarassed that something so simple has me stumed. Thought it might have something to do with PreAuthenticate and have tried WebRequest per Rick Strahl's blog @ http://www.west-wind.com/weblog/posts/243915.aspx
Solution Evangelist
I am aware of the factors above, but they don't seem to be the issue. However, I will accept this answer as I am assuming this is something idiosyncratic with the WebDAV-like functionality of http://*.mysecurebackup.net/. This is something I wanted to get running so I could programatically upload to my JungleDisk via WebClient. If anyone would like access details so that they might attempt/replicate the above themselves I am more than happy to provide them, just drop me a line.
Solution Evangelist