-3

这是我的阅读网页。在c#语言中。
但是当我执行它时它有一些例外。有人知道为什么吗?

          try
        {
            string address = "http://" + txtMsg.Text;
            int port = int.Parse(textBox1.Text);
            System.Net.WebClient webclient = new WebClient();
            String content = webclient.DownloadString(address);

            Socket skt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            skt.Connect(address, port);
            string hdrs = "GET " + address + ":" + port + " HTTP/1.1\r\n"
                + "Host: " + address + ":" + port + "\r\n";
            byte[] req_as_bytes = Encoding.UTF8.GetBytes(hdrs);
            skt.Send(req_as_bytes);
            byte[] data = new byte[1024 * 200];
            int t = skt.Receive(data);
            lstMsg.Items.Add(Encoding.UTF8.GetString(data, 0, t));
            skt.Shutdown(SocketShutdown.Both);
            skt.Close();

            btnConnect.Text = "done";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
4

1 回答 1

3

好的 !
我找到了解决方案:

    private void btnConnect_Click(object sender, EventArgs e)
    {
        var address = "http://" + txtMsg.Text;
        var webclient = new System.Net.WebClient();
        var content = webclient.DownloadString(address);
        //any work 
        btnConnect.Text = "done";
    }
于 2012-06-24T22:19:36.647 回答