您的位置:首页 > 理论基础 > 计算机网络

#.net 中的 HttpWebRequest 和 HttpWebResponse 类发送客户端证书

2011-11-10 11:44 417 查看
当在 Web 服务器需要一个使用 HttpWebRequestHttpWebResponse 类时,您可以发送客户端证书。若要获取可用于通过使用
HttpWebRequest 类发送客户端证书的证书,使用下列方法之一:

方法 1

使用 x509 证书 类来读取该证书从一个.cer 文件,然后设置 ClientCertificates 属性。

方法 2

使用 CryptoAPI 调用来获得证书从证书存储区,然后将 x509 证书 类设置为您接收到来自证书存储区的证书。然后,您可以设置
ClientCertificates 属性。

发送客户端证书的要求

在使用 ASP.NET 应用程序,请确保完成以下要求:

LOCAL_MACHINE 注册表配置单元中,而不是在 CURRENT_USER 注册表配置单元,必须安装客户端证书。若要确认客户端证书的安装位置,请按照下列步骤操作:

单击 开始、 单击 运行,键入
mmc,然后单击 确定
文件 菜单上单击 添加/删除管理单元
添加/删除管理单元 对话框中单击 添加
添加独立管理单元 对话框中单击 证书,然后单击
添加
证书管理单元 对话框中单击 计算机帐户,然后单击
下一步
选择计算机 对话框中单击 完成
添加独立管理单元 对话框中单击 关闭,然后单击
确定
展开 证书 (本地计算机),展开 个人,然后单击
证书

在右窗格中应列出该客户端证书。
您必须授予 ASP.NET 用户私人密钥以便客户端证书的帐户权限。若要为 ASP.NET 用户帐户权限的客户端证书的私钥,使用 $ WinHttpCertCfg.exe 工具。有关详细的信息请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
823193
(http://support.microsoft.com/kb/823193/ ) 如何获得 Windows HTTP 5.1 证书和跟踪工具

有关如何使用此工具,请访问下面的 Microsoft 开发人员网络 (MSDN) 的网站的详细信息:
WinHttpCertCfg.exe,证书配置工具 http://msdn2.microsoft.com/en-us/library/aa384088.aspx (http://msdn2.microsoft.com/en-us/library/aa384088.aspx)

使用.cer 文件

方法 1 会更易于使用,但方法要求您拥有一个.cer 文件。如果您没有安装的.cer 文件,使用 Microsoft Internet 资源管理器导出.cer 文件。

下面的源代码介绍如何获取证书的.cer 文件可以使用与 HttpWebRequest class.

//Uncomment the following code if you need a proxy. The boolean true is used to bypass the local address.

//WebProxy proxyObject = new WebProxy("Your Proxy value",true);

//GlobalProxySelection.Select = proxyObject;

// Obtain the certificate.

try

{

//You must change the path to point to your .cer file location.

X509Certificate Cert = X509Certificate.CreateFromCertFile("C:\\mycert.cer");

// Handle any certificate errors on the certificate from the server.

ServicePointManager.CertificatePolicy = new CertPolicy();

// You must change the URL to point to your Web server.

HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://YourServer/sample.asp");

Request.ClientCertificates.Add(Cert);

Request.UserAgent = "Client Cert Sample";

Request.Method = "GET";

HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

// Print the repsonse headers.

Console.WriteLine("{0}",Response.Headers);

Console.WriteLine();

// Get the certificate data.

StreamReader sr = new StreamReader(Response.GetResponseStream(), Encoding.Default);

int count;

char [] ReadBuf = new char[1024];

do

{

count = sr.Read(ReadBuf, 0, 1024);

if (0 != count)

{

Console.WriteLine(new string(ReadBuf));

}

}while(count > 0);

}

catch(Exception e)

{

Console.WriteLine(e.Message);

}

//Implement the ICertificatePolicy interface.

class CertPolicy: ICertificatePolicy

{

public bool CheckValidationResult(ServicePoint srvPoint,

X509Certificate certificate, WebRequest request, int certificateProblem)

{

// You can do your own certificate checking.

// You can obtain the error values from WinError.h.

// Return true so that any certificate will work with this sample.

return true;

}

}

使用 CryptoAPI 调用

如果您必须获取该证书从证书存储区,CryptoAPI 函数用于获取该的证书,然后将其存储在 x509 证书 的类对象。X509CertificateCollection 类枚举存储区中的所有证书,然后将其置于
X509CertificateCollection 类对象中。

如果您要获取某个特定证书,必须更改类代码使用 CertFindCertificateInStore 函数获取特定的证书。此函数被声明 Wincrypt.h 文件中。 或者,您可以枚举
X509CertificateCollection 函数来查找所需的证书。

下面的代码示例使用从 CertEnumCertificatesInStore 函数返回的集合中的第一个证书

using System;

using System.Net;

using System.IO;

using System.Text;

using System.Security.Cryptography;

using System.Security.Cryptography.X509Certificates;

using System.Runtime.InteropServices;

namespace SelectClientCert

{

/// Sample that describes how how to select client cetificate and send it to the server.

class MyCerts{

private static int CERT_STORE_PROV_SYSTEM = 10;

private static int CERT_SYSTEM_STORE_CURRENT_USER = (1 << 16);

///private static int CERT_SYSTEM_STORE_LOCAL_MACHINE = (2 << 16);

[DllImport("CRYPT32", EntryPoint="CertOpenStore", CharSet=CharSet.Unicode, SetLastError=true)]

public static extern IntPtr CertOpenStore(

int storeProvider, int encodingType,

int hcryptProv, int flags, string pvPara);

[DllImport("CRYPT32", EntryPoint="CertEnumCertificatesInStore", CharSet=CharSet.Unicode, SetLastError=true)]

public static extern IntPtr CertEnumCertificatesInStore(

IntPtr storeProvider,

IntPtr prevCertContext);

[DllImport("CRYPT32", EntryPoint="CertCloseStore", CharSet=CharSet.Unicode, SetLastError=true)]

public static extern bool CertCloseStore(

IntPtr storeProvider,

int flags);

X509CertificateCollection m_certs;

public MyCerts(){

m_certs = new X509CertificateCollection();

}

public int Init()

{

IntPtr storeHandle;

storeHandle = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_CURRENT_USER, "MY");

IntPtr currentCertContext;

currentCertContext = CertEnumCertificatesInStore(storeHandle, (IntPtr)0);

int i = 0;

while (currentCertContext != (IntPtr)0)

{

m_certs.Insert(i++, new X509Certificate(currentCertContext));

currentCertContext = CertEnumCertificatesInStore(storeHandle, currentCertContext);

}

CertCloseStore(storeHandle, 0);

return m_certs.Count;

}

public X509Certificate this [int index]

{

get

{

// Check the index limits.

if (index < 0 || index > m_certs.Count)

return null;

else

return m_certs[index];

}

}

};

class MyHttpResource

{

String m_url;

public MyHttpResource(string url){

m_url = url;

}

public void GetFile(){

HttpWebResponse result = null;

try{

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m_url);

req.Credentials = CredentialCache.DefaultCredentials;

///Method1

//req.ClientCertificates.Add(X509Certificate.CreateFromCertFile("D:\\Temp\\cert\\c1.cer"));

///Method2

///Uses interop services

MyCerts mycert = new MyCerts();

if(mycert.Init() > 0)

req.ClientCertificates.Add(mycert[0]);

result = (HttpWebResponse)req.GetResponse();

Stream ReceiveStream = result.GetResponseStream();

Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

StreamReader sr = new StreamReader( ReceiveStream, encode );

Console.WriteLine("\r\nResponse stream received");

Char[] read = new Char[256];

int count = sr.Read( read, 0, 256 );

Console.WriteLine("HTTP Response...\r\n");

while (count > 0)

{

String str = new String(read, 0, count);

Console.Write(str);

count = sr.Read(read, 0, 256);

}

}

catch(WebException e)

{

Console.WriteLine("\r\nError:");

#if (DEBUG)

Console.WriteLine(e.ToString());

#else

Console.WriteLine(e.Message);

#endif

}

finally

{

if ( result != null ) {

result.Close();

}

}

}

}

class CertSample

{

static void Main(string[] args)

{

try

{

if (args.Length < 1)

{

Console.WriteLine("No url is entered to download, returning.\n");

Console.WriteLine("Usage: CertSample <urltoget>\n");

Console.WriteLine(" e.g: CertSample https://servername \n");

return;

}

MyHttpResource hr = new MyHttpResource(args[0]);

hr.GetFile();

}

catch(Exception e)

{

Console.WriteLine(e.ToString());

}

return;

}

}

}

有关更多的信息,请访问下面的 Microsoft 开发人员网络 (MSDN) 的网站:

x509 证书类

http://msdn2.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate(vs.71).aspx)

平台 SDK: 加密

http://msdn2.microsoft.com/en-us/library/aa380255.aspx

posted @ 2011-04-23 23:51 爱的angell 阅读(106) 评论(1)
编辑
HttpWebRequest解析

摘自:http://apps.hi.baidu.com/share/detail/16502796

HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择。它们支持一系列有用的属性。这两个类位 于System.Net命名空间,默认情况下这个类对于控制台程序来说是可访问的。请注意,HttpWebRequest对象不是利用new关键字通过构 造函数来创建的,而是利用工厂机制(factory mechanism)通过Create()方法来创建的。另外,你可能预计需要显式地调用一个“Send”方法,实际上不需要。接下来调用 HttpWebRequest.GetResponse()方法返回的是一个HttpWebResponse对象。你可以把HTTP响应的数据流
(stream)绑定到一个StreamReader对象,然后就可以通过ReadToEnd()方法把整个HTTP响应作为一个字符串取回。也可以通过 StreamReader.ReadLine()方法逐行取回HTTP响应的内容。

这种技术展示了如何限制请求重定向(request redirections)的次数, 并且设置了一个超时限制。下面是HttpWebRequest的一些属性,这些属性对于轻量级的自动化测试程序是非常重要的。
l AllowAutoRedirect:获取或设置一个值,该值指示请求是否应跟随重定向响应。
l CookieContainer:获取或设置与此请求关联的cookie。
l Credentials:获取或设置请求的身份验证信息。
l KeepAlive:获取或设置一个值,该值指示是否与 Internet 资源建立持久性连接。
l MaximumAutomaticRedirections:获取或设置请求将跟随的重定向的最大数目。
l Proxy:获取或设置请求的代理信息。
l SendChunked:获取或设置一个值,该值指示是否将数据分段发送到 Internet 资源。
l Timeout:获取或设置请求的超时值。
l UserAgent:获取或设置 User-agent HTTP 标头的值

C# HttpWebRequest提交数据方式其实就是GET和POST两种,那么具体的实现以及操作注意事项是什么呢?那么本文就向你详细介绍C# HttpWebRequest提交数据方式的这两种利器。

C# HttpWebRequest提交数据方式学习之前我们先来看看什么是HttpWebRequest,它是 .net 基类库中的一个类,在命名空间 System.Net 下面,用来使用户通过HTTP协议和服务器交互。

C# HttpWebRequest的作用:

HttpWebRequest对HTTP协议进行了完整的封装,对HTTP协议中的 Header, Content, Cookie 都做了属性和方法的支持,很容易就能编写出一个模拟浏览器自动登录的程序。

C# HttpWebRequest提交数据方式:

程序使用HTTP协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成,下面对这两种方式进行一下说明:

C# HttpWebRequest提交数据方式1. GET 方式。

GET 方式通过在网络地址附加参数来完成数据的提交,比如在地址 http://www.google.com/webhp?hl=zh-CN 中,前面部分 http://www.google.com/webhp 表示数据提交的网址,后面部分 hl=zh-CN 表示附加的参数,其中 hl 表示一个键(key), zh-CN 表示这个键对应的值(value)。程序代码如下:

HttpWebRequest req =
(HttpWebRequest) HttpWebRequest.Create(
"http://www.google.com/webhp?hl=zh-CN" );
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
}

C# HttpWebRequest提交数据方式2. POST 方式。

POST 方式通过在页面内容中填写参数的方法来完成数据的提交,参数的格式和 GET 方式一样,是类似于 hl=zh-CN&newwindow=1 这样的结构。程序代码如下:

string param = "hl=zh-CN&newwindow=1";
byte[] bs = Encoding.ASCII.GetBytes(param);

HttpWebRequest req =
(HttpWebRequest) HttpWebRequest.Create(
"http://www.google.com/intl/zh-CN/" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;

using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
}

在上面的代码中,我们访问了 http://www.google.com/ 的网址,分别以 GET 和 POST 方式提交了数据,并接收了返回的页面内容。然而,如果提交的参数中含有中文,那么这样的处理是不够的,需要对其进行编码,让对方网站能够识别。

C# HttpWebRequest提交数据方式3. 使用 GET 方式提交中文数据。

GET 方式通过在网络地址中附加参数来完成数据提交,对于中文的编码,常用的有 gb2312 和 utf8 两种,用 gb2312 方式编码访问的程序代码如下:

Encoding myEncoding = Encoding.GetEncoding("gb2312");
string address = "http://www.baidu.com/s?"
+ HttpUtility.UrlEncode("参数一", myEncoding) +
"=" + HttpUtility.UrlEncode("值一", myEncoding);
HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create(address);
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
}

在上面的程序代码中,我们以 GET 方式访问了网址 http://www.baidu.com/s ,传递了参数“参数一=值一”,由于无法告知对方提交数据的编码类型,所以编码方式要以对方的网站为标准。常见的网站中,
http://www.baidu.com/ (百度)的编码方式是 gb2312, http://www.google.com/ (谷歌)的编码方式是 utf8。

C# HttpWebRequest提交数据方式4. 使用 POST 方式提交中文数据。

POST 方式通过在页面内容中填写参数的方法来完成数据的提交,由于提交的参数中可以说明使用的编码方式,所以理论上能获得更大的兼容性。用 gb2312 方式编码访问的程序代码如下:

Encoding myEncoding = Encoding.GetEncoding("gb2312");
string param =
HttpUtility.UrlEncode("参数一", myEncoding) +
"=" + HttpUtility.UrlEncode("值一", myEncoding) +
"&" + HttpUtility.UrlEncode("参数二", myEncoding) +
"=" + HttpUtility.UrlEncode("值二", myEncoding);

byte[] postBytes = Encoding.ASCII.GetBytes(param);

HttpWebRequest req = (HttpWebRequest)
HttpWebRequest.Create( "http://www.baidu.com/s" );
req.Method = "POST";
req.ContentType =
"application/x-www-form-urlencoded;charset=gb2312";
req.ContentLength = postBytes.Length;

using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
}

从上面的代码可以看出, POST 中文数据的时候,先使用 UrlEncode 方法将中文字符转换为编码后的 ASCII 码,然后提交到服务器,提交的时候可以说明编码的方式,用来使对方服务器能够正确的解析。

以上列出了客户端程序使用HTTP协议与服务器交互的情况,常用的是 GET 和 POST 方式。现在流行的 WebService 也是通过 HTTP 协议来交互的,使用的是 POST 方法。与以上稍有所不同的是, WebService 提交的数据内容和接收到的数据内容都是使用了 XML 方式编码。所以, HttpWebRequest 也可以使用在调用 WebService 的情况下。

C# HttpWebRequest提交数据方式的基本内容就向你介绍到这里,希望对你了解和学习C# HttpWebRequest提交数据方式有所帮助
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐