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 的获取与使用的更多相关文章

  1. iis 访问网站需要进行身份验证

    今天网站输入域名访问的时候提示需要输入账号密码,这是权限出了问题,百度了一下,解决了,分享一下: 1.登陆远程,右键我的电脑->管理->本地用户和组->用户,里面有一个IUSR_WD ...

  2. ASP.NET Web API 2基于令牌的身份验证

    基于令牌的认证 我们知道WEB网站的身份验证一般通过session或者cookie完成的,登录成功后客户端发送的任何请求都带上cookie,服务端根据客户端发送来的cookie来识别用户. WEB A ...

  3. ASP.NET身份验证

    Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验 证用的最多,也最灵活. Forms 验证方式对基于用户的验证 ...

  4. Asp.net 身份验证

    Forms 验证方式对基于用户的验证授权提供了很好的支持,可以通过一个登录页面验证用户的身份,将此用户的身份发回到客户端的Cookie,之后此用户再访问这个 web应用就会连同这个身份Cookie一起 ...

  5. ASP.NET中身份验证的三种方法

    Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验证用的最多,也最灵活.Forms 验证方式对基于用户的验证授权 ...

  6. asp.net中常用的几种身份验证方式

    转载:http://www.cnblogs.com/dinglang/archive/2012/06/03/2532664.html   前言 在B/S系统开发中,经常需要使用"身份验证&q ...

  7. Web服务器使用基于纯文本表单的身份验证——.net(未完待续)

    asp.net 表单验证方式 Asp.net的身份验证有有三种,分别是"Windows | Forms| Passport",其中又以Forms验证用的最多,也最灵活. 根据实际需 ...

  8. 客官,来看看AspNetCore的身份验证吧

    开篇 这段时间潜水了太久,终于有时间可以更新一篇文章了. 通过本篇文章您将Get: Http的一些身份验证概念 在AspNetCore中实现身份验证方案 JWT等概念的基础知识 使用Bearer To ...

  9. 添加AD验证(域身份验证)到现有网站

    每个网站几乎都会有用户登录的模块,登录就会涉及到身份验证的过程.通常的做法是在页面上有个登录的Form,然后根据用户名和密码到数据库中去进行验证. 而验证后如何在网站的各个页面维持这种认证过的状态,有 ...

随机推荐

  1. JavaScript—— 案例:表单验证

    QQ号:<input type="text" id="txtQQ"><span></span><br> 邮箱:& ...

  2. 安卓构架组件——概述 Android Architecture Components

    谷歌官文文档地址:https://developer.android.google.cn/topic/libraries/architecture 安卓构架组建是库的集合:帮助你设计健壮的.易测试的. ...

  3. 使用transporter同步MongoDB数据到es

    由于logstash不支持MongoDB自定义主键导入es,所以使用transporter导入数据. 版本:es5.x,transporter0.25, es6以上不允许一个索引下面多个type,tr ...

  4. VS基本教程

    首先打开vs,点击工具栏,文件----新建----项目 然后依次选择Visual C++----空项目-----命名---项目存放路径 接下来在解决方案资源管理器中选中源文件,右键----添加---- ...

  5. 再读js正则表达式

    正则表达式定义 在js中有两种方式来定义正则表达式, 第一种是类似perl的语法来定义一个正则表达式,我们把它叫做正则表达式字面量法: var expression = /pattern/flag 其 ...

  6. java this的应用

    package java04; /* * 当方法的局部变量和类的成员变量重名时,会根据“就近原则”,优先使用局部变量. * 如果需要访问奔雷中的成员变量,需要使用格式: this.成员变量名 * * ...

  7. Go的学习 append的使用

    1. package main; import "fmt" func test () { ],,,,}; s:=a[:] fmt.Printf(]) s=append(s,); s ...

  8. Struts2基础-2 -实现Action接口创建Action控制器

    1.新建一个web项目,目录结构如下,添加jar包到lib文件夹里,并把jar包add 到 buildpath里面 2.web.xml配置 struts2的过滤器类:StrutsPrepareAndE ...

  9. _stdcall

    __cdecl __fastcall与__stdcall,三者都是调用约定(Calling convention),它决定以下内容:1)函数参数的压栈顺序,2)由调用者还是被调用者把参数弹出栈,3)以 ...

  10. php strtoupper()函数 语法

    php strtoupper()函数 语法 作用:把所有字符转换为大写 语法:strtoupper(string) 参数: 参数 描述 string 必须,规定要转换的字符串 说明:strtouppe ...