我正在编写 Windows Phone 8.1应用程序(WINRT)。我创建了一个登录页面,想要将登录数据发布到服务器并获得响应。
感谢@Kai Brummund给了我以下方法,但它使用了 System.Net.Http ;但我想使用强烈推荐的 Windows.Web.Http.HttpClient
使用新的命名空间,它在这个方法中无处不在给我错误。我需要做出哪些改变?
CancellationTokenSource cancellationToken;
public async void ReadHeaders()
{
cancellationToken = new CancellationTokenSource();
// Create the message
string address = singletonInstance.APIServer + "MkhAdapter/rest/mkh/Login";
var message = new HttpRequestMessage(HttpMethod.Post, new Uri(address));
// Add the message body
message.Content = new StringContent(
LoginJsonData,
System.Text.UTF8Encoding.UTF8, "application/json");
////filter/compress
//HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
// Send
var client = new HttpClient();
var result = await client.SendAsync(message, cancellationToken.Token);
result.EnsureSuccessStatusCode();
// Get ids from result
var finalresult = await result.Content.ReadAsStringAsync();
}
答案 0 :(得分:1)
基本上你想做一个Http Post请求,对吧? 您可以使用WebClient:
// Create the message
var message = new HttpRequestMessage(HttpMethod.Post, new Uri(singletonInstance.APIServer + "MkhAdapter/rest/mkh/Login"));
// Add the message body
message.Content = new StringContent(
"{'UserCategory': 'user','Email': User_EnteredUsername, 'Password':User_EnteredPassword}",
System.Text.UTF8Encoding.UTF8, "application/json");
// Send
var client = new HttpClient();
var result = await client.SendAsync(message, cancellationToken);
result.EnsureSuccessStatusCode();
// Get ids from result
return (await result.Content.ReadAsStringAsync());