模拟提交数据,获取结果
2011-07-28 21:38
183 查看
发现这个很好,缺点就是没有成功案例,测试成功发个案例上来,呵呵。
模拟Post/get提交数据 并抓取返回数据
最近需要做的项目中,使用httpwebrequest httpwebrespone来模拟提交数据还有模拟用户登录后提交数据。
下面做个简单一点的。模拟post提交数据,get在前面的抓取sina的天气预报也使用到了。
下面是代码
view plaincopy to clipboardprint?//提交的post数据
string postData = string.Format("account={0}&reaccount={0}&cardcode={1}&cardpassword={2}",txtLoginId.Text,txtCardNo.Text,txtPass.Text);
postData += string.Format("&__EVENTTARGET={0}&__EVENTARGUMENT=&__VIEWSTATE={1}", "nextStep", "YToxOntzOjExOiJjdXJyZW50VXNlciI7YjowO30=");
HttpWebResponse response;
HttpWebRequest request;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
request = (HttpWebRequest)WebRequest.Create("url");
data = encoding.GetBytes(postData);
request.Method = "POST";
request.ProtocolVersion = HttpVersion.Version10;
request.UserAgent = "Mozilla/4.0";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
string html = string.Empty;
try
{
//获取服务器返回的资源
response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
html = reader.ReadToEnd();
reader.Close();
response.Close();
textBox1.Text = html;
}
catch (Exception ex)
{
label4.Text = ex.Message;
}
//提交的post数据 string postData = string.Format("account={0}&reaccount={0}&cardcode={1}&cardpassword={2}",txtLoginId.Text,txtCardNo.Text,txtPass.Text); postData += string.Format("&__EVENTTARGET={0}&__EVENTARGUMENT=&__VIEWSTATE={1}", "nextStep", "YToxOntzOjExOiJjdXJyZW50VXNlciI7YjowO30="); HttpWebResponse response; HttpWebRequest request; ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(postData); request = (HttpWebRequest)WebRequest.Create("url"); data = encoding.GetBytes(postData); request.Method = "POST"; request.ProtocolVersion = HttpVersion.Version10; request.UserAgent = "Mozilla/4.0"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; Stream stream = request.GetRequestStream(); stream.Write(data, 0, data.Length); stream.Close(); string html = string.Empty; try { //获取服务器返回的资源 response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default); html = reader.ReadToEnd(); reader.Close(); response.Close(); textBox1.Text = html; } catch (Exception ex) { label4.Text = ex.Message; }
相关文章推荐
- ASP模拟POST提交,然后XMLHTTP获取数据总是乱码,请大家帮忙,感谢!
- 【JavaEE笔记】Struts2 (二) 结果页面配置 、获取表单提交数据 、提供获取表单数据方式 、获取数据封装到集合中
- AJAX通过get提交数据,并获取文本返回结果
- AJAX通过post提交数据,并获取XML格式返回结果
- php使用CURL模拟GET与POST向微信接口提交及获取数据的方法
- php模拟GET与POST向微信接口提交及获取数据的方法
- Ajax开发过程中提交获取数据的乱码问题
- 利用Jsoup模拟跳过登录爬虫获取数据
- Fiddler进行模拟Post提交json数据,总为null解决方式
- struts2学习(二)—action获取表单提交数据的三种方式
- 当From窗体中数据变化时,使用代码获取数据库中的数据然后加入combobox中并且从数据库中取得最后的结果
- Fiddler进行模拟Post提交json数据,总为null解决方式
- C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)
- 如何用PHP模拟POST方法 采集提交后的数据
- 用ajax提交数据到后台,并且把计算结果从后台把数据转到前台(该过程不刷新页面)
- ajax 提交post get 数据,得到json结果解析
- ajax提交file表单,spring mvc后台获取file表单数据
- 模拟Post/get提交数据 并抓取返回数据
- PHP put提交和获取数据
- JAVA 爬虫之httpclient post请求提交表单获取Ajax数据