2012-12-03 12 views
2

I'm並希望使用HTTPrequest,使以同樣的方式一個職位,因爲它是寫在捲曲:.NET C#中使用的HttpRequest類似使用.NET C#捲曲腳本

curl 
--header ’Accept: text/html’ 
--user ’test1:password’ 
--Form ’wait=0’ 
--Form ’[email protected];type=text/csv’ 
some URL address here 

I'm越來越Error 500 Internal error from the Internet server

要發送的數據是CSV文件。

我在C#源代碼:

string data = "Time, A, B\n20120101, 100, 24\n20120102\n, 101, 27"; 

// Create a request using a URL that can receive a post. 
        request = WebRequest.Create(url); 
        //Set authorization 
        request.Credentials = new NetworkCredential(username, password); 
        // Set the Method property of the request to POST. 
        request.Method = "POST"; 

        // Create POST data and convert it to a byte array. 
        byte[] byteArray = Encoding.UTF8.GetBytes(data); 
        // Set the ContentType property of the WebRequest. 
        request.ContentType = "application/x-www-form-urlencoded"; 
        // Set the ContentLength property of the WebRequest. 
        request.ContentLength = byteArray.Length; 
        // Get the request stream. 
        dataStream = request.GetRequestStream(); 
        // Write the data to the request stream. 
        dataStream.Write(byteArray, 0, byteArray.Length); 
        // Close the Stream object. 
        dataStream.Close(); 

// Get the original response. 
       WebResponse response = request.GetResponse(); 
       this.Status = ((HttpWebResponse)response).StatusDescription; 
       // Get the stream containing all content returned by the requested server. 
       dataStream = response.GetResponseStream(); 
       // Open the stream using a StreamReader for easy access. 
       StreamReader reader = new StreamReader(dataStream); 
       // Read the content fully up to the end. 
       string responseFromServer = reader.ReadToEnd(); 
       // Clean up the streams. 
       reader.Close(); 
       dataStream.Close(); 
       response.Close(); 

我在做什麼錯?

+0

當內容類型設置爲'application/x-www-form-urlencoded',那麼它通常會被格式化爲一個'GET'請求。另一個選項是'multipart/form-data',我不太確定。當然,最好的方法是查看兩者產生的請求,看看它們之間有什麼不同...... – Chris

+0

不知道您的請求是否正常,但500代碼意味着服務器遇到錯誤,而不是這應該是由於不好的請求而發生的。 – user1852503

+0

如果你看看拋出的異常,你會看到一個響應流。看看內容,服務器可能會給你一些更有幫助的錯誤細節。 –

回答

0

cURL使用multipart/form-data內容類型來創建它的請求。我認爲這是你的情況所必需的,因爲你正在傳遞一個文本參數和一個文件。可悲的是.Net框架似乎並沒有建立在支持創建這種內容類型。

沒有給出示例代碼怎麼自己在這裏做的SO一個問題:Multipart forms from C# client

僅供參考由捲曲命令生成的HTTP請求看起來是這樣的:

POST http://www.example.com/example HTTP/1.1 
Authorization: Basic J3Rlc3Q6cGFzc3dvcmQn 
User-Agent: curl/7.33.0 
Host: www.example.com 
Accept: text/html 
Connection: Keep-Alive 
Content-Length: 335 
Expect: 100-continue 
Content-Type: multipart/form-data; boundary=------------------------99aa46d554951aa6 

--------------------------99aa46d554951aa6 
Content-Disposition: form-data; name="'wait" 

0' 
--------------------------99aa46d554951aa6 
Content-Disposition: form-data; name="'data"; filename="data.csv" 
Content-Type: text/csv' 

Time, A, B\n20120101, 100, 24\n20120102\n, 101, 27 

--------------------------99aa46d554951aa6--