Cannot Download String From An Url C# Windows 8.1 - c#

I am new To C# programming.
I want to develop a simple Weather App using openweathermap API's.
I want to download and read contents of a file from an URL.
This is my Code to download file contents:
class WebClientToDownload
{
string webresponse;
public async void DownloadFile(string url)
{
string baseurl = "http://api.openweathermap.org/data/2.5/forecast/daily?q=";
StringBuilder sb = new StringBuilder(baseurl);
sb.Append(url + "&mode=json&units=metric&cnt=7");
string actual = sb.ToString();
HttpClient http = new System.Net.Http.HttpClient();
HttpResponseMessage response = await http.GetAsync(actual);
webresponse = await response.Content.ReadAsStringAsync();
}
public string StringReturn()
{
return webresponse;
}
string passed to the function is the name of city.
This is MainPage Code where I call those functions:
string JSONData;
private void GetWeatherButton_Click(object sender, RoutedEventArgs e)
{
WebClientToDownload Cls = new WebClientToDownload();
Cls.DownloadFile(GetWeatherText.Text);
JSONData = Cls.StringReturn();
JSONOutput.Text = JSONData;
}
I am getting an error at last line of code says as
An exception of type 'System.ArgumentNullException' occurred in mscorlib.dll but was not handled in user code
Additional information: Value cannot be null.

It looks like it was down to your use of await. Basically, await will pass control back to the calling function and allow it to continue until it is awaited, which isn't happening in your case so it is calling Cls.StringReturn() before the data has been returned. You can change as follows:
In your form:
string JSONData;
// Note the async keyword in the method declaration.
private async void GetWeatherButton_Click(object sender, EventArgs e)
{
WebClientToDownload Cls = new WebClientToDownload();
// Notice the await keyword here which pauses the execution of this method until the data is returned.
JSONData = await Cls.DownloadFile(GetWeatherText.Text);
JSONOutput.Text = JSONData;
}
In your download class:
class WebClientToDownload
{
// Notice this now returns a Task<string>. This will allow you to await on the data being returned.
public async Task<string> DownloadFile(string url)
{
string baseurl = "http://api.openweathermap.org/data/2.5/forecast/daily?q=";
StringBuilder sb = new StringBuilder(baseurl);
sb.Append(url + "&mode=json&units=metric&cnt=7");
string actual = sb.ToString();
HttpClient http = new System.Net.Http.HttpClient();
HttpResponseMessage response = await http.GetAsync(actual);
// Return the result rather than setting a variable.
return await response.Content.ReadAsStringAsync();
}
}
I've tested and it returned valid data but if any of this isn't clear please let me know.

Related

HttpClient GetAsync not working as expected

When testing my web API with Postman my API get executes fine!
When it comes to running the code with HttpClient in my client application the code executes without error but without the expected result on the server.
What could be happening?
From my client application:
private string GetResponseFromURI(Uri u)
{
var response = "";
HttpResponseMessage result;
using (var client = new HttpClient())
{
Task task = Task.Run(async () =>
{
result = await client.GetAsync(u);
if (result.IsSuccessStatusCode)
{
response = await result.Content.ReadAsStringAsync();
}
});
task.Wait();
}
return response;
}
Here is the API controller:
[Route("api/[controller]")]
public class CartsController : Controller
{
private readonly ICartRepository _cartRepo;
public CartsController(ICartRepository cartRepo)
{
_cartRepo = cartRepo;
}
[HttpGet]
public string GetTodays()
{
return _cartRepo.GetTodaysCarts();
}
[HttpGet]
[Route("Add")]
public string GetIncrement()
{
var cart = new CountedCarts();
_cartRepo.Add(cart);
return _cartRepo.GetTodaysCarts();
}
[HttpGet]
[Route("Remove")]
public string GetDecrement()
{
_cartRepo.RemoveLast();
return _cartRepo.GetTodaysCarts();
}
}
Note these API calls work as expected when called from Postman.
You shouldn't use await with client.GetAsync, It's managed by .Net platform, because you can only send one request at the time.
just use it like this
var response = client.GetAsync("URL").Result; // Blocking call!
if (response.IsSuccessStatusCode)
{
// Parse the response body. Blocking!
var dataObjects = response.Content.ReadAsAsync<object>().Result;
}
else
{
var result = $"{(int)response.StatusCode} ({response.ReasonPhrase})";
// logger.WriteEntry(result, EventLogEntryType.Error, 40);
}
You are doing fire-and-forget approach. In your case, you need to wait for the result.
For example,
static async Task<string> GetResponseFromURI(Uri u)
{
var response = "";
using (var client = new HttpClient())
{
HttpResponseMessage result = await client.GetAsync(u);
if (result.IsSuccessStatusCode)
{
response = await result.Content.ReadAsStringAsync();
}
}
return response;
}
static void Main(string[] args)
{
var t = Task.Run(() => GetResponseFromURI(new Uri("http://www.google.com")));
t.Wait();
Console.WriteLine(t.Result);
Console.ReadLine();
}
Simple sample used to get page data.
public string GetPage(string url)
{
HttpResponseMessage response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
string page = response.Content.ReadAsStringAsync().Result;
return "Successfully load page";
}
else
{
return "Invalid Page url requested";
}
}
I've had a problem with chace control when using httpclient.
HttpBaseProtocalFilter^ filter = ref new HttpBaseProtocolFilter();
filter->CacheControl->ReadBehavior = Windows::Web::Http::Filters::HttpCacheReadBehavior::MostRecent;
HttpClient^ httpClient = ref new HttpClient(filter);
I'm not really sure what the expected results are or what results your getting at all so this is really just a guessing game right now.
When I POST something using HttpClient I found adding headers by hand seemed to work more often than using default headers.
auto httpClient = ref new HttpClient();
Windows::Web::Http::Headers::HttpMediaTypeHeaderValue^ type = ref new Windows::Web::http::Headers::HttpMediaTypeHeaderValue("application/json");
content->Headers->ContentType = type;
If I don't do these 2 things I found, for me anyways, that half the time my web requests were either not actually being sent or the headers were all messed up and the other half of the time it worked perfectly.
I just read a comment where you said it would only fire once, that makes me think it is the cachecontrol. I think what happens is something (Windows?) sees 2 requests being sent that are the exact same, so to speed things up it just assumes the same answer and never actually sends the request a 2nd time

How can I get JSON data from Google API and store it in a variable?

I am trying to send an Http Get message to the Google location Api which is supposed to have Json data such as this
https://maps.googleapis.com/maps/api/geocode/json?address=Los%20Angeles,CA=AIzaSyDABt
as you noticed the response is in Json. I want to give a a Http Call to that URL and save the Json content in a variable or string. My code does not give out any errors but it is also not returning anything
public async System.Threading.Tasks.Task<ActionResult> GetRequest()
{
var client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("https://maps.googleapis.com/maps/api/geocode/json?address=Los%20Angeles,CA=AIzaSyDABt");
string data = response.Content.ToString();
return data;
}
I want to send out a Get Request using HttpClient() or anything that will send out the URL request and then save that content into a string variable . Any suggestions would be appreciated, again my code gives no errors but it is not returning anything.
Use ReadAsStringAsync to get the json response...
static void Main(string[] args)
{
HttpClient client = new HttpClient();
Task.Run(async () =>
{
HttpResponseMessage response = await client.GetAsync("https://maps.googleapis.com/maps/api/geocode/json?address=Los%20Angeles,CA=AIzaSyDABt");
string responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
});
Console.ReadLine();
}
If you use response.Content.ToString() it is actually converting the datatype of the Content to string so you will get System.Net.Http.StreamContent
Try this.
var client = new HttpClient();
HttpResponseMessage httpResponse = await client.GetAsync("https://maps.googleapis.com/maps/api/geocode/json?address=Los%20Angeles,CA=AIzaSyDABt");
string data = await httpResponse.Content.ReadAsStringAsync();
How about using the Json framework for .NET powered by Newtonsoft ?
You can try to parse your content to a string with this.
Well, your code can be simplified:
public async Task<ActionResult> GetRequest()
{
var client = new HttpClient();
return await client.GetStringAsync("https://maps.googleapis.com/maps/api/geocode/json?address=Los%20Angeles,CA=AIzaSyDABt");
}
However...
My code does not give out any errors but it is also not returning anything
This is almost certainly due to using Result or Wait further up the call stack. Blocking on asynchronous code like that causes a deadlock that I explain in full on my blog. The short version is that there is an ASP.NET request context that only permits one thread at a time; await by default will capture the current context and resume in that context; and since Wait/Result is blocking a thread in the request context, the await cannot resume executing.
Try this
public async static Task<string> Something()
{
var http = new HttpClient();
var url = "https://maps.googleapis.com/maps/api/geocode/json?address=Los%20Angeles,CA=AIzaSyDABt";
var response = await http.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<string>(result);
return result;
}
return "";
}
var result = Task.Run(() => Something()).Result;

Emulator hangs with Task status WaitingForActivation

I am trying to fetch data from an API.
When I write all code in one method like as follows it work fine.
private async void btvalidate_Click(object sender, RoutedEventArgs e)
{
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("mybaseaddress");
HttpResponseMessage response = await client.GetAsync("mylocaluri");
if (response.IsSuccessStatusCode)// check whether response status is true
{
var data = response.Content.ReadAsStringAsync();//read the data in the response
var msg = JsonConvert.DeserializeObject<myclassname>(data.Result.ToString());//convert the string response in json format
validate.DataContext = msg;// assign the data received to stackpanel
}
}
catch (Exception ex)
{
MessageBox.Show("Somethimng went wrong" + ex);
}
}
But when I try to write this code in a method of a separate class and call it from a click event as follows, it hangs on click of event and has status of data as WaitingforActivation...
public class API
{
public async Task<string> getAPI(string uri)
{
string data1 = null;
var data=data1;
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("mybaseaddress");
HttpResponseMessage response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)// check whether response status is true
{
data = response.Content.ReadAsStringAsync().Result;//read the data in the response
}
}
return data;
}
}
private void btcount_Click(object sender, RoutedEventArgs e)
{
var data = api.getAPI("mylocaluri");
var msg = JsonConvert.DeserializeObject<myclassname>(data.Result.ToString());//convert the string response in json format
validate.DataContext = msg;// assign the data received to stackpanel
}
Can someone tell me what I'm doing wrong?
Thanks for help in advance.
You're causing a deadlock by calling Result, which I explain in full on my blog.
The best solution is to use async "all the way", as I describe in my MSDN article on async best practices.
In this particular case, replace Result with await:
private async void btcount_Click(object sender, RoutedEventArgs e)
{
var data = await api.getAPI("mylocaluri");
var msg = JsonConvert.DeserializeObject<myclassname>(data.ToString());//convert the string response in json format
validate.DataContext = msg;// assign the data received to stackpanel
}
On a side note, consider renaming getAPI to GetApiAsync, to follow common naming patterns.

await GetByteArrayAsync in asp.net not returning

The following code just hangs when called from an ASP.NET app:
private async Task<XPathNavigator> UspsCreateAndPostRequest(string sUrl)
{
HttpClient client = new HttpClient();
byte[] urlContents = await client.GetByteArrayAsync(sUrl);
string sResponse = System.Text.Encoding.UTF8.GetString(urlContents);
... //more code to return XPathNavigator object based on response
}
If I change to following it works fine:
private async Task<XPathNavigator> UspsCreateAndPostRequest(string sUrl)
{
HttpClient client = new HttpClient();
byte[] urlContents = null;
var task = Task.Run(async () => { urlContents = await client.GetByteArrayAsync(strUrl); });
task.Wait();
string sResponse = System.Text.Encoding.UTF8.GetString(urlContents);
... //more code to return XPathNavigator object based on response
}
Is the fact that method signature return is a Task<XPathNavigator> causing the issue? Thank you.
Somewhere higher up the call stack there is a .Wait() being performed on the task that is returned from UspsCreateAndPostRequest.
Because you wrapped the call inside a Task.Run you lost the execution context, that is why it works. Doing
private async Task<XPathNavigator> UspsCreateAndPostRequest(string sUrl)
{
HttpClient client = new HttpClient();
byte[] urlContents = await client.GetByteArrayAsync(sUrl).ConfigureAwait(false);
string sResponse = System.Text.Encoding.UTF8.GetString(urlContents);
... //more code to return XPathNavigator object based on response
}
would achieve the same goal with less resources (but it would be even better to fix the wait higher up on the chain).

How to handle data from httpclient

I'm working on a new Windows Phone 8 app. I'm connecting to a webservice which returns valid json data. I'm using longlistselector to display the data. This works fine when i'm using the string json in GetAccountList(); but when receiving data from the DataServices class i'm getting the error "Cannot implicitly convert type 'System.Threading.Tasks.Task'to string". Don't know what goes wrong. Any help is welcome. Thanks!
DataServices.cs
public async static Task<string> GetRequest(string url)
{
HttpClient httpClient = new HttpClient();
await Task.Delay(250);
HttpResponseMessage response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Debug.WriteLine(responseBody);
return await Task.Run(() => responseBody);
}
AccountViewModel.cs
public static List<AccountModel> GetAccountList()
{
string json = DataService.GetRequest(url);
//string json = #"{'accounts': [{'id': 1,'created': '2013-10-03T16:17:13+0200','name': 'account1 - test'},{'id': 2,'created': '2013-10-03T16:18:08+0200','name': 'account2'},{'id': 3,'created': '2013-10-04T13:23:23+0200','name': 'account3'}]}";
List<AccountModel> accountList = new List<AccountModel>();
var deserialized = JsonConvert.DeserializeObject<IDictionary<string, JArray>>(json);
JArray recordList = deserialized["accounts"];
foreach (JObject record in recordList)
{
accountList.Add(new AccountModel(record["name"].ToString(), record["id"].ToString()));
}
return accountList;
}
UPDATE: I changed it slightly and works like a charm now. Thanks for your help!
DataServices.cs
//GET REQUEST
public async static Task<string> GetAsync(string url)
{
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(url);
string content = await response.Content.ReadAsStringAsync();
return content;
}
AccountViewModel.cs
public async void LoadData()
{
this.Json = await DataService.GetAsync(url);
this.Accounts = GetAccounts(Json);
this.AccountList = GetAccountList(Accounts);
this.IsDataLoaded = true;
}
public static IList<AccountModel> GetAccounts(string json)
{
dynamic context = JObject.Parse(json);
JArray deserialized = (JArray)JsonConvert.DeserializeObject(context.results.ToString());
IList<AccountModel> accounts = deserialized.ToObject<IList<AccountModel>>();
return accounts;
}
public static List<AlphaKeyGroup<AccountModel>> GetAccountList(IList<AccountModel> Accounts)
{
List<AlphaKeyGroup<AccountModel>> accountList = AlphaKeyGroup<AccountModel>.CreateGroups(Accounts,
System.Threading.Thread.CurrentThread.CurrentUICulture,
(AccountModel s) => { return s.Name; }, true);
return accountList;
}
That line is your problem:
return await Task.Run(() => responseBody);
Did you try that? :
return responseBody;
Try this too:
public async static List<AccountModel> GetAccountList()
{
string json = await DataService.GetRequest(url);
...
}
A few things here. First the error
Cannot implicitly convert type 'System.Threading.Tasks.Task' to string
This error is coming from the call to DataService.GetRequest(url). This method does return a string. Tt returns a Task where T is a string. There are many ways that you can use the result of this method. the first (and best/newest) is to await the call to the method.
string json = await DataService.GetResult(url);
Making this change requires you to add the async keyboard to your method
public async static List<AccountModel> GetAccountList()
This is the new async/await pattern. Adding these words tells the compiler that the method cal is asynchronous. It allows you to make asynchronous calls but write code as if it is synchronous.
The other ways to call the method are to work the Task object directly.
// First is to use the Result property of the Task
// This is not recommended as it makes the call synchronous, and ties up the UI thread
string json = DataService.GetResult(url).Result;
// Next is to continue work after the Task completes.
DataService.GetResult(url).ContinueWith(t =>
{
string json = t.Result;
// other code here.
};
Now for the GetResult method. Using the async/await pattern requires you to return Task from methods. Even though the return type is Task, your code should return T. So as Krekkon mentioned, you should change the return line to
return responseBody;
Here is a great article about returning Task from an async method.

Categories

Resources