gpt4 book ai didi

c# - 将 Json 字符串发布到 Web Api Windows Store 8.1

转载 作者:行者123 更新时间:2023-11-30 20:25:02 25 4
gpt4 key购买 nike

随着 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();}

这里的 baseaddress 是一个包含 url 地址的字符串。但是即使我将 IsSuccessStatusCode 设置为 true,也没有输入 json。
我应该在这个 link 中看到输入的结果
Android 团队正在发布其参数值所在的位置

usersJSON=[{"userName":"jerin","userId":"3"}]

根据@Jon 的回答,我编辑了我的代码,而不是 os 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();

关于c# - 将 Json 字符串发布到 Web Api Windows Store 8.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25183830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com