Howto: Read Attachments - Webdav: System.Net.HttpWebRequest vs System.Net.WebClient
I’ve run into issues with using a GET to read large attachments using HttpWebRequest. Because of this, I use System.Net.WebClient instead. Big attachments will have an error thrown back with HttpWebRequest, however it’s possible to GET the smaller ones. So, since System.Net.WebClient seems to work just as good, I use it for getting attachments. I use HttpWebRequest for everything else.
Public Function SaveAttachmentToFile(ByVal sUri As String, ByVal sFileName As String) As Boolean
Dim bRet As Boolean = False
Dim wcClient As New System.Net.WebClient
Dim myUri As System.Uri
Dim sErrorText As String = ""
Try
myUri = New System.Uri(sUri)
Catch exa As Exception
sErrorText = exa.ToString
MsgBox(sErrorText)
Return False
End Try
'Try
'--------------------------- Set Credentials --------------------------------------
Dim myCred As NetworkCredential
Dim MyCredentialCache As CredentialCache
Select Case m_Connection.AuthenticationType
Case AuthenticationTypes.Basic
If m_Connection.Domain.Trim.Length = 0 Then
myCred = New NetworkCredential(m_Connection.UserName, m_Connection.Password)
Else
myCred = New NetworkCredential(m_Connection.UserName, m_Connection.Password, m_Connection.Domain)
End If
MyCredentialCache = New CredentialCache
MyCredentialCache.Add(myUri, "Basic", myCred)
wcClient.Credentials = MyCredentialCache
Case AuthenticationTypes.NTLM
If m_Connection.Domain.Trim.Length = 0 Then
myCred = New NetworkCredential(m_Connection.UserName, m_Connection.Password)
Else
myCred = New NetworkCredential(m_Connection.UserName, m_Connection.Password, m_Connection.Domain)
End If
MyCredentialCache = New CredentialCache
MyCredentialCache.Add(myUri, "NTLM", myCred)
wcClient.Credentials = MyCredentialCache
Case AuthenticationTypes.Windows
wcClient.Credentials = CredentialCache.DefaultCredentials
Case AuthenticationTypes.FBA
Dim strPassedCookies As String
If m_Connection.strReusableCookies.Trim.Length = 0 Then
strPassedCookies = m_Connection.FBACookies() ' Get new cookies
If strPassedCookies = "Error" Then
' had an error getting the cookie!
strPassedCookies = "" ' Reset
m_Connection.strReusableCookies = "" ' Reset
Return ""
Else
' No errors getting the cookie
m_Connection.strReusableCookies = strPassedCookies ' Use New Cookies
End If
Else
strPassedCookies = m_Connection.strReusableCookies ' Use Existing cookies
End If
wcClient.Headers.Add("Cookie", strPassedCookies)
Case AuthenticationTypes.Certificate
MsgBox(m_Connection.AuthenticationType.ToString & " Not Implemented as yet")
Case AuthenticationTypes.None
'MsgBox(m_Connection.AuthenticationType.ToString & " Unknown authentication type")
End Select
'-------------------- Set Additional Headers if needed (From Connect screen)------------------
If m_Connection.ConnectAdditionalHeaders.AnAdditionalHeaderIsSet = True Then
SetAdditionalConnectionHeaders(wcClient)
End If
' Now Download the file!!!
Try
wcClient.DownloadFile(sUri, sFileName)
bRet = True
Catch ex As Exception
sErrorText = ex.ToString
MsgBox(sErrorText)
bRet = False
End Try
Return bRet
End Function
Read Attachments - Webdav: System.Net.HttpWebRequest vs System.Net.WebClient
Comments
Anonymous
August 14, 2008
PingBack from http://housesfunnywallpaper.cn/?p=742Anonymous
August 06, 2010
Have you test the code? What is m_Connection type? I did not see the declaration of that variable :(