CookieContainer模拟登陆存储Cookie以便二次登录用
来源:互联网 发布:淘宝佣金插件 编辑:程序博客网 时间:2024/06/05 02:32
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
private static CookieContainer m_Cookie = new CookieContainer(); //记录浏览器session
protected void Page_Load(object sender, EventArgs e)
{
string strUrl = "http://xxx:8083/Api/login";
string json = "";
json = SendPostHttpRequest(strUrl, "application/x-www-form-urlencoded", "account=admin&password=admin");
string s1 = json;
strUrl = "http://xxx:8083/Api/getHistoryLast";
json = SendPostHttpRequest(strUrl, "application/x-www-form-urlencoded", "type=0&strTEID=64844962885&jsession=cfcd208495d565ef66e7dff9f98764da");
string s2 = json;
}
/// <summary>
/// 发送请求
/// </summary>
/// <param name="url">Url地址</param>
/// <param name="method">方法(post或get)</param>
/// <param name="method">数据类型</param>
/// <param name="requestData">数据</param>
public string SendPostHttpRequest(string url, string contentType, string requestData)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
if (m_Cookie != null)
{
request.CookieContainer = m_Cookie;
}
request.Method = "POST";
byte[] postBytes = null;
request.ContentType = contentType;
postBytes = Encoding.UTF8.GetBytes(requestData);
request.ContentLength = postBytes.Length;
using (Stream outstream = request.GetRequestStream())
{
outstream.Write(postBytes, 0, postBytes.Length);
}
string result = string.Empty;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (m_Cookie == null)
{
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook;
cook = response.Cookies;
m_Cookie.Add(cook);
}
if (response != null)
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
}
}
return result;
}
}
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
private static CookieContainer m_Cookie = new CookieContainer(); //记录浏览器session
protected void Page_Load(object sender, EventArgs e)
{
string strUrl = "http://xxx:8083/Api/login";
string json = "";
json = SendPostHttpRequest(strUrl, "application/x-www-form-urlencoded", "account=admin&password=admin");
string s1 = json;
strUrl = "http://xxx:8083/Api/getHistoryLast";
json = SendPostHttpRequest(strUrl, "application/x-www-form-urlencoded", "type=0&strTEID=64844962885&jsession=cfcd208495d565ef66e7dff9f98764da");
string s2 = json;
}
/// <summary>
/// 发送请求
/// </summary>
/// <param name="url">Url地址</param>
/// <param name="method">方法(post或get)</param>
/// <param name="method">数据类型</param>
/// <param name="requestData">数据</param>
public string SendPostHttpRequest(string url, string contentType, string requestData)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
if (m_Cookie != null)
{
request.CookieContainer = m_Cookie;
}
request.Method = "POST";
byte[] postBytes = null;
request.ContentType = contentType;
postBytes = Encoding.UTF8.GetBytes(requestData);
request.ContentLength = postBytes.Length;
using (Stream outstream = request.GetRequestStream())
{
outstream.Write(postBytes, 0, postBytes.Length);
}
string result = string.Empty;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (m_Cookie == null)
{
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook;
cook = response.Cookies;
m_Cookie.Add(cook);
}
if (response != null)
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
}
}
return result;
}
}
阅读全文
0 0
- CookieContainer模拟登陆存储Cookie以便二次登录用
- 简单登录用存储过程
- C#HttpClient或使用CookieContainer模拟登陆后HttpRequest不发送cookie的解决方法及原因
- CookieContainer cookie
- 用户登录用session和cookie来实现
- 使用firefox分析新浪微博登录参数(供爬虫模拟登录用)
- JForum登录用例(普通用户)
- curl模拟登陆获取cookie
- java模拟cookie登陆操作
- python使用cookie模拟登陆
- Cookie和Post模拟登陆
- 模拟登陆时Cookie的处理方式
- httpclient模拟登陆(使用js设置cookie)
- 模拟登陆之填入cookie【python】
- python模拟登陆知乎,得到cookie
- [Scrapy]模拟登陆并获取Cookie值
- [Scrapy]模拟登陆并获取Cookie值
- php curl 模拟登陆 获取cookie
- Visualizing and Understanding Convolutional Networks-ZFNet
- jQuery学习笔记
- 用 WEKA 进行数据挖掘,第 2 部分: 分类和群集
- ATR指标详细介绍
- Jenkins持续化构建Android项目(五)-设置Jenkins邮件通知(by 星空武哥)
- CookieContainer模拟登陆存储Cookie以便二次登录用
- Linux基础备忘_10:备份和恢复(包括对虚拟机的rescue)
- 进制转换2
- 宋朝五子登科家族金榜进士涂大任后裔在松滋
- 关于eclipse maven项目直接发布到tomcat服务器找不到lib的解决方法
- HTML5 地理定位(代码)
- 尼克的任务
- 河南省第十届ACM省赛题目:问题 H: Intelligent Parking Building
- Visual studio卸载不全后安装方法与重新安装后易出现MSVCP120D.dll和MSVCR120D.dll缺失情况,和exe运行时0xc000007b错误分析