Problem bei gleichzeitigen HttpWebRequest(?)-Verbindungen

  • VB.NET

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von Chaosprogrammierer.

    Problem bei gleichzeitigen HttpWebRequest(?)-Verbindungen

    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

    VB.NET-Quellcode

    1. Public postresponse(100) As HttpWebResponse
    2. Public postreqreader(100) As StreamReader
    3. Public Function SendHTTP(ByVal method As String, ByVal PostString As String, ByVal SendURL As String, ByVal FromURL As String) As String
    4. Dim postData As String = PostString
    5. Dim tempCookies As New CookieContainer
    6. Dim encoding As New UTF8Encoding
    7. Dim byteData As Byte() = encoding.GetBytes(postData)
    8. Dim ProxyIP As String = ""
    9. Dim ProxyPort As Integer = 0
    10. Try
    11. 'Debug.Print(SendURL)
    12. 'search the next free connection
    13. Dim Iw As Integer = 0
    14. Do
    15. If postresponse(Iw) Is Nothing Then
    16. Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create(SendURL), HttpWebRequest)
    17. postReq.KeepAlive = True
    18. postReq.CookieContainer = logincookie
    19. postReq.ContentType = "application/x-www-form-urlencoded"
    20. postReq.Referer = FromURL
    21. postReq.UserAgent = UserAgent
    22. 'proxy
    23. If Not ProxyIP = "" And Not ProxyPort = 0 Then
    24. Dim proxy = New WebProxy(ProxyIP, ProxyPort)
    25. postReq.Proxy = proxy
    26. End If
    27. ' only if post
    28. If LCase(method) = "post" Then
    29. postReq.Method = "POST"
    30. postReq.ContentLength = byteData.Length
    31. Dim postreqstream As Stream = postReq.GetRequestStream()
    32. postreqstream.Write(byteData, 0, byteData.Length)
    33. postreqstream.Close()
    34. Else
    35. postReq.Method = "GET"
    36. End If
    37. postresponse(Iw) = DirectCast(postReq.GetResponse(), HttpWebResponse)
    38. Exit Do
    39. End If
    40. Iw += 1
    41. Loop Until Iw > 100
    42. 'Wait a bit
    43. Threading.Thread.Sleep(0)
    44. 'cookie handling
    45. If Not logincookie Is Nothing Then
    46. If postresponse(Iw).Cookies.Count > 0 Then
    47. logincookie.Add(postresponse(Iw).Cookies)
    48. End If
    49. End If
    50. 'logincookie = tempCookies
    51. postreqreader(Iw) = New StreamReader(postresponse(Iw).GetResponseStream())
    52. Threading.Thread.Sleep(0)
    53. Dim thepage As String = postreqreader(Iw).ReadToEnd
    54. If thepage = "" Then
    55. Debug.Print(thepage)
    56. End If
    57. 'give it free again
    58. postreqreader(Iw).Close()
    59. postreqreader(Iw) = Nothing
    60. postresponse(Iw).Close()
    61. postresponse(Iw) = Nothing
    62. Return thepage
    63. Catch e As WebException
    64. 'give it free again
    65. Debug.Print(ControlChars.Lf + ControlChars.NewLine + "Exception Raised. The following error occured : {0}", e.Status)
    66. If e.Status = WebExceptionStatus.ConnectFailure Then
    67. Return ""
    68. End If
    69. UserAgent = ""
    70. logincookie = New Net.CookieContainer
    71. 'Dim HTML2 As String = SendHTTP(method, PostString, SendURL, FromURL)
    72. Return ""
    73. Catch e As Exception
    74. 'give it free again
    75. Console.WriteLine(ControlChars.NewLine + "The following exception was raised : {0}", e.Message)
    76. Return ""
    77. End Try
    78. 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