我想知道将小型json字符串/数据发送到Web服务的最佳方法。我可以使用WebClient或httpwebrequest这不是问题,但我主要关注如何将字符串转换为json格式并发布请求。
正如所建议的,这里我使用的是json.net,以下是代码:
Uri url = new Uri("http://example.com");
//Create the web request object
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
// Start the request
webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
//
JObject json =
new JObject(
new JProperty("customer", new JObject
(
new JProperty("phoneNumber", "07700555555"),
new JProperty("name", "John")
))
,
new JProperty("pickupAddress", new JObject
(
new JProperty("street", "1 Seagull Lane"),
new JProperty("city", "London"),
new JProperty("county", "London"),
new JProperty("postcode", "E000XX"),
new JProperty("country", "England"),
new JProperty("longitude", "10.18"),
new JProperty("latitude", "12.214")
))
,
new JProperty("destinationAddress", new JObject
(
new JProperty("county", "London"),
new JProperty("street", "1 Snow Lane"),
new JProperty("longitude", "1.79"),
new JProperty("latitude", "1.294"),
new JProperty("postcode", "E00XX"),
new JProperty("country", "England"),
new JProperty("city", "London")
))
,
new JProperty("pickupTime", "1311467121460"),
new JProperty("notes", "some notes"),
new JProperty("accountNumber", "account1"),
new JProperty("accountPassword", "account password")
);
//
// Create the post data
// Demo POST data
//byte[] byteArray = Encoding.UTF8.GetBytes(json.ToString());
//byte[] byteArray = Encoding.UTF8.GetBytes("1233456");
//
JsonSerializer serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Ignore;
using (StreamWriter sw = new StreamWriter(postStream))
using (JsonWriter writer = new JsonTextWriter(sw))
{
//serializer.Serialize(writer, JsonConvert.SerializeObject(json));
json.WriteTo(writer,null);
}
//
// Add the post data to the web request
//postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
// End the get response operation
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
var Response = streamReader.ReadToEnd();
MessageBox.Show(Response);
streamResponse.Close();
streamReader.Close();
response.Close();
}
catch (WebException e)
{
// Error treatment
// ...
}
}
现在代码似乎没问题,json数据创建正常,但服务器响应显示:“服务器响应e = {”远程服务器返回错误:NotFound。“}”
我不知道出了什么问题,请各位朋友帮忙!有人告诉我,我必须在请求的标题中添加一些内容,即标题应显示:booking = {... JSON ...}