如何使HTTP调用,使用ASP.NET MVC? [英] How to make HTTP calls, using ASP.NET MVC?

查看:103
本文介绍了如何使HTTP调用,使用ASP.NET MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来编程,学习ASP.NET MVC 5的我找不到如何使HTTP调用/请求网站的教程中,也没有对计算器(或可能存在,但我不能找到它;搞不清的什么的搜索,但我都试过)。

I'm new to programming, and learning ASP.NET MVC 5. I can't find how to make HTTP calls/requests to websites in tutorials, nor on StackOverflow (or maybe it exists, but I can't find it; not quite sure what to search, though I have tried).

我试图做的事:

我试图实践使HTTP调用(......如果这是它叫什么)从一个简单的ASP.NET MVC Web应用程序。要做到这一点,我试图摆脱 OpenWeatherMap 的天气信息。你可以这样做:

I'm trying to practise making HTTP calls (...if that is what it's called) from a simple ASP.NET MVC web application. To do this, I am attempting to get weather details from OpenWeatherMap. You can do this by:


      
  • 添加以下参数GET请求:APPID = APIKEY
      

        
    • 例如: api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=1111111111

    •   
    • Add the following parameter to the GET request: APPID=APIKEY
      • Example: api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=1111111111

    我的理解,从我的学习:


    • 控制器的是一个使上述HTTP调用。

    • The controller is the one to make the above HTTP call.

    我的问题:


    • 如何居然提出,HTTP GET请求,在ASP.NET MVC?

    推荐答案

    使用的 System.Net.Http.HttpClient

    您可以使用类似以下从网站做一些基本的阅读:

    You can do some basic reading from a website using something like the following:

    using (var client = new HttpClient())
    {
        var uri = new Uri("http://www.google.com/");
    
        var response = await client.GetAsync(uri);
    
        string textResult = await response.Content.ReadAsStringAsync();
    }
    

    您可能希望确保测试 response.IsSuccessStatus code (检查一个HTTP 200结果),以确保其结果是你在你面前的期望解析它。

    You may want to make sure to test response.IsSuccessStatusCode (checks for an HTTP 200 result) to make sure the result is what you expect before you parse it.

    这篇关于如何使HTTP调用,使用ASP.NET MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆