本文介绍了如何使用 Windows Store 应用程序中的 json 网络服务向 HTTP 标头发送请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Windows 商店应用程序,我在其中使用了一个网络服务,该服务具有用于下载下面给出的视频的参数.

I'm working on a windows store app where I am using a web service which has parameters for downloading videos that are given below.

[request addValue:Token Id forHTTPHeaderField:@"Authorization"];

通过 log_in Web 服务,我获得了访问令牌,我必须将其作为授权中的值传递给 HTTP 标头.

Through log_in web services I get the access token which I have to pass as value in the Authorization a request to the HTTP Header.

Token token="hgmgmhmhgm6dfgffdbfetgjhgkj4mhh8dghmge"

我必须使用提供给我的 Web 服务发送这两个参数,但我无法发送它们,因为我收到错误状态代码 404 未授权.

I have to send both these parameters with the web service given to me but I am unable to send them as I'm getting the error status code 404 unauthorized.

这是我的代码:

 System.Net.Http.HttpClient httpClient1 = new System.Net.Http.HttpClient();
        httpClient1.DefaultRequestHeaders.Date = DateTime.Now;
        httpClient1.DefaultRequestHeaders.Add("Authorization",acesstoken);
     var httpResponse1 = await httpClient.GetAsync("http://[email protected]/appi/fdbfdses/3/videos");
        string vale = await httpResponse1.Content.ReadAsStringAsync();
        string responses = vale;

我知道我的代码是错误的,我需要更正它.帮我提出宝贵的建议.

I know my code is wrong and I need to correct it. Help me with your valuable suggestions.

推荐答案

试试这个代码

using (var client = new HttpClient())
  {
      try
      {

          String url = "https://www.example-http-request.com/json";

          var httpClient = new HttpClient(new HttpClientHandler());
          var values = new List<KeyValuePair<string, string>>
          {
              new KeyValuePair<string, string>("Authorization", acesstoken)
          };
          HttpResponseMessage response = await httpClient.PostAsync(new Uri(url), new FormUrlEncodedContent(values));
          response.EnsureSuccessStatusCode();
          var responsesStr = await response.Content.ReadAsStringAsync();
          if (!responsesStr.Equals(""))
          {

              //http request data now capture in responseToken string.
              //you can change name as per your requirement.

              String responseOutput = responsesStr;


              //now convert/parse your json using Newtonsoft JSON library
              // Newtonsoft JSON Librar link : { http://james.newtonking.com/json }

                 //LoggingModel is a class which is the model of deserializing your json output.
                 LoggingModel array = JsonConvert.DeserializeObject<LoggingModel>(responseOutput);

                  bool isSuccess = array.IsSuccessful;

                  if (isSuccess == true)
                  {
                      //if success

                  }else{

                      //if login failed

                  }


            }else{

                  //no response in http request failed.
            }
      }
      catch (Exception ex)
      {
          //catch exceptio here
      }
}

这篇关于如何使用 Windows Store 应用程序中的 json 网络服务向 HTTP 标头发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:15