C# HttpClient Cookie验证解决方法
程序员文章站
2023-12-09 23:21:33
自实现的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();
以下代码配合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();