当前位置: 移动技术网 > IT编程>开发语言>c# > C# HttpClient Cookie验证解决方法

C# HttpClient Cookie验证解决方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
自实现的cookie 验证,远程取值的例子 以下代码配合httpclient使用可以实现跨域(cookie的读写) //验证 复制代码 代码如下: httpclient h
自实现的cookie 验证,远程取值的例子

以下代码配合httpclient使用可以实现跨域(cookie的读写)
//验证
复制代码 代码如下:

httpclient httpclient = new httpclient(url, null, true);
httpclient.postingdata.add(key,value);//登录用户名
httpclient.postingdata.add(key,value);//密码
string str = httpclient.getstring();

----写文件 序列化传回来的cookie
复制代码 代码如下:

cookiecollection cookies = httpclient.context.cookies;//保存一个全局的cookie文件
filestream filestream = new filestream("xxx.dat", filemode.create);
binaryformatter b = new binaryformatter();
b.serialize(filestream, cookies);
filestream.close();

--读文件 反序列化cookies 赋给httpclient的cookies
复制代码 代码如下:

filestream filestream = new filestream("xxx.dat", filemode.open, fileaccess.read, fileshare.read);
binaryformatter b = new binaryformatter();
cookiecollection cookies = b.deserialize(filestream) as cookiecollection;
httpclient httpclient = new httpclient("url");//取值的url
httpclient.context.cookies = cookies;
string str = httpclient.getstring();

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网