邮政JSON字符串到网页API Windows应用商店8.1 [英] Post Json string to Web Api Windows Store 8.1
问题描述
使用Web客户端去precated和莎尔preST不适用于Windows 8.1的工作,我需要我的JSON字符串传递到Web API。
字符串JS = @[{用户名:jerin,用户id:一}];
VAR baseAddress =http://epub3.in/sample/android%20webservice/webservice/insertuser.php/;
块引用>的HttpClient HttpClient的=新的HttpClient();
HttpClient的HttpClient的=新的HttpClient();
httpClient.BaseAddress =新的URI(baseAddress);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(新MediaTypeWithQualityHeaderValue(应用/ JSON));
尝试
{
HTT presponseMessage响应= httpClient.PostAsync(,
新的StringContent(
JS,
Encoding.UTF8,
应用/ JSON))结果。
串SD = response.IsSuccessStatus code.ToString();}现在这里baseaddress是具有URL地址字符串。但是,即使我得到IsSuccessStatus code为真没有被输入了JSON。
而我应该看到在这个 链接 输入的结果STRONG>
Android团队正在张贴,他们的PARAMS值为
usersJSON = [{username的:jerin,用户id:3}]
块引用>据@乔恩的答案我编辑我的code,其中我现在用Windows.Web.http而是OS System.Net.Http它仍然没有上传
Windows.Web.Http.HttpClient的HttpClient =新Windows.Web.Http.HttpClient();
Windows.Web.Http.Htt prequestMessage味精=新Windows.Web.Http.Htt prequestMessage(新Windows.Web.Http.HttpMethod(POST),新的URI(baseAddress));
msg.Content =新HttpStringContent((JS));
msg.Content.Headers.ContentType =新HttpMediaTypeHeaderValue(应用/ JSON);
Windows.Web.Http.Htt presponseMessage响应=等待httpClient.SendRequestAsync(MSG).AsTask();
块引用>解决方案尝试使用的HttpClient从Windows.Web.Http命名空间:
的HttpClient HttpClient的=新的HttpClient();
HTT prequestMessage味精=新HTT prequestMessage(新列举HTTPMethod(POST),新的URI(baseAddress));
msg.Content =新HttpStringContent(JS);
msg.Content.Headers.ContentType =新HttpMediaTypeHeaderValue(应用/ JSON);
HTT presponseMessage响应=等待_httpClient.SendRequestAsync(MSG).AsTask();With webClient deprecated and SharpRest not working for Windows 8.1 I need to pass my json string to a 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();}
Now here baseaddress is a string which has url address. But even though I am getting IsSuccessStatusCode as true the json is not being entered.
and I am supposed to see the entered result in this link
Android team is posting where their params value isusersJSON=[{"userName":"jerin","userId":"3"}]
According to @Jon's answer I edited my code where instead os System.Net.Http I am now using Windows.Web.http but its still not uploading
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();
解决方案Try using the HttpClient from the Windows.Web.Http namespace:
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();
这篇关于邮政JSON字符串到网页API Windows应用商店8.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!