Click here to Skip to main content
15,885,063 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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


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


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)

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. . .
Posted
Updated 12-Jun-11 20:28pm
v4
Comments
Sergey Alexandrovich Kryukov 9-Jun-11 11:32am    
Can you get this JPG file using a Web browser? Can you get any other file from any other site using your code? Just asking...
--SA
hansoctantan 10-Jun-11 2:03am    
yes, when I logged in I can download the image. Same happen when no user is logged in, I can view the image but when I save the image 0 byte is downloaded.
Dave Kreskowiak 13-Jun-11 9:35am    
Don't post the same question multiple times, especially just 3 hours apart. I deleted your other post on this because this one provided more useful information to diagnose the problem.

1 solution

Use CertificatePolicyHandler code as mentioned below

1) copy the CustomCertificatePolicyHandler class in your code

2) modify your code as shown below

XML
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''''
 
Share this answer
 
v5
Comments
hansoctantan 10-Jun-11 6:39am    
Error. . .
ServicePointManager.CertificatePolicy = objCertificatePolicy
How can I fix it
hitesh_tech 10-Jun-11 12:12pm    
what is the error?
hansoctantan 13-Jun-11 1:57am    
This "ServicePointManager.CertificatePolicy"
Error.....
'Public Shared Property CertificatePolicy() As System.Net.ICertificatePolicy' is obsolete: 'CertificatePolicy is obsolete type, please use ServerClientValidationCallback instead. http://go.microsoft.com/fwlink/linkid=14202'
hitesh_tech 13-Jun-11 6:07am    
you can replace
ServicePointManager.ServerCertificateValidationCallback = objCertificatePolicy.ServerCertificateValidationCallback

with

ServicePointManager.CertificatePolicy = objCertificatePolicy

but if
ServicePointManager.CertificatePolicy = objCertificatePolicy

is allowing you to compile the code then you can ignore the obsolete type error that will work fine.
#realJSOP 10-Jun-11 10:42am    
Moved the start of the pre tag in your answer to be at the beginning of the actual code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900