1

我有一个托管在本地 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());
            }


        }



    }
4

1 回答 1

0

您是否尝试从模拟器或设备访问该页面?如果是设备,则需要确保可以从辅助设备访问网页。也就是说 - 确保端口 80 对 LAN 访问开放。

于 2013-04-14T17:28:23.340 回答