我想向服务器发送http POST
请求。此请求包含标头,发布json
对象并获取响应。我正在使用此代码:
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Accept = "application/json";
httpWebRequest.Method = "POST";
using (var stream = await Task.Factory.FromAsync<Stream> (httpWebRequest.BeginGetRequestStream, httpWebRequest.EndGetRequestStream, null)){
byte[] jsonAsBytes = Encoding.UTF8.GetBytes(jsonString);
await stream.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length);
}
我认为我成功发布了我的请求,但我不知道如何获取响应字符串。
答案 0 :(得分:1)
请尝试此代码示例。 收到的字符串应该是响应。
String received = null;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] requestBody = Encoding.UTF8.GetBytes(postData);
// ASYNC: using awaitable wrapper to get request stream
using (var postStream = await request.GetRequestStreamAsync())
{
// Write to the request stream.
// ASYNC: writing to the POST stream can be slow
await postStream.WriteAsync(requestBody, 0, requestBody.Length);
}
try
{
// ASYNC: using awaitable wrapper to get response
var response = (HttpWebResponse)await request.GetResponseAsync();
if (response != null)
{
var reader = new StreamReader(response.GetResponseStream());
// ASYNC: using StreamReader's async method to read to end, in case
// the stream i slarge.
received = await reader.ReadToEndAsync();
}
}
catch (WebException we)
{
var reader = new StreamReader(we.Response.GetResponseStream());
string responseString = reader.ReadToEnd();
Debug.WriteLine(responseString);
return responseString;
}
return received;
答案 1 :(得分:0)
您可以使用HttpWebResponse
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse()
例如:
//Get Response from server
using (HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse())
{
StreamReader myStream = new StreamReader(myHttpWebResponse.GetResponseStream());
string resultJson = myStream.ReadToEnd();
myStream.Close();
}
然后,您还可以使用序列化API通过以下方式序列化对象中的json:
using System.Runtime.Serialization.Json;
例如:
//Create new instance of JsonObject
MySerializeObject obj = new MySerializeObject();
//Get the byte
byte[] byteArray = Encoding.UTF8.GetBytes(resultJson);
//Create Memorystream from the byteArray
MemoryStream myMemoryStream = new MemoryStream(byteArray);
//Create Json DataContract from serialize object
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(MySerializeObject));
//Read Stream in object
myMemoryStream.Position = 0;
obj = (MySerializeObject)ser.ReadObject(myMemoryStream);
//Close the stream
myMemoryStream.Close();
答案 2 :(得分:0)
我解决了这个问题
HttpClient Client= new HttpClient();
Client.DefaultRequestHeaders.Add("accept", "Application/JSON");
//Add the content body (which is a json object)
HttpContent content = new StringContent(jsonString);
//Add the header
content.Headers.TryAddWithoutValidation("Content-Type", "application/json");
HttpResponseMessage response = await Client.PostAsync(new Uri(string), content);
response.EnsureSuccessStatusCode();
string ch = await response.Content.ReadAsStringAsync();
答案 3 :(得分:0)
示例代码:
HttpClient httpClient = new HttpClient();
HttpResponseMessage wcfResponse = await httpClient.PostAsync(new Uri(url), new StringContent(json, Encoding.UTF8, "application/json"));
string result = await wcfResponse.Content.ReadAsStringAsync();
dynamic data = JObject.Parse(result);
var item = data.element;