Post Json字符串到Web Api Windows Store 8.1
本文关键字:Windows Store Api Web Json 字符串 Post | 更新日期: 2023-09-27 18:17:00
与webClient弃用和SharpRest不工作的Windows 8.1我需要传递我的json字符串到web api
字符串js = @"{"用户名":"jerin","标识":"一"}]";
var baseAddress = " http://epub3.in/sample/android%20webservice/webservice/insertuser.php/",
HttpClient httpClient = new HttpClient();
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(baseAddress);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
HttpResponseMessage response = httpClient.PostAsync("",
new StringContent(
js,
Encoding.UTF8,
"application/json")).Result;
string sd = response.IsSuccessStatusCode.ToString();}
现在baseaddress是一个字符串,它有url地址。但即使我得到IsSuccessStatusCode为真json没有被输入。
我应该看到输入的结果在这个链接
Android团队发布的参数值为
usersJSON =[{"用户名":"jerin"、"用户名":"3"}]
根据@Jon的回答,我编辑了我的代码,而不是System.Net.Http,我现在使用Windows.Web.http,但它仍然没有上传
Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient(); Windows.Web.Http.HttpRequestMessage msg = new Windows.Web.Http.HttpRequestMessage(new Windows.Web.Http.HttpMethod("POST"), new Uri(baseAddress)); msg.Content = new HttpStringContent((js)); msg.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/json"); Windows.Web.Http.HttpResponseMessage response = await httpClient.SendRequestAsync(msg).AsTask();
尝试从Windows.Web.Http命名空间使用HttpClient:
HttpClient httpClient = new HttpClient();
HttpRequestMessage msg = new HttpRequestMessage(new HttpMethod("POST"), new Uri(baseAddress));
msg.Content = new HttpStringContent(js);
msg.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/json");
HttpResponseMessage response = await _httpClient.SendRequestAsync(msg).AsTask();