http client4 访问无效证书的HTTPS网站

0 篇文章 0 订阅

X509TrustManager tm = new X509TrustManager() {

public void checkClientTrusted(X509Certificate[] xcs,

String string) throws CertificateException {

}

 

public void checkServerTrusted(X509Certificate[] xcs,

String string) throws CertificateException {

}

 

public X509Certificate[] getAcceptedIssuers() {

return null;

}

 

};

 

StringBuffer result = new StringBuffer();

try {

HttpClient client = new DefaultHttpClient();

SSLContext ctx = SSLContext.getInstance("TLS");

ctx.init(null, new TrustManager[] { tm }, null);

SSLSocketFactory ssf = new SSLSocketFactory(ctx);

ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

ClientConnectionManager ccm = client.getConnectionManager();

SchemeRegistry sr = ccm.getSchemeRegistry();

sr.register(new Scheme("https", ssf, 443));

HttpClient sslClient = new DefaultHttpClient(ccm,client.getParams());

HttpPost post=new HttpPost(Constant.HOST);

HttpResponse response = sslClient.execute(post)

status = response.getStatusLine().getStatusCode();

HttpEntity entity = response.getEntity();

BufferedReader rd = new BufferedReader(new InputStreamReader(

entity.getContent(), HTTP.UTF_8));

String tempLine = rd.readLine();

while (tempLine != null) { 

result.append(tempLine);

tempLine = rd.readLine();

}

} catch (Exception e) {

throw new ServiceException("请求出错");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: WebClient可以通过以下步骤访问HTTPS: 1. 创建一个WebClient对象。 2. 创建一个ServicePointManager对象,并设置其SecurityProtocol属性为Tls12。 3. 使用WebClient对象的DownloadString方法或DownloadData方法访问HTTPS网站。 示例代码: ``` using System.Net; // 创建WebClient对象 WebClient client = new WebClient(); // 创建ServicePointManager对象,并设置SecurityProtocol属性为Tls12 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // 访问HTTPS网站并获取数据 string result = client.DownloadString("https://www.example.com"); byte[] data = client.DownloadData("https://www.example.com"); ``` 注意:在访问HTTPS网站时,需要确保证书有效并且受信任。如果证书无效或不受信任,将无法访问HTTPS网站。 ### 回答2: # WebClient访问 HTTPS 在使用 WebClient 进行 HTTPS 请求时,需要注意以下几点: 1. 证书信任 为确保请求的安全性,WebClient 会验证 HTTPS 证书。如果证书无效或未知,WebClient 会抛出异常。 为避免抛出异常,可以忽略证书验证,或者手动将证书添加到信任列表中。 以下是忽略证书验证的示例代码: ```csharp ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; ``` 以下是手动添加证书的示例代码: ```csharp var cert = new X509Certificate2("certificate.pfx", "password"); ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { if (certificate.GetCertHashString() == cert.GetCertHashString()) { return true; } return sslPolicyErrors == SslPolicyErrors.None; }; ``` 2. TLS 版本 WebClient 默认使用 TLS 1.0,但有些服务器可能只接受较新的 TLS 版本,因此需要使用较新的 TLS 版本。 以下是设置 TLS 版本的示例代码: ```csharp ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; ``` 3. 代理设置 如果需要通过代理访问 HTTPS 网站,可以设置 WebClient 的 Proxy 属性。 以下是设置代理的示例代码: ```csharp var proxy = new WebProxy("http://proxy.example.com", 8080); WebClient client = new WebClient(); client.Proxy = proxy; ``` 4. 响应数据编码 HTTPS 响应的编码可能是 GZIP 或 DEFLATE,需要对响应进行解压缩。 以下是对响应解压缩的示例代码: ```csharp using (var stream = new MemoryStream(client.DownloadData(url))) using (var gzip = new GZipStream(stream, CompressionMode.Decompress)) using (var reader = new StreamReader(gzip, Encoding.UTF8)) { var response = reader.ReadToEnd(); } ``` 总之,在使用 WebClient 访问 HTTPS 时,需要考虑证书信任、TLS 版本、代理设置和响应数据编码等多个方面,以确保请求的安全性和正确性。 ### 回答3: # 如何使用WebClient访问HTTPS URL WebClient是C#中的一个类,它可以用来访问Web资源。如果我们需要访问HTTP,那么使用WebClient是非常简单的,但如果我们需要访问HTTPS,那就要涉及到证书验证、协议处理、密钥协商等问题。在这篇文章中,我将介绍使用C# WebClient访问HTTPS URL的过程。 ## 1. 创建WebClient对象 创建WebClient对象非常简单,只需要使用下面的代码: ```csharp WebClient client = new WebClient(); ``` ## 2. 设置SSL协议 在访问HTTPS URL时,我们需要设置SSL协议,以确保我们的请求能够成功。我们可以通过设置ServicePointManager.SecurityProtocol属性来选择需要使用的协议。例如,下面的代码会将协议设置为TLS 1.2: ```csharp ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; ``` 如果您需要支持TLS 1.1或TLS 1.0,则可以使用: ```csharp ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; ``` ## 3. 添加证书 当我们需要访问HTTPS URL时,我们需要一个安全证书。如果证书无效或未受信任,我们的请求将会被服务器拒绝。我们可以在代码中添加证书,以避免这个问题。 例如,假设您在本地计算机上安装了一个名为“test”的证书,然后通过以下代码将其添加到WebClient对象中: ```csharp X509Certificate cert = new X509Certificate(@"C:\test.cer"); client.Credentials = new NetworkCredential("username", "password"); client.ClientCertificates.Add(cert); ``` ## 4. 请求URL 请求一个HTTPS URL与请求HTTP URL时是相同的,只需要调用DownloadString或DownloadData方法即可。例如: ```csharp string result = client.DownloadString("https://www.baidu.com"); ``` ## 5. 总结 使用C# WebClient访问HTTPS URL可能会遇到一些问题,但是只要正确地设置SSL协议、添加证书和请求URL,我们就能成功地访问HTTPS URL并获取所需的数据。希望这篇文章能够对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值