I want to know the best way to send a small json string/ data to a web service. I can use WebClient or httpwebrequest that's not the issue, but i am mainly concerned about how to convert the string into json format and post with the request.
As suggested , here i am using json.net , following is the code :
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 JPr开发者_如何转开发operty("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
// ...
}
}
Now the code seems to be ok , json data is created ok , but response from server is showing : "the server response e = {"The remote server returned an error: NotFound."}"
I don't know what's going wrong , please help friends ! Some one told me that i have to add something in header of the request i.e. The header should show: booking = {...JSON...}
You could use the built in DataContractJsonSerializer Class.
Alternatively, you may get better performance with json.net.
精彩评论