0

I have a little problem when using HttpClient. I plan to make several web requests, mainly GET and POST. A few years ago I was able to do it without worries, and there I wanted to do a little project again, only it doesn't work anymore: the program freezes (because it's not asynchronous but I don't really care for that) and sends me a timeout. There I'm using HttpClient, which is safer than HttpWebRequest from what I understand, because before that's what I was using. So code problem or other, I don't really know.. I tried with several hosts but all the time the same. There the test is carried out with youtube. It must show me the answer, so the source code of the page logically.

Here is the code used:

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MsgBox(WRequest("https://www.youtube.com/", "GET", ""))
    'WRequest("http://", "POST", "name=jane&stat=active")
End Sub
Function WRequest(URL As String, method As String, POSTdata As String) As String
    Dim responseData As String = ""
    Try
        Dim cookieJar As New Net.CookieContainer()
        Dim hwrequest As Net.HttpWebRequest = Net.WebRequest.Create(URL)
        hwrequest.CookieContainer = cookieJar
        hwrequest.Accept = "*/*"
        hwrequest.AllowAutoRedirect = True
        hwrequest.UserAgent = "http_requester/0.1"
        hwrequest.Timeout = 600
        hwrequest.Method = method
        If hwrequest.Method = "POST" Then
            hwrequest.ContentType = "application/x-www-form-urlencoded"
            Dim encoding As New Text.ASCIIEncoding() 'Use UTF8Encoding for XML requests
            Dim postByteArray() As Byte = encoding.GetBytes(POSTdata)
            hwrequest.ContentLength = postByteArray.Length
            Dim postStream As IO.Stream = hwrequest.GetRequestStream()
            postStream.Write(postByteArray, 0, postByteArray.Length)
            postStream.Close()
        End If
        Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
        If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
            Dim responseStream As IO.StreamReader =
              New IO.StreamReader(hwresponse.GetResponseStream())
            responseData = responseStream.ReadToEnd()
        End If
        hwresponse.Close()
    Catch e As Exception
        responseData = "An error occurred: " & e.Message
    End Try
    Return responseData
End Function 
End Class
3
  • 1
    There's nothing related to HttpClient here -- Remove this: hwrequest.Timeout = 600, quite inappropriate. The default is 100,000 -- not asynchronous but I don't really care: you do Commented Aug 17, 2023 at 1:51
  • 1
    BTW, set Option Strict to ON. You care about that, too Commented Aug 17, 2023 at 2:02
  • So I did as you told me. But infact it worked from the beginning, the problem came from displaying the response in MsgBox, it did not display it.. There I display it in a richtextbox, but it takes about 15 seconds to display the result, this which is huge for a simple GET request, knowing that I have a very good internet connection, in my browser it's almost instantaneous.. Thanks again for your help! PS: I just saw while debugging that the response is received almost immediately, the problem comes mainly from quickly displaying the response in the richtextbox.. Commented Aug 17, 2023 at 7:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.