using System.Net;
string strUrl = "http://www.sina.com.cn";
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(strUrl);HttpWebResponse oResponse  = (HttpWebResponse)oRequest.GetResponse();
StreamReader sr = new StreamReader(oResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));string sResultContents = sr.ReadToEnd();
oResponse.Close();
byte[]  bytes = System.Text.Encoding.GetEncoding("gb2312").GetBytes(sResultContents);
FileStream fs = new FileStream("c:\\2.htm", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
fs.Close();

解决方案 »

  1.   

    string sAddress=txtAddress.Text;//url
    System.Text.StringBuilder sbHtml=new System.Text.StringBuilder();
    //Disable button
    btnGetHtml.Enabled=false;HttpWebRequest hwReq=(HttpWebRequest)WebRequest.Create(sAddress);
    HttpWebResponse hwResponse=(HttpWebResponse)hwReq.GetResponse();
    Stream stream=hwResponse.GetResponseStream();
    StreamReader reader=new StreamReader(stream,System.Text.Encoding.GetEncoding("GB2312"));//char[] read = new char[256];
    // Reads 256 characters at a time.    
    int count = reader.Read( read, 0, 256 );
    while (count > 0) 
    {
    // Dumps the 256 characters on a string and displays the string to the console.
    string str = new string(read, 0, count);
    sbHtml.Append(str);
    count =reader.Read(read,0,256);
    }
    // Releases the resources
    hwResponse.Close();
    stream.Close();
    reader.Close();//
    txtHtml.Text=sbHtml.ToString();//结果
    //enable button
    btnGetHtml.Enabled=true;