c# - .NET C# 使用类似于 Curl 脚本的 HTTPrequest

标签 c# .net curl httprequest

我正在使用 .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

我收到 Internet 服务器错误 500 内部错误

要发送的数据是一个 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();

我做错了什么?

最佳答案

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--

关于c# - .NET C# 使用类似于 Curl 脚本的 HTTPrequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13684555/

相关文章:

c# - 您能否使用 Linq-to-SQL 获得类似 DataReader 的流?

c# - 使用 C# foreach 元组

c# - 带有通用列表的foreach,在使用值类型时检测第一次迭代

.net - 在 foreach 中将节点添加到 LinkedList<T>

c# - 起订量参数 TargetParameterCountException : Parameter count mismatch Exception

c# - 如何使用 html agility pack 解析一个简单的页面?

curl - Elasticsearch-删除GrayLog2中的旧源

PHP curl 用于下载 Facebook 视频

r - 安装包 ‘devtools’ 在 Ubuntu 上具有非零退出状态

c# - 如何更快地取消任务