vendredi 27 février 2015

Webclient equivalent of CURL command

I am trying to upload a file via a POST to a REST API in a c# winform.


If I run the following command with curl the file is uploaded successfully:



curl.exe -H "Content-type: application/octet-stream" -X POST http://myapiurl --data-binary @C:\test.docx


I have tried using WebClient in my WinForm:



using (var client = new WebClient())
{
client.Headers.Add("Content-Type", "application/octet-stream");

byte[] result = client.UploadFile(url, file);
string responseAsString = Encoding.Default.GetString(result);
tb_result.Text += responseAsString;
}


But I just get a (500) Internal Server.


Checking this with fiddler the following headers are added with CURL:



POST http://myapiurl HTTP/1.1
User-Agent: curl/7.33.0
Host: 10.52.130.121:90
Accept: */*
Connection: Keep-Alive
Content-type: application/octet-stream
Content-Length: 13343
Expect: 100-continue


But checking my WebClient method shows the following:



POST http://myapiurl HTTP/1.1
Accept: */*
Content-Type: multipart/form-data; boundary=---------------------8d220bbd95f8b18
Host: 10.52.130.121:90
Content-Length: 13536
Expect: 100-continue
Connection: Keep-Alive


How can I simulate the CURL command above from my app?


Aucun commentaire:

Enregistrer un commentaire