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();
我在做什麼錯?
當內容類型設置爲'application/x-www-form-urlencoded',那麼它通常會被格式化爲一個'GET'請求。另一個選項是'multipart/form-data',我不太確定。當然,最好的方法是查看兩者產生的請求,看看它們之間有什麼不同...... – Chris
不知道您的請求是否正常,但500代碼意味着服務器遇到錯誤,而不是這應該是由於不好的請求而發生的。 – user1852503
如果你看看拋出的異常,你會看到一個響應流。看看內容,服務器可能會給你一些更有幫助的錯誤細節。 –