winform保存登录cookie
最好注意以下几点:
1.使用成员变量:CookieContainer mycookie=new CookieContainer();
2.在每个HttpWebRequest创建后赋值 CookieContainer 属性 request.CookieContainer = mycookie;
3.在每次创建响应HttpWebResponse时将cookie的内容重新写回请求中
response.Cookies = mycookie.GetCookies(request.RequestUri);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms; namespace APPTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// private const string baseUrl = "";
private CookieContainer mycookie=new CookieContainer();
private CookieCollection cookieCollection = new CookieCollection();
private void Form1_Load(object sender, EventArgs e)
{
txtbaseUrl.Text = "http://passport.baidu.com/center";
txtparams.Text = "{\"loginname\":\"...\",\"loginpwd\":\"...\"}";
txtType.Text = "Post";
/*
uuid:db1921de-421b-4f8d-8ca4-664c7cfcd0c7
loginname:1234
nloginpwd:123
loginpwd:123
machineNet:
machineCpu:
machineDisk:
authcode:mfcv
dAsFXMIFRN:fDFLH
*/
} private void btnOK_Click(object sender, EventArgs e)
{
mydata data = new mydata { url=txtUrl.Text, param=txtparams.Text, type=txtType.Text };
if (data.type.ToUpper()=="GET")
{
richTextBox1.Text = GetDataWithMyClass(data);
}else{
richTextBox1.Text = PostDataWithMyClass(data);
} } private string GetDataWithMyClass(mydata data)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}{1}/{2}",txtbaseUrl.Text,data.url,data.param));
request.CookieContainer = mycookie;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string ret = string.Empty;
Stream s;
string strValue = ""; if (response.StatusCode == HttpStatusCode.OK)
{
response.Cookies = mycookie.GetCookies(request.RequestUri);
s = response.GetResponseStream();
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
strValue = Reader.ReadToEnd();
Reader.Close();
}
return strValue;
} private string PostDataWithMyClass(mydata data)
{ // 将提交的字符串数据转换成字节数组
byte[] postData = Encoding.UTF8.GetBytes(data.param); // 设置提交的相关参数
HttpWebRequest request = WebRequest.Create(string.Format("{0}{1}", txtbaseUrl.Text, data.url)) as HttpWebRequest;
request.Method = data.type;
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
// request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.CookieContainer = mycookie;
request.ContentLength = postData.Length; // 提交请求数据
System.IO.Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close(); HttpWebResponse response;
Stream responseStream;
StreamReader reader;
string srcString;
try
{
response = request.GetResponse() as HttpWebResponse;
response.Cookies = mycookie.GetCookies(request.RequestUri);
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
srcString = reader.ReadToEnd();
cookieCollection = response.Cookies;
reader.Close();
return srcString;
}
catch (Exception e)
{
MessageBox.Show(e.Message,"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return ""; } private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (((RadioButton)sender).Checked)
{
txtbaseUrl.Text = "http://passport.baidu.com/center";
txtparams.Text = "{\"loginname\":\"\",\"loginpwd\":\"\"}";
txtType.Text = "Post";
}
else {
txtbaseUrl.Text = "http://jd2008.jd.com/JdHome/OrderList.aspx";
txtparams.Text = "";
txtType.Text = "Get";
} }
}
public class mydata
{
public string remark { get; set; }
public string text { get; set; }
public string url { get; set; }
public string param { get; set; }
public string type { get; set; }
}
}
winform保存登录cookie的更多相关文章
- 保存登录信息的Cookie加密技术
所有需要账户登录的website 基本都会想到这样一个问题, 如何保持用户在一定时间内登录有效. 最近本人就在项目中遇到这样的需求,某些页面只能Admin账户登录后访问, 当登录Admin账户后如何才 ...
- cookie保存登录的用户名和密码
用cookie保存登录的用户名和密码,当用户访问网站的时候,获取cookie的用户名和密码,通过用 用cookie保存登录的用户名和密码,当用户访问网站的时候,获取cookie的用户名和密码,通过用户 ...
- JMeter 怎么保存登录状态
在Recording Controller中添加一个HTTP Cookie Manager Recording Controller右键-->add-->config element--& ...
- c#获取新浪微博登录cookie
用新浪微博api收集数据有诸多限制,每小时只能调用官方api函数150次,认证也很麻烦.因此想通过爬网页的方式来收集数据.访问新浪微博用户网页首先需要登录,登录获取cookie后可直接获取网页数据,无 ...
- Servlet中保存的cookie值读取不到
在设计登录时记住密码功能时,很多时候回使用cookie,在Servlet中保存cookie时,再次访问登录页面,没有读取到保存的cookie值,代码如下: 1 Cookie idCookie = ne ...
- C#保存登录用户名供其他页面调用
一.保存登录用户名供其他页面调用 步骤: (1)项目自带的Program.cs,类方法里定义登录的用户名为全局变量loginid,这样整个项目都可以调用它 static class Program { ...
- [android]-如何在向服务器发送request时附加已保存的cookie数据
[android]-如何在向服务器发送request时附加已保存的cookie数据 应用场景:在开发android基于手机端+服务器端的应用时,登陆->获取用户信息->获取授权用户相关业务 ...
- XManager&XShell如何保存登录用户和登录密码
Xshell配置ssh免密码登录 - qingfeng2556的博客 - CSDN博客https://blog.csdn.net/wuhenzhangxing/article/details/7948 ...
- 爬虫程序获取登录Cookie信息时遇到302,怎么处理
最近要做个爬虫程序爬爬东西,先搞定登录授权这块,没得源代码,所以只能自行搞定了,按平时的直接发起HttpWebRequest(req)请求,带上用户名密码,好了,然后 HttpWebResponse ...
随机推荐
- css3 web字体记
css3 web字体 @font-face语法 @font-face能够加载服务器端的字体,让客户端浏览器显示客户端没有安装的字体. @font-face{ font-family:<YourW ...
- Quartz Scheduler(2.2.1) - Working with SchedulerListeners
SchedulerListeners SchedulerListeners are much like TriggerListeners and JobListeners, except they r ...
- mssql 查询效率
(1)临时表.表变量 据说:当数据量<100行数据时使用表变量,数据量较大时使用临时表(可创建索引提高查询效率). 表变量只能创建主键或唯一索引,准确讲是约束不是索引. (2)存储过程直接在查询 ...
- SQL Server的三种物理连接之Loop Join(一)
Sql Server有三种物理连接Loop Join,Merge Join,Hash Join, 当表之间连接的时候会选择其中之一,不同的连接产生的性能不同,理解这三种物理连接对性能调优有很大帮助. ...
- 我眼中的go的语法特点
因为基本从c#/javascript/c/c++/python/lua/action script,一路走来,对所有的C系列的语法既熟悉又有好感: 那在看语言的时候肯定会与C系列的东西进行类比,那就总 ...
- 三星智能手机如何运用Smart Switch?
1.Smart Switch是指? 使用前注意事项: 将两部智能手机的距离保持在50cm以内 两部智能手机都下载相同的最新版本Smart Switch 确认可支持的机器参考应用程序说明 不受干扰的安静 ...
- Jquery库及其他库之间的$命名冲突解决办法
首先我们应该知道,在jquery中,$(美元符号)就是jquery的别名,也就是说使用$和使用jquery是一样的,在很多时候我们命名空间时,正是因为这个$而产生的冲突的发生.比如说:$('#xmla ...
- (转)实战Memcached缓存系统(2)Memcached Java API基础之MemcachedClient
1. 构造函数 public MemcachedClient(InetSocketAddress[] ia) throws IOException; public MemcachedClient(Li ...
- 通过使用Chrome的开发者工具来学习JavaScript
本文作者是Peter Rybin,Chrome开发者工具团队成员. 本文中,我们将通过使用Chrome的开发者工具,来学习JavaScript中的两个重要概念”闭包”和”内部属性”. 闭包 首先要讲的 ...
- 基于FPGA的按键扫描程序
最近在学习FPGA,就试着写了个按键扫描的程序.虽说有过基于单片机的按键扫描处理经验,对于按键的处理还是有一些概念.但是单片机程序的编写通常都采用C写,也有用汇编,而FPGA却是采用VHDL或者Ver ...