Jer*_*rin 2 c# json asp.net-web-api windows-store-apps
由于不推荐使用webClient而且SharpRest不适用于Windows 8.1,我需要将我的json字符串传递给web api.
string js = @"[{""userName"":""jerin"",""userId"":""a""}]";
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();}
Run Code Online (Sandbox Code Playgroud)
现在这里baseaddress是一个包含url地址的字符串.但即使我将IsSuccessStatusCode设为true,也没有输入json.
我应该看到
Android团队在这个链接中输入的结果
是他们的参数值
usersJSON = [{ "的userName": "jerin", "用户id": "3"}]
根据@ Jon的回答,我编辑了我的代码而不是os System.Net.Http我现在正在使用Windows.Web.http,但它仍然没有上传
Run Code Online (Sandbox Code Playgroud)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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4923 次 |
| 最近记录: |