我为UWP编写应用程序
我从后端收到json:
string url = "http://api.simplegames.com.ua/index.php/?wc_orders=all_orders";
{
string jsonString;
using (var httpClient = new System.Net.Http.HttpClient())
{
var stream = await httpClient.GetStreamAsync(url);
StreamReader reader = new StreamReader(stream);
jsonString = reader.ReadToEnd();
}
return jsonString;
}
我尝试像这样发送POST请求
OrdersList = new List<RootObject>(rootObjectData);
using (HttpClient httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(@"http://api.simplegames.com.ua");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("utf-8"));
string endpoint = @"/post_from_local.php";
try
{
HttpContent content = new StringContent(JsonConvert.SerializeObject(OrdersList), Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.PostAsync(endpoint, content);
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
Debug.WriteLine(jsonResponse);
//do something with json response here
}
}
catch (Exception)
{
//Could not connect to server
//Use more specific exception handling, this is just an example
}
}
但是,Back end dev说他看到空行,但没有收到数据。
感谢您的帮助。我的错误在哪里?
答案 0 :(得分:1)
更改此行:
var stream = await httpClient.GetStreamAsync(url);
到:
var stream = httpClient.GetStreamAsync(url).Result;