pourquoi ai-je une exception stack overflow ?
Bonjour,
j'ai fait une petite application pour d'abord me connecter avec mon login et password sur un site et ensuite avec le httpwebrequest, downloader chaque fichier qui commencent tous par le même nom mais avec un id différent, voilà pourquoi j'ai fait une boucle.
par contre après environ 200 fichiers, j'ai toujours une exception stack overflow sur le request.getresponse. Quelqu'un aurait-il une idée pourquoi ?
Voici mon code:
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| Public Class Form1
Dim ix As Int32
Dim urlString As String
Dim filename As String
Dim destinationFile As String
Dim readStream As BinaryReader
Dim ByteBucket() As [Byte]
Private Sub grab()
urlString = "http://www.site.com/script.php?id=" & CStr(ix)
Try
Dim request As HttpWebRequest = HttpWebRequest.Create(urlString)
request.CookieContainer = GetCookieContainer()
Using response As HttpWebResponse = request.GetResponse
filename = response.Headers(7).ToString
Do Until filename.Length > 3
System.Threading.Thread.Sleep(500)
Loop
If filename.Substring(0, 9) = "filename=" And filename.Length > 12 Then
destinationFile = txtSaveFolder.Text.ToString & Split(filename, "=")(1)
Using readStream As New BinaryReader(response.GetResponseStream())
Using fileToWrite As FileStream = New FileStream(destinationFile, FileMode.Create, FileAccess.Write)
Dim Buffer(4096) As Byte, BlockSize As Integer
'Memory stream to store data
Using TempStream As New MemoryStream
Do
BlockSize = readStream.Read(Buffer, 0, 4096)
If BlockSize > 0 Then TempStream.Write(Buffer, 0, BlockSize)
Loop While BlockSize > 0
ByteBucket = TempStream.ToArray
fileToWrite.Write(ByteBucket, 0, ByteBucket.Length)
End Using
End Using
End Using
End If
response.Close()
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
Call GC.Collect()
System.Threading.Thread.Sleep(2000)
If ix < CInt(txtEnd.Text) Then
ix += 1
Call grab()
End If
End Sub
Public Function GetCookieContainer() As CookieContainer
Dim container As New CookieContainer()
For Each cookie As String In webBrowser1.Document.Cookie.Split(";"c)
Dim name As String = cookie.Split("="c)(0)
Dim value As String = cookie.Substring(name.Length + 1)
Dim path As String = "/"
Dim domain As String = ".site.com"
container.Add(New Cookie(name.Trim(), value.Trim(), path, domain))
Next
Return container
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ix = CInt(txtStart.Text)
Call grab()
End Sub
End Class |