zt:Consuming Webservices over HTTPS (SSL)

services|web

When Webservices are used, a common concern is security: SOAP messages are transferred in plain text over the network, so anyone with a sniffer could intercept the SOAP message and read it. In my opinion this could happen also to binary data, but probably it requires a little bit more hacker skills. So a solution is to use HTTPS (SSL) instead of HTTP, so the communication is encrypted. To accomplish this, you need to get and install a certificate (issued by a Certificate Authority) on your webserver. In a production environment you would buy a certificate from Verisign or another well known CA, or you would install your own CA, which is a component of Windows Server. If you only want to play with HTTPS, SSL and certificates or your project is in the development phase, you can also generate a test certificate using the MakeCert.exe tool (included in the .NET Framework SDK). After that you have to add this certificate to a website in IIS, and set a port which HTTPS should use.

When you browse to a HTTPS site, you probably get a dialog window asking you if you want to trust the certificate provided by the webserver. So the responsibility of accepting the certificate is handled by the user. Let's get back to the webservice scenario, if you want to invoke a webservice located on a webserver which uses SSL and HTTPS there is a problem. When you make the call from code, there is no dialog window popping up, and asking if you trust the certificate (luckily because this would be pretty ugly in server-side scenarios); probably you'll get following exception:
An unhandled exception of type 'System.Net.WebException' occurred in system.dll

Additional information: The underlying connection was closed: Could not establish trust relationship with remote server.

But there is a solution for this problem, you can solve this in your code by creating your own CertificatePolicy class (which implements the ICertificatePolicy interface). In this class you will have to write your own CheckValidationResult function that has to return true or false, like you would press yes or no in the dialog window. For development purposes I've created the following class which accepts all certificates, so you won't get the nasty WebException anymore:
public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
{
public TrustAllCertificatePolicy()
{}

public bool CheckValidationResult(ServicePoint sp,
X509Certificate cert,WebRequest req, int problem)
{
return true;
}
}

As you can see the CheckValidationResult function always returns true, so all certificates will be trusted. If you want to make this class a little bit more secure, you can add additional checks using the X509Certificate parameter for example. To use this CertificatePolicy, you'll have to tell the ServicePointManager to use it:
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
This must be done (one time during the application life cycle) before making the call to your webservice.

时间: 2024-09-15 20:20:29

zt:Consuming Webservices over HTTPS (SSL)的相关文章

ssl 配置 mq后 ,导致webservices 客户端https 请求证书错误

问题描述 ssl 配置 mq后 ,导致webservices 客户端https 请求证书错误 active mq ssl配置后,导致axis2生成的webservice 客户端https 请求报错

HTTPS/SSL简介及与HTTP的不同之处

摘要: 8月7日谷歌在其官方博客上宣布他们将HTTPS/SSL纳入其搜索算法机制中,采用HTTPS/SSL安全认证的网站将会被谷歌给予更多的 信任,从而有利于网站在谷歌搜索结果中的排名提升.但是目前8月7日谷歌在其官方博客上宣布他们将HTTPS/SSL纳入其搜索算法机制中,采用HTTPS/SSL安全认证的网站将会被谷歌给予更多的 信任,从而有利于网站在谷歌搜索结果中的排名提升.但是目前HTTPS/SSL对于谷歌网站排名的影响因素非常有限,只是其几百个排名因素中非常微弱的一 个.该HTTPS/SS

SharePoint 2013 配置HTTPS(SSL)

1.打开服务器上的IIS,找到服务器证书,如下图: 2.进入服务器证书,点击右侧操作"创建自签名证书",如下图: 3.为证书指定一个好记名称,存储选择个人,如下图: 4.点击确定以后,服务器证书页面,多出了我们新建的证书,如下图: 5.双击证书,进入详细信息页面,点击复制到文件,如下图: 6.进入证书导出向导,点击下一步,如下图: 7.选择"不,不要导出私钥",点击下一步,如下图: 8.选择"DER 编码二进制",点击下一步,如下图: 9.选择指

浅谈https\ssl\数字证书,互联网营销

在互联网安全通信方式上,目前用的最多的就是https配合ssl和数字证书来保证传输和认证安全了.本文追本溯源围绕这个模式谈一谈. 名词解释 首先解释一下上面的几个名词: https:在http(超文本传输协议)基础上提出的一种安全的http协议,因此可以称为安全的超文本传输协议.http协议直接放置在TCP协议之上,而https提出在http和TCP中间加上一层加密层.从发送端看,这一层负责把http的内容加密后送到下层的TCP,从接收方看,这一层负责将TCP送来的数据解密还原成http的内容.

iOS访问https ssl和tls双向加密

文章已经移到此处:http://www.henishuo.com/ios-https-tls-ssl/ 关于https和ssl的原理,请到此处查看:http://blog.163.com/magicc_love/blog/static/185853662201321423527263/ 由于项目需求,访问服务是https的,并且使用的是ssl加密方式 下面说明使用MKNetworkit网络库实现的代码: - (void)testClientCertificate { SecIdentityRef

Android or java https ssl exception

详细分析Android及Java中访问https请求exception(SSLHandshakeException, SSLPeerUnverifiedException)的原因及解决方法.1.现象 用Android(或Java)测试程序访问下面两个链接. https链接一:web服务器为jetty,后台语言为java. https链接二:web服务器为nginx,后台语言为php. 链接一能正常访问,访问链接二报异常,且用HttpURLConnection和apache的HttpClient两

Nginx的http和https(ssl)共存配置方法

因为公司项目有需要用到https的链接,就去弄了个证书,配置上之后,https可以正常访问,但是之前的http的都是显示400错误,nginx的配置如下:  代码如下 复制代码 server {             listen 80 default backlog=2048;             listen 443;             server_name 111cn.net;             root /var/www/html;             ssl o

windows环境下用squid代理https(ssl)的方法_win服务器

这个其实跟配置http代理也差不多,我之前是因为没有使用支持ssl的squid版本,所以反复折腾都不见效! 首先得检查你正在使用的squid版本是否是支持ssl的,主要看squid\sbin\ssleay32.dll文件是否存在,如果没有此文件那就得去换个支持ssl的squid版本了. 我这里就只提供个最简单的例子: 复制代码 代码如下: acl all src 0.0.0.0/0.0.0.0 acl manager proto cache_object acl localhost src 12

Axis 1 https(SSL) client 证书验证错误ValidatorException workaround

Axis 1.x 编写的client在测试https的webservice的时候, 由于client 代码建立SSL连接的时候没有对truststore进行设置,在与https部署的webservice 连接会在运行时报出:  javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpat