模拟提交数据,获取结果

 

发现这个很好,缺点就是没有成功案例,测试成功发个案例上来,呵呵。

模拟Post/get提交数据 并抓取返回数据

最近需要做的项目中,使用httpwebrequest httpwebrespone来模拟提交数据

还有模拟用户登录后提交数据。

下面做个简单一点的。模拟post提交数据,get在前面的抓取sina的天气预报也使用到了。

下面是代码

  1. //提交的post数据   
  2.             string postData = string.Format("account={0}&reaccount={0}&cardcode={1}&cardpassword={2}",txtLoginId.Text,txtCardNo.Text,txtPass.Text);  
  3.             postData += string.Format("&__EVENTTARGET={0}&__EVENTARGUMENT=&__VIEWSTATE={1}""nextStep""YToxOntzOjExOiJjdXJyZW50VXNlciI7YjowO30=");  
  4.             HttpWebResponse response;  
  5.             HttpWebRequest request;  
  6.             ASCIIEncoding encoding = new ASCIIEncoding();  
  7.             byte[] data = encoding.GetBytes(postData);  
  8.             request = (HttpWebRequest)WebRequest.Create("url");  
  9.             data = encoding.GetBytes(postData);  
  10.             request.Method = "POST";  
  11.             request.ProtocolVersion = HttpVersion.Version10;  
  12.             request.UserAgent = "Mozilla/4.0";  
  13.   
  14.             request.ContentType = "application/x-www-form-urlencoded";  
  15.             request.ContentLength = data.Length;  
  16.             Stream stream = request.GetRequestStream();  
  17.             stream.Write(data, 0, data.Length);  
  18.             stream.Close();  
  19.             string html = string.Empty;  
  20.             try  
  21.             {  
  22.                 //获取服务器返回的资源   
  23.                 response = (HttpWebResponse)request.GetResponse();  
  24.                 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);  
  25.                 html = reader.ReadToEnd();  
  26.                 reader.Close();  
  27.                 response.Close();  
  28.                 textBox1.Text = html;  
  29.             }  
  30.             catch (Exception ex)  
  31.             {  
  32.                 label4.Text = ex.Message;  
  33.             }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值