c# 使用网站的身份验证及 Cookie 的获取与使用
C# 的 Http 访问可以使用 .net 自带的 HttpWebRequest, WebClient, HttpClient 类。也可以使用开源库 RestSharp 。
RestSharp 的优点很多,最重要的一点是对于新手来说,Postman 直接提供了 RestSharp 的实现代码。


下面给出了网站的登录与 Cookie 的获取使用的方式,分别用 WebRequest 和 RestSharp 实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Threading;
using System.IO;
using System.Net.Http;
using RestSharp; namespace WebServer
{
class Program
{
static void Main(string[] args)
{
if (true)
{
//使用RestSharp
RestResponseCookie cookie = null;
string html = GetHtml("http://xd:1234@192.168.0.121:8080/vetsync/v1/orders", out cookie);
Console.WriteLine(html);
html = GetHtml("http://192.168.0.121:8080/vetsync/v1/orders", cookie);
Console.WriteLine(html);
}
else
{
//使用WebRequest
string cookie;
string html = GetHtml("http://192.168.0.121:8080/vetsync/v1/orders", "xd", "", out cookie);
Console.WriteLine(html);
html = GetHtml("http://192.168.0.121:8080/vetsync/v1/orders", cookie);
Console.WriteLine(html);
}
Console.ReadLine();
} //第一次使用用户名密码登录(在 url 中),获得cookie
public static string GetHtml(string url, out RestResponseCookie cookie)
{
var client = new RestClient(url);
RestRequest request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Basic eGQ6MTIzNA==");
IRestResponse response = client.Execute(request);
cookie = response.Cookies[];
return response.Content;
} //第二次及以后访问使用cookie验证
public static string GetHtml(string url, RestResponseCookie cookie)
{
var client = new RestClient(url);
RestRequest request = new RestRequest(Method.GET);
request.AddCookie(cookie.Name, cookie.Value);
IRestResponse response = client.Execute(request);
return response.Content;
} //第一次使用用户名密码登录,获得cookie
public static string GetHtml(string url, string userName, string password, out string cookie)
{
WebRequest request = WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultCredentials;
string code = Convert.ToBase64String(Encoding.Default.GetBytes(string.Format("{0}:{1}", userName, password)));
request.Headers.Add("Authorization", "Basic " + code);
WebResponse wrp = request.GetResponse();
cookie = wrp.Headers.Get("Set-Cookie");
return new StreamReader(wrp.GetResponseStream(), Encoding.Default).ReadToEnd();
} //第二次及以后访问使用cookie验证
public static string GetHtml(string url, string cookieStr)
{
WebRequest wrt = WebRequest.Create(url);
wrt.Credentials = CredentialCache.DefaultCredentials;
wrt.Headers.Add("Cookie", cookieStr);
WebResponse wrp = wrt.GetResponse();
return new StreamReader(wrp.GetResponseStream(), Encoding.Default).ReadToEnd();
}
}
}
c# 使用网站的身份验证及 Cookie 的获取与使用的更多相关文章
- iis 访问网站需要进行身份验证
今天网站输入域名访问的时候提示需要输入账号密码,这是权限出了问题,百度了一下,解决了,分享一下: 1.登陆远程,右键我的电脑->管理->本地用户和组->用户,里面有一个IUSR_WD ...
- ASP.NET Web API 2基于令牌的身份验证
基于令牌的认证 我们知道WEB网站的身份验证一般通过session或者cookie完成的,登录成功后客户端发送的任何请求都带上cookie,服务端根据客户端发送来的cookie来识别用户. WEB A ...
- ASP.NET身份验证
Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验 证用的最多,也最灵活. Forms 验证方式对基于用户的验证 ...
- Asp.net 身份验证
Forms 验证方式对基于用户的验证授权提供了很好的支持,可以通过一个登录页面验证用户的身份,将此用户的身份发回到客户端的Cookie,之后此用户再访问这个 web应用就会连同这个身份Cookie一起 ...
- ASP.NET中身份验证的三种方法
Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验证用的最多,也最灵活.Forms 验证方式对基于用户的验证授权 ...
- asp.net中常用的几种身份验证方式
转载:http://www.cnblogs.com/dinglang/archive/2012/06/03/2532664.html 前言 在B/S系统开发中,经常需要使用"身份验证&q ...
- Web服务器使用基于纯文本表单的身份验证——.net(未完待续)
asp.net 表单验证方式 Asp.net的身份验证有有三种,分别是"Windows | Forms| Passport",其中又以Forms验证用的最多,也最灵活. 根据实际需 ...
- 客官,来看看AspNetCore的身份验证吧
开篇 这段时间潜水了太久,终于有时间可以更新一篇文章了. 通过本篇文章您将Get: Http的一些身份验证概念 在AspNetCore中实现身份验证方案 JWT等概念的基础知识 使用Bearer To ...
- 添加AD验证(域身份验证)到现有网站
每个网站几乎都会有用户登录的模块,登录就会涉及到身份验证的过程.通常的做法是在页面上有个登录的Form,然后根据用户名和密码到数据库中去进行验证. 而验证后如何在网站的各个页面维持这种认证过的状态,有 ...
随机推荐
- day04 列表增删改查、元祖以及range
01 课前小甜点 千万不要随意做决定 只要你做了决定,你要坚持下去. 02 昨日内容回顾 int <---> bool : 非0 True 0 False True 1 False 0 i ...
- Codeforces Round #420 (Div. 2) - B
题目链接:http://codeforces.com/contest/821/problem/B 题意:二维每个整点坐标(x,y)拥有的香蕉数量为x+y,现在给你一个直线方程的m和b参数,让你找一个位 ...
- 利用 Redis 锁解决高并发问题
这里我们主要利用 Redis 的 setnx 的命令来处理高并发. setnx 有两个参数.第一个参数表示键.第二个参数表示值.如果当前键不存在,那么会插入当前键,将第二个参数做为值.返回 1.如果当 ...
- jmeter 参数化3_User Defined Variables(用户自定义变量)
User Defined Variables: 一般用于Test Plan中不需要随请求迭代的参数设置,如:Host.Port Number 操作路径:Thread Group-->Add-- ...
- BZOJ 2565 最长回文串
传送门 回文自动机! 正着跑一遍 记录以每个点作为回文子串的右端点的最大长度 倒过来跑一遍 记录每个点作为左端点的最大长度 求个和就好啦 附代码. #include<cstdio> #in ...
- call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数
<?php function foobar($arg, $arg2) { echo __FUNCTION__, " got $arg and $arg2\n"; } clas ...
- IntelliJ IDEA设置maven
1.更改默认的maven仓库 2.手动更新maven 项目——也就是下载依赖的jar包 3. 不想每次手动更新,设置IDEA自动更新mav项目,下载jar包
- shell脚本学习(4)cut
cut 的两种用法 1种是 -c list 剪切字符串中特定位置的文字, /etc/passwd中的原始数据: yuyuyu:x:1000:1000:yuyuyu,,,:/home/yuyuyu: ...
- Android中实现Activity的启动拦截之----实现360卫士的安装应用界面
第一.摘要 今天不是周末,但是我已经放假了,所以就开始我们的技术探索之旅,今天我们来讲一下Android中最期待的技术,就是拦截Activity的启动,其实我在去年的时候,就像实现这个技术了,但是因为 ...
- 【CF1257A】Two Rival Students【思维】
题意:给你n个人和两个尖子生a,b,你可以操作k次,每次操作交换相邻两个人的位置,求问操作k次以内使得ab两人距离最远是多少 题解:贪心尽可能的将两人往两边移动,总结一下就是min(n-1,|a-b| ...