WebClient DownloadFile problème de téléchargement d'un fichier avec une requête paramétrée
Bonjour à tous,
Je souhaite utiliser la méthode DownloadFile de la classe WebClient (System.Net).
Le problème est que la méthode me retourne le code de la page HTML dans le chemin spécifié en paramètre.
L'URL est la suivante : http://xxx.com/tc_fichier.php?name=InternalID.csv
le code que j'utilise est le suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| Try
Using MyWebClient As New CookieWebClient()' CookieWebClient hérite de WebClient mais en plus récupère le cookie
MyWebClient.Headers(HttpRequestHeader.UserAgent) = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0"
MyWebClient.Headers(HttpRequestHeader.Accept) = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
MyWebClient.Headers(HttpRequestHeader.AcceptEncoding) = "gzip, deflate"
MyWebClient.Headers(HttpRequestHeader.Host) = "xxx.com"
'Proxy
Dim MyProxy As WebProxy = New WebProxy("http.zzz.com", 80)
MyProxy.Credentials = New System.Net.NetworkCredential(My.User.Name.Split(Convert.ToChar("\"))(1), MyBase.TextBox_Password.Text)
MyProxy.BypassProxyOnLocal = True
MyWebClient.Proxy = MyProxy
'Requête paramétrée
Dim MyQueryStringCollection As New NameValueCollection()
MyQueryStringCollection.Add("name", "internalID.csv")
MyWebClient.BaseAddress = m_BaseUrl ' m_BaseUrl est : <a href="http://xxx.com/tc_fichier.php" target="_blank">http://xxx.com/tc_fichier.php</a>
'MyWebClient.QueryString = MyQueryStringCollection
'
Dim MyCredentialCache As CredentialCache = New CredentialCache
MyCredentialCache.Add(New Uri(m_BaseUrl), "Basic", New NetworkCredential(My.User.Name, MyBase.TextBox_Password.Text))
MyWebClient.Credentials = MyCredentialCache
Try
MyWebClient.DownloadFile(Url, "c:\internalID.csv")' Url est <a href="http://xxx.com/tc_fichier.php" target="_blank">http://xxx.com/tc_fichier.php</a>
Catch ex As WebException
MessageBox.Show(ex.Message, "Http", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Using
Catch ex As WebException
MessageBox.Show(ex.Message, "Http", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
Cursor = Cursors.Default
End Try
Public Class CookieWebClient
Inherits WebClient
Public Property CookieContainer() As CookieContainer
Get
Return m_CookieContainer
End Get
Private Set(value As CookieContainer)
m_CookieContainer = value
End Set
End Property
Private m_CookieContainer As CookieContainer
''' <summary>
''' This will instanciate an internal CookieContainer.
''' </summary>
Public Sub New()
Me.CookieContainer = New CookieContainer()
End Sub
''' <summary>
''' Use this if you want to control the CookieContainer outside this class.
''' </summary>
Public Sub New(cookieContainer As CookieContainer)
Me.CookieContainer = cookieContainer
End Sub
Protected Overrides Function GetWebRequest(address As Uri) As WebRequest
Dim request = TryCast(MyBase.GetWebRequest(address), HttpWebRequest)
If request Is Nothing Then
Return MyBase.GetWebRequest(address)
End If
request.CookieContainer = CookieContainer
Return request
End Function
End Class |
quelqu'un connait-il le bon paramétrage de la classe webclient pour ce cas ?
Merci par avance pour vos réponses.