cURL call in C# with flag

女生的网名这么多〃 提交于 2019-12-21 21:38:40

问题


I'd like to make a following curl call in C#:

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

I found that I should use WebRequest class, but I'm still not sure how deal with this part:

-F "myfile=@tutorial.html"

回答1:


The code snippet from http://msdn.microsoft.com/en-us/library/debx8sh9.aspx shows how to send POST data using the WebRequest class:

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



回答2:


As an alternative to WebRequest, you might consider using the WebClient class. It offers what might be considered to be a cleaner and simpler syntax than WebRequest. Something like this:

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

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



来源:https://stackoverflow.com/questions/12398541/curl-call-in-c-sharp-with-flag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!