2

我想在 C# 中进行以下 curl 调用:

curl "http://localhost:8983/solr/update/extract?literal.id=doc1&commit=true" -F "myfile=@tutorial.html"

我发现我应该使用 WebRequest 类,但我仍然不确定如何处理这部分:

-F "myfile=@tutorial.html"
4

2 回答 2

2

来自http://msdn.microsoft.com/en-us/library/debx8sh9.aspx的代码片段展示了如何使用 WebRequest 类发送 POST 数据:

// Create a request using a URL that can receive a post. 
WebRequest request = WebRequest.Create("http://localhost:8983/solr/update/extract?literal.id=doc1&commit=true");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "myfile=@tutorial.html";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// 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;
于 2012-09-13T02:25:22.917 回答
1

作为 WebRequest 的替代方案,您可以考虑使用 WebClient 类。它提供了可能被认为比 WebRequest 更干净、更简单的语法。像这样的东西:

using (WebClient client = new WebClient())
        {
            client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

            byte[] postResult = client.UploadFile("http://localhost:8983/solr/update/extract?literal.id=doc1&commit=true", "POST", "tutorial.html");
        }

请参阅http://msdn.microsoft.com/en-us/library/esst63h0%28v=vs.100%29.aspx

于 2012-09-18T09:28:33.267 回答