[英]How to set the content of an HttpWebRequest in C#?
HttpWebRequest具有ContentLength和ContentType属性,但是如何实际设置请求的内容?
The following should get you started 以下内容应该让您入门
byte[] buffer = ...request data as bytes
var webReq = (HttpWebRequest) WebRequest.Create("http://127.0.0.1/target");
webReq.Method = "REQUIRED METHOD";
webReq.ContentType = "REQUIRED CONTENT TYPE";
webReq.ContentLength = buffer.Length;
var reqStream = webReq.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
var webResp = (HttpWebResponse) webReq.GetResponse();
.NET 4.5 (or .NET 4.0 by adding the Microsoft.Net.Http package from NuGet) provides a lot of additional flexibility in setting the request content. .NET 4.5(或通过添加NuGet的Microsoft.Net.Http包提供.NET 4.0)在设置请求内容时提供了很多额外的灵活性。 Here is an example:
这是一个例子:
private System.IO.Stream Upload(string actionUrl, string paramString, Stream paramFileStream, byte [] paramFileBytes)
{
HttpContent stringContent = new StringContent(paramString);
HttpContent fileStreamContent = new StreamContent(paramFileStream);
HttpContent bytesContent = new ByteArrayContent(paramFileBytes);
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
formData.Add(stringContent, "param1", "param1");
formData.Add(fileStreamContent, "file1", "file1");
formData.Add(bytesContent, "file2", "file2");
var response = client.PostAsync(actionUrl, formData).Result;
if (!response.IsSuccessStatusCode)
{
return null;
}
return response.Content.ReadAsStreamAsync().Result;
}
}
Here's a different option for posting info without messing with Bytes and Streams. 这里有一个不同的选项,用于发布信息而不会弄乱Bytes和Streams。 I personally find it easier to follow, read, and debug.
我个人觉得更容易理解,阅读和调试。
// Convert Object to JSON
var requestMessage = JsonConvert.SerializeObject(requestObject);
var content = new StringContent(requestMessage, Encoding.UTF8, "application/json");
// Create the Client
var client = new HttpClient();
client.DefaultRequestHeaders.Add(AuthKey, AuthValue);
// Post the JSON
var responseMessage = client.PostAsync(requestEndPoint, content).Result;
var stringResult = responseMessage.Content.ReadAsStringAsync().Result;
// Convert JSON back to the Object
var responseObject = JsonConvert.DeserializeObject<ResponseObject>(stringResult);
HttpWebRequest's RequestStream is where the action is at - rough code... HttpWebRequest的RequestStream是动作所在的位置 - 粗略代码......
//build the request object
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(http://someapi.com/);
//write the input data (aka post) to a byte array
byte[] requestBytes = new ASCIIEncoding().GetBytes(inputData);
//get the request stream to write the post to
Stream requestStream = request.GetRequestStream();
//write the post to the request stream
requestStream.Write(requestBytes, 0, requestBytes.Length);
If you're sending extended chars, use UTF8Encoding, make sure you set the right content-type/charset header too. 如果您要发送扩展字符,请使用UTF8Encoding,请确保您也设置了正确的content-type / charset标头。
HttpWebRequest.GetRequestStream()
gets the request Stream. HttpWebRequest.GetRequestStream()
获取请求Stream。 After you have set the headers, use GetRequestStream()
and write the content to the stream. 设置标头后,使用
GetRequestStream()
并将内容写入流。
This post explains how to transmit files using HttpWebRequest
, which should provide a good example of how to send content. 这篇文章解释了如何使用
HttpWebRequest
传输文件,这应该提供一个如何发送内容的好例子。
But, basically the format would be 但是,基本上格式是
var stream = request.GetRequestStream();
stream.Write( stuff );
stream.Close();
var response = request.GetResponse();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.