5

What I'm trying to do:

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:

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

My understanding, from my learning:

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

My question:

  • How do I actually make that HTTP GET request, in ASP.NET MVC?
2
  • EDIT: I understand that there are many ways, so what is a 'simple, easy' way, to get started? Commented Apr 8, 2015 at 1:20
  • 1
    If you mean that you want to make HTTP calls from within your code, invoked by your server-side application, then note that this really doesn't have anything to do with MVC. Any .NET code would use objects like HttpClient to make HTTP requests and receive responses. MVC is the front-end application host for your code, but you're talking about back-end .NET code which can be invoked from any application host. Commented Apr 8, 2015 at 1:31

1 Answer 1

11

Use 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();
}

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Cheers. The main 'beef' for me were these lines: var response = await client.GetAsync("google.com"); string content = await response.Content.ReadAsStringAsync(); Console.WriteLine(content);
CS4033: "The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing it's return type to 'Task'." So, what does the method declaration surrounding this code look like, and what is the valid Task<T> value you use in the corresponding return statement? 🤔🤨
Also, client.GetAsync(uri) returns a value of type Task<HttpResponseMessage>, which has neither the Content nor IsSuccessStatusCode fields. Under VS2022/.Net-7.0, this 'answer' is literally not even valid code, let alone a 'solution'.
@NetXpert You should read up on how tasks work in C#. The await keyword automatically waits for the task to complete and unwraps the result. HttpResponseMessage has the properties you're looking for.
@TheVermilionWizard -- as I literally posted the exact error that this code generated in Visual Studio at the time of my post, I'm not sure what it is you're trying to rebut? As I asked two years ago: How do you encapsulate the given snippet within a method without having to mark it with the async modifier (and the required Task return type) such that you can then use the data that was returned by HttpClient, in the code which called that method?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.