我尝试使用 Newtonsoft.Json 序列化Web请求对象,但序列化对象但是标题为空。任何人都可以帮我找到方法
WebRequest WebRequest = WebRequest.Create("http://test.com/test");
WebRequest.Method = "Post";
var postData = "thing1=hello";
postData += "&thing2=world";
var data = Encoding.ASCII.GetBytes(postData);
WebRequest.Headers.Add("MyTestHeader", "My Test Header Value");
WebRequest.Method = "POST";
WebRequest.ContentType = "application/x-www-form-urlencoded";
WebRequest.ContentLength = data.Length;
Stream dataStream = WebRequest.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(data, 0, data.Length);
// Close the Stream object.
dataStream.Close();
Console.Writeline(JsonConvert.SerializeObject(WebRequest));
Json结果:
{
"_HttpRequestHeaders": [
"MyTestHeader",
"Content-Type",
"Host",
"Content-Length",
"Expect",
"Connection"
],
"_Proxy": {
"Credentials": null
},
"_KeepAlive": true,
"_Pipelined": true,
"_AllowAutoRedirect": true,
"_AllowWriteStreamBuffering": true,
"_HttpWriteMode": 1,
"_MaximumAllowedRedirections": 50,
"_AutoRedirects": 0,
"_Timeout": 100000,
"_ReadWriteTimeout": 300000,
"_MaximumResponseHeadersLength": 64,
"_ContentLength": 25,
"_MediaType": null,
"_OriginVerb": {},
"_ConnectionGroupName": null,
"_Version": {
"Major": 1,
"Minor": 1,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"_OriginUri": "http://test.com/test"
}
提前致谢。我希望序列化请求而不会丢失任何信息,例如:如果您要发送一些数据传递给该请求,我想在我的json中保留该信息。