HttpWebRequest

本文介绍了如何使用HttpWebRequest通过GET和POST方式从指定URL获取网页内容。GET方式直接请求并读取页面,而POST方式则包含发送表单数据的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

转载于:https://www.cnblogs.com/mooner/archive/2008/06/14/1221978.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值