近来发现这个挺有用,从网上搜集过来一系列,参考参考。
在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等。
先说下流程
1.使用httpwebrequest先进入你要登录的网站,获取cookie
2.使用第一步获取的cookie到验证码的网页将验证码下载下来。
3.使用Post数据 发送至网站。如果有cookie则继续保存。
4.使用第三步的cookie登陆相关网页操作。
获取相关数据可以使用抓包工具进行抓取,如httpwatch。(网上下载的好多都有病毒,下载的时候注意点)
1。
-
-
-
-
-
-
publicstaticArrayListGetHtmlData(stringpostUrl,CookieContainercookie)
- {
- HttpWebRequestrequest;
- HttpWebResponseresponse;
-
ArrayListlist=newArrayList();
-
request=WebRequest.Create(postUrl)asHttpWebRequest;
-
request.Method="GET";
-
request.UserAgent="Mozilla/4.0";
- request.CookieContainer=cookie;
-
request.KeepAlive=true;
- request.CookieContainer=cookie;
-
try
- {
-
-
using(response=(HttpWebResponse)request.GetResponse())
- {
-
using(StreamReaderreader=newStreamReader(response.GetResponseStream(),Encoding.Default))
- {
- cookie.Add(response.Cookies);
-
- list.Add(cookie);
- list.Add(reader.ReadToEnd());
-
list.Add(Guid.NewGuid().ToString());
- }
- }
- }
-
catch(WebExceptionex)
- {
- list.Clear();
-
list.Add("发生异常/n/r");
- WebResponsewr=ex.Response;
-
using(Streamst=wr.GetResponseStream())
- {
-
using(StreamReadersr=newStreamReader(st,System.Text.Encoding.Default))
- {
- list.Add(sr.ReadToEnd());
- }
- }
- }
-
catch(Exceptionex)
- {
- list.Clear();
-
list.Add("5");
-
list.Add("发生异常:"+ex.Message);
- }
-
returnlist;
- }
2.下载验证码,保存在本地。
-
-
-
-
-
-
publicstaticboolDowloadCheckImg(stringUrl,CookieContainercookCon,stringsavePath)
- {
-
boolbol=true;
- HttpWebRequestwebRequest=(HttpWebRequest)WebRequest.Create(Url);
-
-
webRequest.AllowWriteStreamBuffering=true;
- webRequest.Credentials=System.Net.CredentialCache.DefaultCredentials;
- webRequest.MaximumResponseHeadersLength=-1;
-
webRequest.Accept="image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/x-shockwave-flash,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/*";
-
webRequest.UserAgent="Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1;Maxthon;.NETCLR1.1.4322)";
-
webRequest.ContentType="application/x-www-form-urlencoded";
-
webRequest.Method="GET";
-
webRequest.Headers.Add("Accept-Language","zh-cn");
-
webRequest.Headers.Add("Accept-Encoding","gzip,deflate");
-
webRequest.KeepAlive=true;
- webRequest.CookieContainer=cookCon;
-
try
- {
-
-
using(HttpWebResponsewebResponse=(HttpWebResponse)webRequest.GetResponse())
- {
-
using(Streamsream=webResponse.GetResponseStream())
- {
-
List<byte>list=newList<byte>();
-
while(true)
- {
-
intdata=sream.ReadByte();
-
if(data==-1)
-
break;
-
list.Add((byte)data);
- }
- File.WriteAllBytes(savePath,list.ToArray());
- }
- }
- }
-
catch(WebExceptionex)
- {
-
bol=false;
- }
-
catch(Exceptionex)
- {
-
bol=false;
- }
-
returnbol;
- }
3。发送post数据
-
-
-
-
-
-
-
-
-
publicstaticArrayListPostData(stringpostData,stringpostUrl,CookieContainercookie)
- {
-
ArrayListlist=newArrayList();
- HttpWebRequestrequest;
- HttpWebResponseresponse;
-
ASCIIEncodingencoding=newASCIIEncoding();
-
request=WebRequest.Create(postUrl)asHttpWebRequest;
-
byte[]b=encoding.GetBytes(postData);
-
request.UserAgent="Mozilla/4.0";
-
request.Method="POST";
- request.CookieContainer=cookie;
- request.ContentLength=b.Length;
-
using(Streamstream=request.GetRequestStream())
- {
- stream.Write(b,0,b.Length);
- }
-
try
- {
-
-
using(response=request.GetResponse()asHttpWebResponse)
- {
-
using(StreamReaderreader=newStreamReader(response.GetResponseStream(),Encoding.UTF8))
- {
-
if(response.Cookies.Count>0)
- cookie.Add(response.Cookies);
- list.Add(cookie);
- list.Add(reader.ReadToEnd());
- }
- }
- }
-
catch(WebExceptionwex)
- {
- WebResponsewr=wex.Response;
-
using(Streamst=wr.GetResponseStream())
- {
-
using(StreamReadersr=newStreamReader(st,System.Text.Encoding.Default))
- {
- list.Add(sr.ReadToEnd());
- }
- }
- }
-
catch(Exceptionex)
- {
-
list.Add("发生异常/n/r"+ex.Message);
- }
-
returnlist;
- }
4。就是第三步请求的链接地址换一个就行了
好了
以上核心代码已经贴出了
具体实现需要靠你们按照你们自己的逻辑
作者还说:
还有一些header能不写就不写,因为2天前一直在获取返回response这地方报500错误。
找了N多代码,看了N多资料都不可以。最后将一些header注释掉就可以了,真郁闷。
我马上试验,等有做出成熟的例子,再写新文章提供给大家!
在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等。
先说下流程
1.使用httpwebrequest先进入你要登录的网站,获取cookie
2.使用第一步获取的cookie到验证码的网页将验证码下载下来。
3.使用Post数据 发送至网站。如果有cookie则继续保存。
4.使用第三步的cookie登陆相关网页操作。
获取相关数据可以使用抓包工具进行抓取,如httpwatch。(网上下载的好多都有病毒,下载的时候注意点)
1。
-
-
-
-
-
-
publicstaticArrayListGetHtmlData(stringpostUrl,CookieContainercookie)
- {
- HttpWebRequestrequest;
- HttpWebResponseresponse;
-
ArrayListlist=newArrayList();
-
request=WebRequest.Create(postUrl)asHttpWebRequest;
-
request.Method="GET";
-
request.UserAgent="Mozilla/4.0";
- request.CookieContainer=cookie;
-
request.KeepAlive=true;
- request.CookieContainer=cookie;
-
try
- {
-
-
using(response=(HttpWebResponse)request.GetResponse())
- {
-
using(StreamReaderreader=newStreamReader(response.GetResponseStream(),Encoding.Default))
- {
- cookie.Add(response.Cookies);
-
- list.Add(cookie);
- list.Add(reader.ReadToEnd());
-
list.Add(Guid.NewGuid().ToString());
- }
- }
- }
-
catch(WebExceptionex)
- {
- list.Clear();
-
list.Add("发生异常/n/r");
- WebResponsewr=ex.Response;
-
using(Streamst=wr.GetResponseStream())
- {
-
using(StreamReadersr=newStreamReader(st,System.Text.Encoding.Default))
- {
- list.Add(sr.ReadToEnd());
- }
- }
- }
-
catch(Exceptionex)
- {
- list.Clear();
-
list.Add("5");
-
list.Add("发生异常:"+ex.Message);
- }
-
returnlist;
- }
2.下载验证码,保存在本地。
-
-
-
-
-
-
publicstaticboolDowloadCheckImg(stringUrl,CookieContainercookCon,stringsavePath)
- {
-
boolbol=true;
- HttpWebRequestwebRequest=(HttpWebRequest)WebRequest.Create(Url);
-
-
webRequest.AllowWriteStreamBuffering=true;
- webRequest.Credentials=System.Net.CredentialCache.DefaultCredentials;
- webRequest.MaximumResponseHeadersLength=-1;
-
webRequest.Accept="image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/x-shockwave-flash,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/*";
-
webRequest.UserAgent="Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1;Maxthon;.NETCLR1.1.4322)";
-
webRequest.ContentType="application/x-www-form-urlencoded";
-
webRequest.Method="GET";
-
webRequest.Headers.Add("Accept-Language","zh-cn");
-
webRequest.Headers.Add("Accept-Encoding","gzip,deflate");
-
webRequest.KeepAlive=true;
- webRequest.CookieContainer=cookCon;
-
try
- {
-
-
using(HttpWebResponsewebResponse=(HttpWebResponse)webRequest.GetResponse())
- {
-
using(Streamsream=webResponse.GetResponseStream())
- {
-
List<byte>list=newList<byte>();
-
while(true)
- {
-
intdata=sream.ReadByte();
-
if(data==-1)
-
break;
-
list.Add((byte)data);
- }
- File.WriteAllBytes(savePath,list.ToArray());
- }
- }
- }
-
catch(WebExceptionex)
- {
-
bol=false;
- }
-
catch(Exceptionex)
- {
-
bol=false;
- }
-
returnbol;
- }
3。发送post数据
-
-
-
-
-
-
-
-
-
publicstaticArrayListPostData(stringpostData,stringpostUrl,CookieContainercookie)
- {
-
ArrayListlist=newArrayList();
- HttpWebRequestrequest;
- HttpWebResponseresponse;
-
ASCIIEncodingencoding=newASCIIEncoding();
-
request=WebRequest.Create(postUrl)asHttpWebRequest;
-
byte[]b=encoding.GetBytes(postData);
-
request.UserAgent="Mozilla/4.0";
-
request.Method="POST";
- request.CookieContainer=cookie;
- request.ContentLength=b.Length;
-
using(Streamstream=request.GetRequestStream())
- {
- stream.Write(b,0,b.Length);
- }
-
try
- {
-
-
using(response=request.GetResponse()asHttpWebResponse)
- {
-
using(StreamReaderreader=newStreamReader(response.GetResponseStream(),Encoding.UTF8))
- {
-
if(response.Cookies.Count>0)
- cookie.Add(response.Cookies);
- list.Add(cookie);
- list.Add(reader.ReadToEnd());
- }
- }
- }
-
catch(WebExceptionwex)
- {
- WebResponsewr=wex.Response;
-
using(Streamst=wr.GetResponseStream())
- {
-
using(StreamReadersr=newStreamReader(st,System.Text.Encoding.Default))
- {
- list.Add(sr.ReadToEnd());
- }
- }
- }
-
catch(Exceptionex)
- {
-
list.Add("发生异常/n/r"+ex.Message);
- }
-
returnlist;
- }
4。就是第三步请求的链接地址换一个就行了
好了
以上核心代码已经贴出了
具体实现需要靠你们按照你们自己的逻辑
作者还说:
还有一些header能不写就不写,因为2天前一直在获取返回response这地方报500错误。
找了N多代码,看了N多资料都不可以。最后将一些header注释掉就可以了,真郁闷。
我马上试验,等有做出成熟的例子,再写新文章提供给大家!
分享到:
相关推荐
平时用浏览器看网页的时候,点击一下submit按钮的时候其实就是给服务器发送了一个POST请求。但是如何在自己的C#程序里面实现类似的功能呢?本文给出具体的实现方法并详细说明其实现难点。
C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
C# asp.net http HttpWebRequest模拟浏览器请求下载文件到本地
httpwebrequest调用webservice返回数据
本示例使用C#构造SOAP信息,通过HttpWebRequest调用java编写的带有Windows身份验证的WebService,代码中详细注释了每行代码的功能与作用; 对应文章:http://blog.csdn.net/cgs_______/article/details/77894599
C#中HttpWebRequest使用介绍 GET HTTP HTTPS 请求
C#使用HttpWebRequest自动更新客户端应用程序
利用httpWebRequest模拟登陆
1.这是一个用HttpWebRequest类构建完整Http多部请求上传文件的示例 2.上传地址是金山文档预览的地址,所以,只要上传的是word、pdf、excel一类的文档,可以通过返回的地址直接在线预览,是一种偷懒的文档在线浏览...
控制台程序模拟浏览器请求,一次流程请求4次,请求的url从文件中随机读取,每次请求之间随机停5秒以内,关闭控制台之前一直循环执行流程
C# 多线程基本于httpwebrequest实现的下载功能 每个线程均可以通过事件追踪情况
HttpWebRequest C# WEBAPI 测试案例;HttpWebRequest C# WEBAPI 测试案例
使用HttpWebRequest访问网页
C#(也称Csharp)在多线程下并发执行HTTP请求的实现,采用C#封装HttpWebRequest类开发的多线程并发采集程序源码文档,文档中详细说明了HttpWebRequest并发HTTP请求实现网站采集的方法,经过测试同时并发1000+不是问题...
使用HttpWebRequest可以让开发者控制请求/响应流程的各个方面,如 timeouts, cookies, headers, protocols。另一个好处是HttpWebRequest类不会阻塞UI线程。例如,当您从响应很慢的API服务器下载大文件时,您的应用...
c#HttpWebRequest与HttpWebResponse实现模拟点击链接,模拟点击按钮和,网页抓取
1、启动和暂停下载 2、展示下载进度
主要介绍了C#实现的三种模拟自动登录和提交POST信息的方法,分别列举了WebBrowser、WebClient及HttpWebRequest实现自动登录及提交POST的相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
c# HttpWebRequest_和HttpWebResponse