【问题标题】:How i can connect http client我如何连接http客户端
【发布时间】:2018-10-24 23:09:27
【问题描述】:

我有两个项目 一个是api项目(web服务) 一个是消耗 api 项目的 xamarin 项目。Xamarin 项目可以像这样使用标准 api (https://api.coinmarketcap.com/v2/listings/) 但无法使用我的项目我收到错误“System.Net.Http.HttpRequestException:发送请求时发生错误”

这是我的 Http 客户端连接代码:

#HttpClient client = new HttpClient(); 
         client.DefaultRequestHeaders.Add("Accept", "application/json");
        return client;

谁能帮帮我?

【问题讨论】:

    标签: xamarin


    【解决方案1】:

    试试这个解决方案,使用 SharpProxy 从您的模拟器/设备调试您的 REST 服务 https://www.barelycompetent.co.za/debugging-your-rest-service-from-your-emulator-device-using-sharpproxy/

    【讨论】:

      【解决方案2】:

      在异步方法中使用以下代码,一旦运行,您应该在 response_string 中有 api 响应。

      System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
      var response = await client.SendAsync(new System.Net.Http.HttpRequestMessage { RequestUri = new Uri("https://api.coinmarketcap.com/v2/listings/") });
      var response_string = await response.Content.ReadAsStringAsync();
      

      【讨论】:

      • @Erkan,问题出在您的 api 中,您确定它可以正常工作并且可以被客户端应用程序访问。尝试使用 coinmarketcap api,就像我在上面的代码中那样,如果这有效意味着你应该修复你的 api。
      【解决方案3】:

      试试这个,告诉我它是否有效:)

      私有字符串 Url = "https://api.coinmarketcap.com/v2/listings/";

          public async Task<List<Todo>> GetTodos()
          {
              var httpClient = new HttpClient();
      
              var json = await httpClient.GetStringAsync(Url);
      
              var todoes = JsonConvert.DeserializeObject<List<Todo>>(json);
      
              return todoes;
          }
      

      【讨论】:

      猜你喜欢
      • 2015-11-22
      • 2023-04-10
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 2017-05-10
      相关资源
      最近更新 更多