2

我正在使用 .NET C# 并希望使用HTTPrequest与在 Curl 中编写的方式相同的方式进行 POST:

curl
--header ’Accept: text/html’
--user ’test1:password’
--Form ’wait=0’
--Form ’data=@data.csv;type=text/csv’
some URL address here

我得到了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();

我究竟做错了什么?

4

2 回答 2

0

cURL 使用 multipart/form-data 内容类型来创建它的请求。我认为在您的情况下这是必需的,因为您同时传递了文本参数和文件。遗憾的是,.Net 框架似乎没有内置支持创建这种内容类型。

有一个关于 SO 的问题提供了如何在此处自己操作的示例代码:Multipart forms from C# client

作为参考,该 cURL 命令生成的 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--
于 2014-11-16T09:56:10.953 回答
-1

数据不需要为表单发布进行 URL 编码吗?http://en.wikipedia.org/wiki/POST_%28HTTP%29#Use_for_submitting_web_forms

于 2012-12-03T14:29:35.197 回答