2012-09-13 47 views

回答

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 = "[email protected]"; 
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; 
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