2013-03-24 65 views
0

我正在研究一個需要使用curl命令將文件加載到特定站點的項目。我希望這是用C#實現的。從看到類似的問題,我沒有發現任何似乎適用於我的東西,除非我做錯了。使用WebRequest代替curl命令

這是curl命令我使用Cygwin成功地使用:

curl -i -u user:password -F '[email protected]\Users\User\Desktop\test.jpg' -F 'json={"content": {"type": "text/html", "text": "<body><p>look at me, look at me</p></body>"}, "type": "document", "subject": "Document with Attachment"};type=application/json' http://someurl 

現在我努力使這項工作有WebRequest的,因爲我讀過,這是可能的其他職位。這是我迄今爲止所做的。

WebRequest request = WebRequest.Create("http://someurl"); 
      request.PreAuthenticate = true; 
      request.ContentType = "application/json"; 
      request.Method = "POST"; 
      string authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes("user:password")); 
      request.Headers["Authorization"] = "Basic " + authInfo; 

      //what do I set my buffer to? 

      Stream reqstr = request.GetRequestStream(); 
      reqstr.Write(buffer, 0, buffer.Length); 
      reqstr.Close(); 

      WebResponse response = request.GetResponse(); 

我該如何設置我的緩衝區到這裏寫curl命令,然後發送文件test.jpg?另外,我嘗試使用libcurl,但沒有成功。任何正確的方向指針非常感謝!

+0

您必須編寫類型的multipart/form-data的含量(捲曲送2場,一個JSON和文件的-f參數)。這裏有一個例子:http://stackoverflow.com/a/3890955/209727 – 2013-03-24 22:07:51

+0

轉儲所有認證的東西。 request.Credentials = new NetworkCredential(user,pass); – 2013-03-24 22:13:55

回答