Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带标志的C#中的cURL调用_C#_.net_Curl_Solr - Fatal编程技术网

带标志的C#中的cURL调用

带标志的C#中的cURL调用,c#,.net,curl,solr,C#,.net,Curl,Solr,我想用C#做一个下面的curl调用: 我发现我应该使用WebRequest类,但我仍然不确定如何处理这部分: -F "myfile=@tutorial.html" 中的代码段显示了如何使用WebRequest类发送POST数据: // Create a request using a URL that can receive a post. WebRequest request = WebRequest.Create("http://localhost:8983/solr/update/ex

我想用C#做一个下面的curl调用:

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

-F "myfile=@tutorial.html"
中的代码段显示了如何使用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;

作为WebRequest的替代品,您可以考虑使用WebCube类。它提供了比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");
        }

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");
        }