我使用httpwebrequest将其他网站的内容导入我的Windows Phone应用程序,如下所示 . 我想获取页面的html,然后解析它以获得我想要的数据 .
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Uri targetUri = new System.Uri("http://www.contoso.com");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
}
private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse myResponse=(HttpWebResponse)myRequest.EndGetResponse(callbackResult);// Exception is showing here.
using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
string results = httpwebStreamReader.ReadToEnd();
//TextBlockResults.Text = results; //-- on another thread!
Dispatcher.BeginInvoke(() => TextBlockResults.Text = results);
}
myResponse.Close();
}
但是得到以下错误:
{System.Net.WebException:远程服务器返回错误:NotFound . ---> System.Net.WebException:远程服务器返回错误:NotFound . 在System.Net.Browser.ClientHttpWebRequest上的System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) . <> c_DisplayClasse.b_d(Object sendState)在System.Net.Browser.AsyncHelper . <> c_DisplayClass1.b_0(Object sendState) ---内部异常堆栈跟踪的结束---在myApplication.MainPage.RespCallback处的System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)的System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,Object state)中IAsyncResult asynchronousResult)}
可能是什么问题?任何帮助将不胜感激 . 谢谢 .
1 回答
试试这段代码(WebClient):
或者阅读这篇文章Portable HttpClient for .NET Framework and Windows Phone