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,然后根据用户名和密码到数据库中去进行验证. 而验证后如何在网站的各个页面维持这种认证过的状态,有 ...
随机推荐
- nodejs,事件轮询总结
宏任务 script,setTimeoout,setInterval,setlmmediate(node 独有),I/o,render渲染 微任务 process.nextTick(),promise ...
- ssh-agent - 认证代理
总览 (SYNOPSIS) ssh-agent [-a bind_address ] [-c | -s ] [-t life ] [-d ] [command [args ... ] ] ssh-ag ...
- 93-基于ATOM E3825的3U PXIe 主板控制器
基于ATOM E3825的3U PXIe 主板控制器 一.板卡概述: 本主板采用intel ATOM 处理器 E3825 设计主板控制器,是一种低成本.低功耗解决方案.板卡采用Intel Bay Tr ...
- 解决图片插入word文档后清晰度降低的问题
解决图片插入word文档后清晰度降低的问题 在默认情况下,word程序会自动压缩插入word文档中的图片以减小整个word文档的.当我们需要插入word文档中的图片保持原始清晰度时,可以通过设置wor ...
- shell --08 例子
目录 1.按照时间生成文件2018-05-22.log将每天的磁盘使用状态写入到对应日期的文件 [root@web01 ceshi]# cat 1.sh #!/bin/bash Date=`date ...
- Codeforces Round #393 (Div. 2) - C
题目链接:http://codeforces.com/contest/760/problem/C 题意:有n个烤串,并且每个烤串起初都放在一个火盆上并且烤串都正面朝上,现在定义p序列,p[i]表示在i ...
- Switch能否用String类型做参数?
switch(expr): 其中,expr参数可以是一个枚举常量(由整型或字符类型实现)或一个整数表达式,其中整数表达式可以是基本类型int或其包装类Integer.由于byte.short和char ...
- Linux数据库还原备份
Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDBHotbackup的一个很好的替代品. https://www.percon ...
- pwn的一些技巧与总结
原文地址:https://github.com/Naetw/CTF-pwn-tips 目录 溢出 在gdb中寻找字符串 二进制服务 找到libc中特定函数的偏移地址 Find '/bin/sh' or ...
- jmeter函数 助手
Jmeter(一)——函数助手 __char:把一组数字转化成Unicode __counter:计数器,支持多线程(可以理解为多用户) 线程组设置为2个用户,循环两次${__counter(true ...