Search code examples
c#windows-10-universaldotnet-httpclientwindows-10-mobile

how can i post a json string along with some parameters in windows universal application c#


post a json value along with parameters in universal windows application

example:

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


Solution

  • we can create the parameters as dictionary and post it as 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;
    
        }