http in c# GET a page



这是我在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.1rn"
                + "Host: " + address + ":" + port + "rn";
            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);
        }

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

    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";
    }

最新更新