Hallo liebe VBler,
ich habe gerade ein Problem und komme absolut nicht weiter. Hoffe von euch hatte schon mal jemand das Problem und kann meinem Abhilfe schaffen
Ich bastel gerade an einem Tool und habe eine Menge Web-Abfragen. Die Prozesse laufen in max. 5 Backgroundworkern plus dem Application Thread. Dabei kommt es anscheinend nicht selten vor das sich die Aufrufe der HTTP-Funktion "überschneiden". Also gleichzeitig von mehreren Threads die HTTP-Funktion aufgerufen wird (ich habe eine zentrale Function für die http-requests).
Im Grunde sind es zwei fehlermeldungen die ich bekomme.
1) Die Anfrage wurde abgebrochen: Die Verbindung wurde unerwartet getrennt..
2) Von der Übertragungsverbindung können keine Daten gelesen werden: Die Verbindung wurde geschlossen.
Es passiert nur sporadisch (schätze mal alle 10-30 aufrufe...) und durch die Backgroundworker nicht wirklich nachzuvollziehen wo genau er den Fehler verursacht.
Hier mein aktueller Code der Function
Habe den code gerade noch einmal geändert und im moment kommt mehr der 2. fehler.
Habe schon versucht mehrere webrequests (Public postReq(100) As HttpWebRequest) zu erstellen, aber das geht nicht. WebResponse und StreamReder macht er, webrequest nicht.
Hat jemand eine Ahnung wie man das Problem lösen kann? Oder was das Problem überhaupt ist? (Meine Vermutung sind 2 gleichzeitige Zugriffe auf das eine "postReq As HttpWebRequest". Aber wie umgehen?
Danke schon mal und viele Grüße
Chaosprogrammierer
ich habe gerade ein Problem und komme absolut nicht weiter. Hoffe von euch hatte schon mal jemand das Problem und kann meinem Abhilfe schaffen
Ich bastel gerade an einem Tool und habe eine Menge Web-Abfragen. Die Prozesse laufen in max. 5 Backgroundworkern plus dem Application Thread. Dabei kommt es anscheinend nicht selten vor das sich die Aufrufe der HTTP-Funktion "überschneiden". Also gleichzeitig von mehreren Threads die HTTP-Funktion aufgerufen wird (ich habe eine zentrale Function für die http-requests).
Im Grunde sind es zwei fehlermeldungen die ich bekomme.
1) Die Anfrage wurde abgebrochen: Die Verbindung wurde unerwartet getrennt..
2) Von der Übertragungsverbindung können keine Daten gelesen werden: Die Verbindung wurde geschlossen.
Es passiert nur sporadisch (schätze mal alle 10-30 aufrufe...) und durch die Backgroundworker nicht wirklich nachzuvollziehen wo genau er den Fehler verursacht.
Hier mein aktueller Code der Function
VB.NET-Quellcode
- Public postresponse(100) As HttpWebResponse
- Public postreqreader(100) As StreamReader
- Public Function SendHTTP(ByVal method As String, ByVal PostString As String, ByVal SendURL As String, ByVal FromURL As String) As String
- Dim postData As String = PostString
- Dim tempCookies As New CookieContainer
- Dim encoding As New UTF8Encoding
- Dim byteData As Byte() = encoding.GetBytes(postData)
- Dim ProxyIP As String = ""
- Dim ProxyPort As Integer = 0
- Try
- 'Debug.Print(SendURL)
- 'search the next free connection
- Dim Iw As Integer = 0
- Do
- If postresponse(Iw) Is Nothing Then
- Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create(SendURL), HttpWebRequest)
- postReq.KeepAlive = True
- postReq.CookieContainer = logincookie
- postReq.ContentType = "application/x-www-form-urlencoded"
- postReq.Referer = FromURL
- postReq.UserAgent = UserAgent
- 'proxy
- If Not ProxyIP = "" And Not ProxyPort = 0 Then
- Dim proxy = New WebProxy(ProxyIP, ProxyPort)
- postReq.Proxy = proxy
- End If
- ' only if post
- If LCase(method) = "post" Then
- postReq.Method = "POST"
- postReq.ContentLength = byteData.Length
- Dim postreqstream As Stream = postReq.GetRequestStream()
- postreqstream.Write(byteData, 0, byteData.Length)
- postreqstream.Close()
- Else
- postReq.Method = "GET"
- End If
- postresponse(Iw) = DirectCast(postReq.GetResponse(), HttpWebResponse)
- Exit Do
- End If
- Iw += 1
- Loop Until Iw > 100
- 'Wait a bit
- Threading.Thread.Sleep(0)
- 'cookie handling
- If Not logincookie Is Nothing Then
- If postresponse(Iw).Cookies.Count > 0 Then
- logincookie.Add(postresponse(Iw).Cookies)
- End If
- End If
- 'logincookie = tempCookies
- postreqreader(Iw) = New StreamReader(postresponse(Iw).GetResponseStream())
- Threading.Thread.Sleep(0)
- Dim thepage As String = postreqreader(Iw).ReadToEnd
- If thepage = "" Then
- Debug.Print(thepage)
- End If
- 'give it free again
- postreqreader(Iw).Close()
- postreqreader(Iw) = Nothing
- postresponse(Iw).Close()
- postresponse(Iw) = Nothing
- Return thepage
- Catch e As WebException
- 'give it free again
- Debug.Print(ControlChars.Lf + ControlChars.NewLine + "Exception Raised. The following error occured : {0}", e.Status)
- If e.Status = WebExceptionStatus.ConnectFailure Then
- Return ""
- End If
- UserAgent = ""
- logincookie = New Net.CookieContainer
- 'Dim HTML2 As String = SendHTTP(method, PostString, SendURL, FromURL)
- Return ""
- Catch e As Exception
- 'give it free again
- Console.WriteLine(ControlChars.NewLine + "The following exception was raised : {0}", e.Message)
- Return ""
- End Try
- End Function
Habe den code gerade noch einmal geändert und im moment kommt mehr der 2. fehler.
Habe schon versucht mehrere webrequests (Public postReq(100) As HttpWebRequest) zu erstellen, aber das geht nicht. WebResponse und StreamReder macht er, webrequest nicht.
Hat jemand eine Ahnung wie man das Problem lösen kann? Oder was das Problem überhaupt ist? (Meine Vermutung sind 2 gleichzeitige Zugriffe auf das eine "postReq As HttpWebRequest". Aber wie umgehen?
Danke schon mal und viele Grüße
Chaosprogrammierer