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 ...
随机推荐
- Nginx - SSI Module
SSI, for Server Side Includes, is actually a sort of server-side programming language interpreted by ...
- 【AngularJs】---实现select的ng-options
controller .controller('MainController', function($scope, $http, $ionicModal, $timeout) { var post = ...
- 为Widget添加事件
原帖:http://bbs.51cto.com/thread-965565-1-1.html 在appWidget中,ImageButton和Button都是被支持的控件,其事件可分成三种类型: ...
- ansible安装(批量执行命令
rpm安装 下载epl源 : Download the latest epel-release rpm from:http://dl.fedoraproject.org/pub/epel/6/x86 ...
- Java之趣味编程结婚问题
问题如下:判断结婚的组合对数数.定义: 好三位新郎为 A,B,C ;三位新娘为X,Y,Z 有人想要知道他们谁和谁结婚 ,于是问了其中的三位. 回答是这样的:A说他将和X结婚 :X说她的未婚夫是C ;C ...
- knowlege experience
The konwledge is you need learning some basic knowledge. The experience is you can use konwledge ma ...
- PHP之HMVC
HMVC(Hierarchical-Model-View-Controller),也可以叫做 Layered MVC.顾名思义,就是按等级划分的 MVC 模式,简单的解释就是把MVC又细分成了多个子 ...
- SCSS是什么
SCSS即是SASS的新语法,是Sassy CSS的简写,是CSS3语法的超集,也就是说所有有效的CSS3样式也同样适合于SASS. SASS是CSS3的一个扩展,增加了规则嵌套.变量.混合.选择器继 ...
- 从0开始学习react(三)
这次我们来讲解第三节知识,考虑了下,先不去讲什么理论了,毕竟网上一搜一大堆,而且理论真心看不太懂啊!!! 今天我们就直接上实例喽! 大家HIGH起来!!!(想了好久,还是没舍得删这句话) 1.根据下图 ...
- Js中的运算符
运算符 运算符:就是可以运算的符号 比如 + .-.*./ 运算符包括: 算术运算符 比较运算符 逻辑运算符 赋值运算符 字符串运算符 1.算术运算符 +.-.*./.%(求余数).++.-- ++: ...