如何在Windows通用应用程序中发布json字符串以及一些参数c#

时间:2017-10-15 07:55:21

标签: c# windows-10-universal dotnet-httpclient windows-10-mobile

在通用窗口应用程序中发布json值和参数

示例:

http://sampleurl?param=1&param2=1 json body:[{" key":" value"," key2":" value2"}]

1 个答案:

答案 0 :(得分:0)

我们可以将参数创建为字典并将其发布为formcontent

  public static async Task<string> postdataAsync(Uri posturl, Dictionary<string, string> pairs)
    {
        string response = "";
        try
        {
            var filter = new HttpBaseProtocolFilter();
            filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
            filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
            filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);
            HttpClient httpClient = new HttpClient(filter);
            Windows.Web.Http.HttpFormUrlEncodedContent formContent = new Windows.Web.Http.HttpFormUrlEncodedContent(pairs);
            HttpResponseMessage httpresponse = await httpClient.PostAsync(posturl, formContent);
            response = await httpresponse.Content.ReadAsStringAsync();
        }
        catch
        {
            return null;

        }
        return response;

    }