C#模拟提交表单,已登陆的用户cookie传送,返回到登陆页面
stringstrcok="cookie";ServicePointManager.ServerCertificateValidationCallback=newSyst...
string strcok = "cookie";
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
CookieContainer cookie = new CookieContainer();
request.CookieContainer=cookie;//如果用不到Cookie,删去即可
//以下是发送的http头,随便加,其中referer挺重要的,有些网站会根据这个来反盗链
request.Referer = referer;
request.Accept = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Headers["Accept-Language"] = "zh-CN,zh;q=0.8";
request.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";
request.Headers["Cookie"] = strcok;
request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36";
request.KeepAlive = true;
//上面的http头看情况而定,但是下面俩必须加
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
string postString = "companyName=阔海玻璃";//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进来
byte[] postData = Encoding.UTF8.GetBytes(postString);//编码,尤其是汉字,事先要看下抓取网页的编码方式
Encoding encoding = Encoding.UTF8;//根据网站的编码自定义
//byte[] postData = encoding.GetBytes(postDataStr);//postDataStr即为发送的数据,格式还是和上次说的一样
request.ContentLength = postData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postData, 0, postData.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
//如果http头中接受gzip的话,这里就要判断是否为有压缩,有的话,直接解压缩即可
if (response.Headers["Content-Encoding"] != null && response.Headers["Content-Encoding"].ToLower().Contains("gzip"))
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
}
StreamReader streamReader = new StreamReader(responseStream, encoding);
string ck = response.Headers.Get("Set-Cookie");
string retString = streamReader.ReadToEnd(); 展开
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
CookieContainer cookie = new CookieContainer();
request.CookieContainer=cookie;//如果用不到Cookie,删去即可
//以下是发送的http头,随便加,其中referer挺重要的,有些网站会根据这个来反盗链
request.Referer = referer;
request.Accept = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Headers["Accept-Language"] = "zh-CN,zh;q=0.8";
request.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";
request.Headers["Cookie"] = strcok;
request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36";
request.KeepAlive = true;
//上面的http头看情况而定,但是下面俩必须加
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
string postString = "companyName=阔海玻璃";//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进来
byte[] postData = Encoding.UTF8.GetBytes(postString);//编码,尤其是汉字,事先要看下抓取网页的编码方式
Encoding encoding = Encoding.UTF8;//根据网站的编码自定义
//byte[] postData = encoding.GetBytes(postDataStr);//postDataStr即为发送的数据,格式还是和上次说的一样
request.ContentLength = postData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postData, 0, postData.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
//如果http头中接受gzip的话,这里就要判断是否为有压缩,有的话,直接解压缩即可
if (response.Headers["Content-Encoding"] != null && response.Headers["Content-Encoding"].ToLower().Contains("gzip"))
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
}
StreamReader streamReader = new StreamReader(responseStream, encoding);
string ck = response.Headers.Get("Set-Cookie");
string retString = streamReader.ReadToEnd(); 展开
1个回答
2015-03-23 · 知道合伙人软件行家
关注
展开全部
你代码不都写了吗。还要干什么啊。无非就是分析下retString里的内容
更多追问追答
追问
我已经手动登陆了,想在里面某个页面实现自动提交,但是返回的数据是登陆页面。是不是这个cookie传送有问题呢 ,cookie我是抓包得到的。
追答
一个页面没有登录,服务器会进去质询,要求你登录,所以。返回的是登录页才正常啊。
你在最后字符串里判断下是否是登录页的地址,如果是,则再访问你要的页面。把cookie带上。
先登录,再请求具体页面(整体流程)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询