VB.NET无法连接到HTTPS Web服务器 [英] VB.NET can't connect to a HTTPS Web Server

查看:85
本文介绍了VB.NET无法连接到HTTPS Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于从HTTPS Web服务器下载文件的代码:

I have a code for downloading a file from a HTTPS Web Server:

Dim URI As String = "https://secure.ddcos.com/MOVEitISAPI/MOVEitISAPI.310/action=hu_downld!parm=e930810185440778334!NoAttach=1/98677_5050854223051_942963.jpg"
        Dim oRequest As System.Net.HttpWebRequest = CType(HttpWebRequest.Create(URI), HttpWebRequest)
        oRequest.Credentials = New System.Net.NetworkCredential("username", "password")
        Using oResponse As System.Net.WebResponse = CType(oRequest.GetResponse, System.Net.WebResponse)
            Console.WriteLine("{0}", oResponse.Headers)
            Using responseStream As IO.Stream = oResponse.GetResponseStream
                Using fs As New IO.FileStream("D:\98677_5050854223051_942963.jpg", FileMode.Create, FileAccess.Write)
                    Dim buffer(2047) As Byte
                    Dim read As Integer
                    Do
                        read = responseStream.Read(buffer, 0, buffer.Length)
                        fs.Write(buffer, 0, read)
                    Loop Until read = 0
                    responseStream.Close()
                    fs.Flush()
                    fs.Close()
                End Using
                responseStream.Close()
            End Using
            oResponse.Close()
        End Using
        MsgBox("done")
    End Sub
End Class



Console.WriteLine("{0}",oResponse.Headers)的输出



Output of the Console.WriteLine("{0}", oResponse.Headers)

X-siLock-ErrorCode: 100
X-siLock-ErrorDescription: ISAPI can''t open parameter file
Connection: close
Content-Length: 0
Date: Thu, 09 Jun 2011 14:42:43 GMT
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET



我尝试将其用于不需要任何用户名和密码的其他Web服务器,一切正常.
在上面的代码中,我还尝试了 ClientCertificate 代码:



I try it to other Web Server that doesn''t require any username and password and everything is working alright.
In above code I also try ClientCertificate code:

Dim appPath As String = System.Reflection.Assembly.GetExecutingAssembly. _
        GetModules()(0).FullyQualifiedName()
Dim appDIR As String = System.IO.Path.GetDirectoryName(appPath)
Dim certificate As X509Certificate = X509Certificate.CreateFromCertFile(appDIR & "\Certificates\Base-64 X.509.cer")
oRequest.ClientCertificates.Add(certificate)


我通过以下操作获得证书: http://support.microsoft.com/kb/895971 [ ^ ]
我将证书导入到我的应用程序文件夹(找到EXE的位置)中

另外,我使用 POST 方法,在下载文件之前,我先发送用户名和密码,然后使用 GET 方法.

但是发生了相同的结果
不知道为什么我无法连接到该Web服务器.
任何建议表示赞赏.

谢谢. . .


I get a certificate by following this: http://support.microsoft.com/kb/895971[^]
I import the certificate to my Application Folder(where the EXE is found)

Also I use the POST method, before I download a file I send the username and password first, then I use GET method.

but same result happened
Don''t know why i can''t connect to that Web Server.
Any any suggestion is appreciated.

Thanks. . .

推荐答案

使用CertificatePolicyHandler代码,如下所述

1)在您的代码中复制CustomCertificatePolicyHandler类

2)如下所示修改您的代码

Use CertificatePolicyHandler code as mentioned below

1) copy the CustomCertificatePolicyHandler class in your code

2) modify your code as shown below

Dim oRequest As System.Net.HttpWebRequest = CType(HttpWebRequest.Create(URI), HttpWebRequest)

Dim objCertificatePolicy As New CustomCertificatePolicyHandler
ServicePointManager.CertificatePolicy = objCertificatePolicy

oRequest.Credentials = New System.Net.NetworkCredential("username", "password")











''''''CustomCertificatePolicyHandler''''''''class''''start''''''''

    '''''' <summary>
    '''''' The code allows the client application to accept every certificate that the server provides.
    '''''' and then accepts every request under SSL.
    '''''' Refered From http://support.microsoft.com/kb/823177
    '''''' </summary>
    '''''' <remarks></remarks>
    Public Class CustomCertificatePolicyHandler
        Implements ICertificatePolicy

        Private _ServerCertificateValidationCallback As System.Net.Security.RemoteCertificateValidationCallback

    #Region "Properties"
        '''''' <summary>
        '''''' The code allows the client application to accept every certificate that the server provides.
        '''''' and then accepts every request under SSL.
        '''''' Refered From http://support.microsoft.com/kb/823177
        '''''' </summary>
        '''''' <value></value>
        '''''' <returns></returns>
        '''''' <remarks></remarks>
        Public Overridable ReadOnly Property ServerCertificateValidationCallback() As System.Net.Security.RemoteCertificateValidationCallback
            Get
            Return _ServerCertificateValidationCallback
            End Get
        End Property
    #End Region

    #Region "Methods"
        '''''' <summary>
        '''''' Check Validation Result and allows the client application to accept every certificate that the server provides
        '''''' </summary>
        '''''' <param name="srvPoint"></param>
        '''''' <param name="cert"></param>
        '''''' <param name="request"></param>
        '''''' <param name="certificateProblem"></param>
        '''''' <returns></returns>
        '''''' <remarks></remarks>
        Public Function CheckValidationResult(ByVal srvPoint As ServicePoint, _
                  ByVal cert As X509Certificate, ByVal request As WebRequest, _
                  ByVal certificateProblem As Integer) _
              As Boolean Implements ICertificatePolicy.CheckValidationResult
            ''Return True to allow the certificate to be accepted.
            Return True
        End Function

    #End Region

    #Region "Procedures"
        '''''' <summary>
        '''''' Constructor
        '''''' </summary>
        '''''' <remarks></remarks>
        Public Sub New()
            MyBase.new()
        End Sub

    #End Region
    End Class
''''''CustomCertificatePolicyHandler''''''''class''''End''''''''


这篇关于VB.NET无法连接到HTTPS Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆