带CookieContainer进行post
1.获取CookieContainer ——用户登录
CookieContainer cookie = new CookieContainer();
UserLoginPost("post地址", "参数", ref cookie, "utf-8");
void UserLoginPost(string Url, string postDataStr, ref CookieContainer cookie1, string EncodingType)
{
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(Url);
if (cookie1.Count == 0)
{
request1.CookieContainer = new CookieContainer();
cookie1 = request1.CookieContainer;
}
else
{
request1.CookieContainer = cookie1;
}
byte[] postData = Encoding.GetEncoding(EncodingType).GetBytes(postDataStr);
request1.Method = "POST";
request1.KeepAlive = false;
request1.AllowAutoRedirect = true;
request1.ContentType = "application/x-www-form-urlencoded";
request1.ContentLength = postData.Length;
request1.Referer = Url;
request1.UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:14.0) Gecko/20100101 Firefox/14.0.1";
//提交
Stream myRequestStream = request1.GetRequestStream();
myRequestStream.Write(postData, 0, postData.Length);
myRequestStream.Dispose();
myRequestStream.Close();
//获取信息
HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
response1.Cookies = cookie1.GetCookies(request1.RequestUri);//获取一个包含url的Cookie集合的CookieCollection
Stream myResponseStream = response1.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(EncodingType));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
}
2.保存CookieContainer到txt里
public List<Cookie> GetAllCookies(CookieContainer cc)
{
List<Cookie> lstCookies = new List<Cookie>();
Hashtable table = (Hashtable)cc.GetType().InvokeMember("m_domainTable",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField |
System.Reflection.BindingFlags.Instance, null, cc, new object[] { });
foreach (object pathList in table.Values)
{
SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField
| System.Reflection.BindingFlags.Instance, null, pathList, new object[] { });
foreach (CookieCollection colCookies in lstCookieCol.Values)
foreach (Cookie c in colCookies) lstCookies.Add(c);
}
return lstCookies;
}
public string CookieContainerToString(CookieContainer cc)
{
StringBuilder sbc = new StringBuilder();
List<Cookie> cooklist = GetAllCookies(cc);
foreach (Cookie cookie in cooklist)
{
sbc.AppendFormat("{0};{1};{2};{3};{4};{5}\r\n",
cookie.Domain, cookie.Name, cookie.Path, cookie.Port,
cookie.Secure.ToString(), cookie.Value);
}
return sbc.ToString();
}
FileStream fs = File.Create("E:\\caijiPerson\\ajax\\jianli.txt");
fs.Close();
File.WriteAllText("E:\\caijiPerson\\ajax\\jianli.txt", CookieContainerToString(cookie), System.Text.Encoding.Default);
3.读取txt里的cookie
//读取txt里的cookie 第一种方法
string[] cookies = File.ReadAllText("E:\\caijiPerson\\ajax\\jianli.txt", System.Text.Encoding.Default).Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
//读取txt里的cookie 第2种方法
StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("ajax/jianli.txt"), System.Text.Encoding.Default);
string input = sr.ReadToEnd();
sr.Close();
string[] cookies = Regex.Split(input, "\r\n", RegexOptions.IgnoreCase);
//
string cookieString = changeCookieForSend(cookies);
public string changeCookieForSend(string[] cookies)
{
string sendCookie = "";
foreach (string c in cookies)
{
string[] cc = c.Split(";".ToCharArray());
Cookie ck = new Cookie(); ;
ck.Discard = false;
ck.Domain = cc[0];
ck.Expired = true;
ck.HttpOnly = true;
ck.Name = cc[1];
ck.Path = cc[2];
ck.Port = cc[3];
ck.Secure = bool.Parse(cc[4]);
ck.Value = cc[5];
sendCookie += ck.Name + "=" + ck.Value + ";";
}
return sendCookie;
}
4.带上cookie字符串去post
string cookieString = changeCookieForSend(cookies);
html = GetPostData("post地址", "utf-8", cookieString);
string GetPostData(string url, string encodeType, string cookieString)
{
request = (HttpWebRequest)HttpWebRequest.Create(url); //创建一个请求示例
//本来4.0以下的使用 request.CookieContainer = CookieContainer;
//4.0以上的包括4.0用下面的字符串形式
request.Headers["Cookie"] = cookieString;//net4.0以上的有一个bug,所以用这个cookie字符串的格式来实现
request.Method = "GET";
request.Host = "jianli.tzrl.com";
request.KeepAlive = true;
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; QQDownload 717; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
response = (HttpWebResponse)request.GetResponse(); //获取响应,即发送请求
//response.Cookies = cookie1.GetCookies(request.RequestUri);//获取一个包含url的Cookie集合的CookieCollection
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encodeType));
string html = streamReader.ReadToEnd();
return html;
}
带CookieContainer进行post的更多相关文章
- 用HttpWebRequest提交带验证码的网站
using System; using System.Drawing; using System.IO; using System.Net; using System.Text; using Syst ...
- HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理
原文地址:http://blog.csdn.net/cdefg198/article/details/8315438 万能框架:http://www.sufeinet.com/forum.php?mo ...
- C# 模拟提交带附件(input type=file)的表单
今天调用某API时,对于文档中的传入参数:File[] 类型,感觉很陌生,无从下手! 按通常的方式在json参数中加入file的二进制数据提交,一直报错(参数错误)!后来经过多方咨询,是要换一种 表单 ...
- HttpHelpers类普通GET和POST方式,带Cookie和带证书验证模式
HttpHelpers类普通GET和POST方式,带Cookie和带证书验证模式 参考路径:https://www.cnblogs.com/splendidme/archive/2011/09/14/ ...
- C# 调用API接口处理公共类 自带JSON实体互转类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- WPF带cookie get/post请求网页,下载文件,图片,可保持会话状态
直接写成啦一个MyNet.cs类方便使用 get/post方法请求 //get请求 MyNet.SendRequest("http://www.baidu.com"); //pos ...
- C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)
第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...
- 带你实现开发者头条APP(五)--RecyclerView下拉刷新上拉加载
title: 带你实现开发者头条APP(五)--RecyclerView下拉刷新上拉加载 tags: -RecyclerView,下拉刷新,上拉加载更多 grammar_cjkRuby: true - ...
- 带你实现开发者头条APP(四)---首页优化(加入design包)
title: 带你实现开发者头条APP(四)---首页优化(加入design包) tags: design,Toolbar,TabLayout,RecyclerView grammar_cjkRuby ...
随机推荐
- Codeforces 260C - Balls and Boxes
260C - Balls and Boxes 思路:模拟.在x前面找到最小值,如果没有,从0跳到n,继续找到最小值,边找最小值路过的点边减1.然后所有值都减去最小值,最小值那个点加上减去的值. 找到x ...
- python 函数参数介绍
python 函数参数介绍 python 使用过程总,总会遇到 *args,**kw形式的参数,总是一头雾水,而且网上介绍的或是叫法不一,为此专门深入实践进而了解了函数参数的使用 具体请看代码 #-* ...
- Ubuntu 14.04 的 VNC Server
首先,如果是Desktop 版本的 Ubuntu,不需要另外安装vnc server. 网上也不知怎么搞的,一堆奇怪的方法,要安装TightVNCServer,然后一堆sb设置 然后,主要有两个配置 ...
- android------锁屏(手机启动出现锁屏界面)
以前用过一个红包锁屏的软件,第一次打开手机出现锁屏,滑动领取收益,当时觉得这功能不错,就查阅资料,写了一个案例, apk运行流程: 进入软件--->启动服务--->关闭手机(可先退出应用) ...
- Sunday算法[原创]
一.应用: 同样的,sunday算法也是在一个字符串中查找另一个字符串出现的首地址,是Daniel M.Sunday于1990年提出的,从销量上讲,Sunday>BM>KMP,是这类问题的 ...
- h1042 N!大数乘int
计算10000以内某个数的阶乘,即大数乘以int,考虑到一个int存一个数位过于繁琐且浪费空间,采用万进制 一个int存四个位数,但注意除了最高位,其他位不够四位数时要加上前导0凑够四位: 例1234 ...
- 二叉树—-1(No.9HN省赛小题)
题目: 1013: Prototypes analyze 时间限制: 1 Sec 内存限制: 128 MB提交: 6 解决: 4[提交][状态][讨论版] 题目描述 ALpha Ceiling M ...
- Leetcode 96
class Solution { public: int numTrees(int n) { ]; dp[] = ; dp[] = ; dp[] = ; ;i <= n;i++){ ; ;j & ...
- WinForm窗体自适应分辨率
我们自己编写程序的界面,会遇到各种屏幕分辨率,只有自适应才能显的美观.实际上,做到这点也很简单,就是首先记录窗体和它上面控件的初始位置和大小,当窗体改变比例时,其控件的位置和大小也按此比例变化即可.因 ...
- 网络SSID是什么意思
ssid是网络的ID(名称).一般用在无线网络上.搜索无线网络名一般就是在搜索无线网络的ssid. SSID是Service Set Identifier的缩写,意思是:服务集标识.SSID技术可以将 ...