日期:2014-05-18 浏览次数:21230 次
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SSLWeb
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string user_name=Request.QueryString["user_name"];
string password = Request.QueryString["password"];
if (user_name == "admin" && password == "admin")
{
Response.Write("OK");
}
else
{
Response.Write("Error");
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://192.168.0.62/Default.aspx");
ServicePointManager.CertificatePolicy = new MyPolicy1();
//创建证书文件
X509Certificate objx509 = new X509Certificate("D:\\zhengshu\\tom_test.cer");
//添加到请求里
request.ClientCertificates.Add(objx509);
//User-AgentHTTP标头的值
request.UserAgent = "Client Cert Sample";
request.Method = "POST";
//我还想Post两个数据过去,同样不知道怎么处理。user_name和password
//读返回的流
StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream());
MessageBox.Show(reader.ReadToEnd());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
public class MyPolicy1 : ICertificatePolicy
{
//这个方法应该是在client和server已经进行了初步验证之后才调用的。
//不能实现我的要求。
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
{
Console.WriteLine( "now CheckValidationResult....... ");
//Return True to force the certificate to be accepted.
return true;
} // end CheckValidationResult
} // class MyPolicy