문제

I am creating a simple web browser in C#. I have been instructed not to use the webbrowser class provided, so I have to code everything. I am struggling to load the page from my url. Here's my code:

private void toolStripButton5_Click(object sender, EventArgs e)
    {
        url = "http://" + toolStripTextBox1.Text;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream pageStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(pageStream);
    }

StripButton5 corresponds to my navigate button in my form. The problem i'm having is when I type the address like www.google.com. the page just hangs and doesn't load. Any suggestions appreciated.

도움이 되었습니까?

해결책

Had to encode and then read the HTML then get the browser to display it in html.

 private void toolStripButton5_Click(object sender, EventArgs e)
 {
   url = "http://" + toolStripTextBox1.Text;
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
   HttpWebResponse response = (HttpWebResponse)request.GetResponse();
   Stream pageStream = response.GetResponseStream();
   StreamReader reader = new StreamReader(pageStream,Encoding.Default);
   string s = reader.ReadToEnd();
   webBrowser1.DocumentText = s;
 }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top