无法从 Url C# Windows 8.1 下载字符串
Cannot Download String From An Url C# Windows 8.1
我是 C# 编程新手。
我想使用 openweathermap API 开发一个简单的天气应用程序。
我想从 URL.
下载并读取文件的内容
这是我下载文件内容的代码:
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;
}
传递给函数的字符串是城市的名称。
这是我调用这些函数的 MainPage 代码:
string JSONData;
private void GetWeatherButton_Click(object sender, RoutedEventArgs e)
{
WebClientToDownload Cls = new WebClientToDownload();
Cls.DownloadFile(GetWeatherText.Text);
JSONData = Cls.StringReturn();
JSONOutput.Text = JSONData;
}
我在代码的最后一行遇到错误
An exception of type 'System.ArgumentNullException' occurred in mscorlib.dll but was not handled in user code
Additional information: Value cannot be null.
看来是因为你使用了 await。基本上, await 会将控制权传回给调用函数并允许它继续,直到它被等待,这在你的情况下不会发生,所以它在返回数据之前调用 Cls.StringReturn() 。您可以更改如下:
在您的表单中:
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;
}
在您的下载中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();
}
}
我已经测试并返回了有效数据,但如果有任何不清楚的地方请告诉我。
我是 C# 编程新手。 我想使用 openweathermap API 开发一个简单的天气应用程序。 我想从 URL.
下载并读取文件的内容这是我下载文件内容的代码:
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;
}
传递给函数的字符串是城市的名称。 这是我调用这些函数的 MainPage 代码:
string JSONData;
private void GetWeatherButton_Click(object sender, RoutedEventArgs e)
{
WebClientToDownload Cls = new WebClientToDownload();
Cls.DownloadFile(GetWeatherText.Text);
JSONData = Cls.StringReturn();
JSONOutput.Text = JSONData;
}
我在代码的最后一行遇到错误
An exception of type 'System.ArgumentNullException' occurred in mscorlib.dll but was not handled in user code
Additional information: Value cannot be null.
看来是因为你使用了 await。基本上, await 会将控制权传回给调用函数并允许它继续,直到它被等待,这在你的情况下不会发生,所以它在返回数据之前调用 Cls.StringReturn() 。您可以更改如下:
在您的表单中:
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;
}
在您的下载中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();
}
}
我已经测试并返回了有效数据,但如果有任何不清楚的地方请告诉我。