I am having a bit of an issue in using WebClient on Windows Phone 7. I am currently trying to use it by downloading a file into a string so that I can parse the string using JSON.NET. Unfortunately, I am can't even seem to get the file into a string just yet. Here is my code:
private void GetFileAsString()
{
var client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(sClient_DownloadStringCompleted);
client.DownloadStringAsync(new Uri(searchData.searchurl, UriKind.Relative));
}
private void sClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
textBlock1.Text = e.Result;
}
else
{
textBlock1.Text = e.Error.ToString();
}
}
And here is a screenshot of the error I am receiving: http://k.min.us/jzvIAYJ18uQbV.png
I've looked online quite a bit and can't seem to find a solution to this problem, including in previous p开发者_如何学运维osts on this site. Any help here is greatly appreciated!
You need to setup BaseAddress
property on your WebClient
instance correctly when you use UriKind.Relative
, alternatively just use absolute Uri
- otherwise it tries to use your xap's origin Uri
...
MSDN documentation suggests two possible issues you could be running into. You haven't given us enough info to say for sure.
http://msdn.microsoft.com/en-us/library/ms144202(v=VS.95).aspx
This method retrieves the specified resource using the GET method. The resource is downloaded asynchronously. When the download is completed, the DownloadStringCompleted event is raised. The downloaded string is available in the Result property of the DownloadStringCompletedEventArgs. You cannot call the DownloadStringAsync method again on the same WebClient object, until the first string download operation is completed. Doing this causes an exception.
If the BaseAddress property is not an empty string and the address does not contain an absolute URI, the address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data.
精彩评论