CookieContainer模拟登陆存储Cookie以便二次登录用
2017-05-24 17:55
671 查看
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;
}
}
相关文章推荐
- [转发]HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
- HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
- HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
- C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用
- python3下使用scrapy实现模拟用户登录与cookie存储 —— 基础篇(马蜂窝)
- python3下使用scrapy实现模拟用户登录与cookie存储—— 中级篇(百度云俱乐部)
- C# : WebRequest发起Http Post请求模拟登陆并cookie处理示例
- 模拟登陆时Cookie的处理方式
- 如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证
- 如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证
- (winform cookie)如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证
- c# 实现网页上用户自动登陆|asp.net 模拟网站登录
- 如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证(续)
- 如果向某网址Post信息,并得到CookieContainer以便以后直接通过验证
- 如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证
- ecshop整合UCenter,discuz,实现会员同步登录 不需要二次登陆
- 如果向某网址Post信息,并得到CookieContainer以便以后直接通过验证
- 如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证
- 如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证
- 几个SQL server登陆,登录,用户,角色和组用到的系统存储过程