我有:
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(UserTask));
s.WriteObject(stream1, task);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
Trace.Write("JSON form of Person object: ");
Trace.WriteLine(sr.ReadToEnd());
string json = sr.ReadToEnd();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(json);
logger.Write("Attempting to post the foillowing JSON data: " + json);
Trace.WriteLine("Attempting to post the foillowing JSON data: " + json);
// Make a post to the other service
HttpWebRequest httpWReq =
(HttpWebRequest)WebRequest.Create(@"https://some.company.url");
//(HttpWebRequest)WebRequest.Create(@"https://some.company.url");
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = null;
string responseString = null;
try
{
// Do some work that may result in a transient fault.
retryPolicy.ExecuteAction(
() =>
{
// Call a method that uses Windows Azure storage and which may
// throw a transient exception.
response = (HttpWebResponse)httpWReq.GetResponse();
responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
});
}
这实际上并不是通过网络将 JSON 发送到其他服务......我正在使用 DataContractJsonSerializer 因为数据包含字典。如果有用的话,我可以发布数据的样子,但我认为那部分有效吗?