views:

604

answers:

1

Okay so here is the deal. As the question states, I'm trying to POST a file to a webserver and am having a few issues.

I've tried posting this same file to the same webserver using Curl.exe and have had no issues. I've posted the flags I used with curl just incase they might point out any potential reasons why I'm having trouble with the .NET classes.

curl.exe --user "myUser:myPass" --header "Content-Type: application/gzip"  
--data-binary "@filename.txt.gz" --cookie "data=service; data-ver=2; date=20100212;  
time=0900; location=1234" --output "out.txt" --dump-header "header.txt"  
http://mysite/receive

I'm trying to use a .NET class like WebClient or HttpWebRequest to do the same thing. Here is a sample of the code I've tried. With the WebClient I get a 505 HTTP Version Not Supported error and with the HttpWebRequest I get a 501 Not Implemented.

When trying it with a WebClient:

public void sendFileClient(string path){
    string url = "http://mysite/receive";  
    WebClient wc = new WebClient();  
    string USERNAME = "myUser";  
    string PSSWD = "myPass";  

    NetworkCredential creds = new NetworkCredential(USERNAME, PSSWD);  
    wc.Credentials = creds;  
    wc.Headers.Set(HttpRequestHeader.ContentType, "application/gzip");  
    wc.Headers.Set("Cookie", "location=1234; date=20100226; time=1630; data=service; data-ver=2");  

    wc.UploadFile(url, "POST", path);
}

And while using a HttpRequest:

public Stream sendFile(string path)
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://myserver/receive");  
    string USERNAME = "myUser";    
    string PSSWD = "myPass";  
    NetworkCredential creds = new NetworkCredential(USERNAME, PSSWD);  
    request.Credentials = creds;  
    request.Method = "POST";  
    request.ContentType = "application/gzip";    
    request.Headers.Set("Cookie", "location=1234; date=20100226; time=1630; data=service; data-ver=2");  
    FileInfo fInfo = new FileInfo(path);  
    long numBytes = fInfo.Length;  
    FileStream fStream = new FileStream(path, FileMode.Open, FileAccess.Read);  
    BinaryReader br = new BinaryReader(fStream);  
    byte[] data = br.ReadBytes((int)numBytes);  
    br.Close();  
    fStream.Close();  
    fStream.Dispose();  
    Stream wrStream = request.GetRequestStream();  
    BinaryWriter bw = new BinaryWriter(wrStream);  
    bw.Write(data);  
    bw.Close();  
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
    return response.GetResponseStream();  
}
+2  A: 

First, use something like fiddler and inspect the requests and responses to see what differs between curl and System.Net.WebClient.

Also, you can try (although inspecting with the debugging proxy should allow you to pinpoint the difference):

Use the credential cache to set your credentials for basic authentication:

var cc= new CredentialCache();
cc.Add(new Uri(url),
                  "Basic", 
                  new NetworkCredential("USERNAME", "PASSWORD"));
wc.Credentials = cc;

Set a user agent header:

string _UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
wc.Headers.Add(HttpRequestHeader.UserAgent, _UserAgent);

Change the protocol version on the WebRequest:

reqeust.KeepAlive = false;
request.ProtocolVersion=HttpVersion.Version10;
thedugas
The "Change the protocol version on the WebRequest" did the trick!
Myishmael