我有一个托管在本地 Apache 服务器上的 HTML 页面,我正在尝试使用以下代码向该页面发出 HTTP Web 请求。代码遇到了一个 web 异常,并且也抛出了一个异常invalid argument exception
。这发生在用于从代码的 catch 部分中的流中获取数据的代码中:
private void b1_Click_1(object sender, RoutedEventArgs e)
{
System.Uri targetUri = new System.Uri(@"http://192.168.1.4/san/index1.html");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
}
private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
try
{
HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
string results = httpwebStreamReader.ReadToEnd();
//TextBlockResults.Text = results; //-- on another thread!
// Dispatcher.BeginInvoke(() => TextBlockResults.Text = results);
}
myResponse.Close();
}
catch (WebException ex)
{
using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream()))
{
Debug.WriteLine(reader.ReadToEnd());
}
}
}