Bonjour,
Je n'arrive pas à télécharger la réponse du WebRequest.

Enfaite j'envoi une requête sur un serveur qui va lire les donnés d'un fichier (app, ...) en bytes et qui les envoi; sur un navigateur cela démarre un téléchargement.

Voici ma fonction Request ou j'essaye de télécharger la réponse (la réponse est le code source html et donc dans mon cas la fonction crash)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
   Function _HTTPRequest(ByVal url As String, ByVal post As String) As String
        Dim request As WebRequest = WebRequest.Create(url)
        ' Set the Method property of the request to POST.
        request.Method = "POST"
        ' Create POST data and convert it to a byte array.
        Dim postData As String = post
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        ' Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded"
        ' Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length
        ' Get the request stream.
        Dim dataStream As Stream = request.GetRequestStream()
        ' Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length)
        ' Close the Stream object.
        dataStream.Close()
        Console.Write(byteArray)
        ' Get the response.
        Dim response As WebResponse = request.GetResponse()
        ' Display the status.
        Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
        ' Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.
        Dim reader As New StreamReader(dataStream)
        ' Read the content.
        Dim reponse As String = reader.ReadToEnd()
        ' Display the content.
        Console.WriteLine(reponse)
        ' Clean up the streams.
        reader.Close()
        dataStream.Close()
        response.Close()
 
        Return reponse
    End Function
J'ai donc cherché une fonction pour télécharger la réponse de la requête, j'en ai trouvé une en C# donc je l'ai convertie en VB :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
Function DownloadFile(ByVal remoteFilename As String, ByVal localFilename As String)
        ' Function will return the number of bytes processed
        ' to the caller. Initialize to 0 here.
        Dim bytesProcessed As Integer
 
        ' Assign values to these objects here so that they can
        ' be referenced in the finally block
        Dim remoteStream As Stream
        Dim localStream As Stream
        Dim response As WebResponse
        Dim request
        ' Use a try/catch/finally block as both the WebRequest and Stream
        ' classes throw exceptions upon error
        ' Try
        '{
        '// Create a request for the specified remote file name
        request = WebRequest.Create(remoteFilename)
        If Not request Is Nothing Then
            '// Send the request to the server and retrieve the
            '// WebResponse object
            response = request.GetResponse()
 
            If Not response Is Nothing Then
                '// Once the WebResponse object has been retrieved,
                '// get the stream object associated with the response's data
                remoteStream = response.GetResponseStream()
 
                '// Create the local file
                localStream = File.Create(localFilename)
 
                '// Allocate a 1k buffer
                Dim buffer As Byte() = New Byte(1024) {} 'String(CChar(" "), 1024)
 
                Dim bytesRead As Integer
 
                '// Simple do/while loop to read from stream until
                '// no bytes are returned
                While (bytesRead > 0)
                    '// Read data (up to 1k) from the stream
                    bytesRead = remoteStream.Read(buffer, 0, Len(buffer))
 
                    '// Write the data to the local file
                    localStream.Write(buffer, 0, bytesRead)
 
                    '// Increment total bytes processed
                    bytesProcessed += bytesRead
                End While
            End If
        End If
        'catch(Exception e)
        'Console.WriteLine(e.Message)
        ' Finally
 
        '// Close the response and streams objects here
        '// to make sure they're closed even if an exception
        '// is thrown at some point
        If Not response Is Nothing Then response.Close()
        If Not remoteStream Is Nothing Then remoteStream.Close()
        If Not localStream Is Nothing Then localStream.Close()
 
        '// Return total bytes processed to caller.
        Return bytesProcessed
    End Function
Mais cela n'as rien donné comme cette fonction n'envoi pas de requête post et que j'en ait besoin :/

J'espère que j'ai donné assez de renseignements


Cordialement, d3mon.