HttpWebRequest

HttpWebRequest get 方式取得网页内容

 //url: http://www.programfan.com/club/showpost.asp?id=12156
        string url = this.TextBox1.Text.ToString();
        HttpWebRequest webReques 
= (HttpWebRequest)HttpWebRequest.Create(url);
        webReques.Method 
= "GET";
        
        HttpWebResponse webRespon 
= (HttpWebResponse)webReques.GetResponse();
        System.IO.Stream newstream 
= webRespon.GetResponseStream();
        System.IO.StreamReader srRead 
= new System.IO.StreamReader(newstream, System.Text.Encoding.Default);
        
string outString=srRead.ReadToEnd();
        
this.TextBox2.Text = outString;        
        srRead.Close();         



HttpWebRequest  post 方式取得网页内容(还有待修改)

 CookieContainer cookieContain = new CookieContainer();
        

        
string url = this.TextBox1.Text.ToString();
        HttpWebRequest httpWebRequest 
= (HttpWebRequest)HttpWebRequest.Create(url);
        httpWebRequest.Method 
= "POST";
        httpWebRequest.ContentType 
= "application/x-www-form-urlencoded";

        
byte[] setStr = System.Text.Encoding.Default.GetBytes("txtEmployeeCode=10004194&txtPassword=10004194");
        httpWebRequest.ContentLength 
= setStr.Length;
        httpWebRequest.CookieContainer 
= cookieContain;


        System.IO.Stream s 
= httpWebRequest.GetRequestStream();
        s.Write(setStr, 
0, setStr.Length);
        s.Close();

        HttpWebResponse httpWebResponse 
= (HttpWebResponse)httpWebRequest.GetResponse();
        System.IO.StreamReader sr 
= new System.IO.StreamReader(httpWebResponse.GetResponseStream(), System.Text.Encoding.Default);
        
string outPut = sr.ReadToEnd();
        
//this.TextBox2.Text = outPut;
        sr.Close();
        Response.Write(outPut);
posted @ 2008-06-14 17:37  TerryLove  阅读(1595)  评论(0编辑  收藏  举报