0

I have a code in which I declared a variable like that

Dim http As New XMLHTTP60

then in the code, I used this line

http.Open "POST", urlexample, False
http.Send strArg

How can I use the waitForResponse to wait for the server to respond to the request. I tried looping like that after the .Send statement

While http.ReadyState <> 4: DoEvents: Wend

But this works sometimes well and sometimes doesn't return anything.

YasserKhalil
  • 9,138
  • 7
  • 36
  • 95

1 Answers1

1

Looking through object model seems that this method is not available through MSXML2.XMLHTTP60 but is with WinHttp.WinHttpRequest. The latter does not have a ReadyState property however.

enter image description here

You'll need to investigate the default timeout and see if you need SetTimeOuts on WinHttp object to obtain longer times.

Option Explicit
Public Sub testing()
    Dim http As winHttp.WinHttpRequest
    Set http = New winHttp.WinHttpRequest
  
    With http
        '.SetTimeouts ........
        .Open "GET", "http://books.toscrape.com/", True
        .setRequestHeader "User-Agent", "Mozilla/5.0"
        .send
        .waitForResponse 1000
    End With
    Stop
End Sub

N.B. Your request will not be async if the async arg is set to FALSE in the .Open


Interesting reading: https://github.com/VBA-tools/VBA-Web/issues/337

Seems Tim Hall's VBA-Web would be a nice pre-packaged way to go about this.

WinHTTP

QHarr
  • 83,427
  • 12
  • 54
  • 101
  • 1
    I'm at work. If this is a new question please open one as others may be able to answer it before I get to look at it. – QHarr Feb 18 '21 at 08:46
  • I have a weird case, sometimes the response has only one table while when navigating to the website, I found two tables. So it seems I have to wait till it is loaded properly (I am not sure). i tried to increate the timeout to 3000 but the same problem happens sometimes too. To your information, the problem is not happening all the time. But it is occassionally occur. Any ideas? – YasserKhalil Feb 18 '21 at 09:47
  • 1
    Did you set async to True? I am not saying that is the solution. I am asking how you are currently running please? – QHarr Feb 18 '21 at 09:55
  • I am using False in the code. I will try using the True to see if this will make difference or not. – YasserKhalil Feb 18 '21 at 10:00
  • 1
    I expect True to make situation worse! Have a look at @Omegastripes async answers for VBA. I think he answered one of your questions before. – QHarr Feb 18 '21 at 10:01
  • When I used the True I got an error at the part of `.waitForResponse` – YasserKhalil Feb 18 '21 at 10:02
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228890/discussion-between-qharr-and-yasserkhalil). – QHarr Feb 18 '21 at 12:00
  • Thanks a lot. What's the equivalent in python for that wait in requests package? – YasserKhalil Feb 20 '21 at 16:06
  • 1
    @YasserKhalil Equivalent of? There are [timeouts](https://requests.readthedocs.io/en/master/user/quickstart/#timeouts) but requests is not async. Async was moved: See https://stackoverflow.com/questions/9110593/asynchronous-requests-with-python-requests – QHarr Feb 20 '21 at 16:48
  • I don't think async is the right case for me. The problem with me with specific webpage which has one or two tables (sometimes one table and sometimes two tables). The problem happens with me when only one table is loaded and when manually go to that web page I found two tables .. That's too weird, and this webpage is loaded by javascript commands. So what I am seeking for is something that guarantee the full-load of the webpage .. – YasserKhalil Feb 20 '21 at 17:25
  • What's the webpage? Are you sure one table isn't loaded by javascript? – QHarr Feb 20 '21 at 18:46
  • I have explained the details on that link https://stackoverflow.com/questions/66295832/webpage-not-loaded-fully-all-the-time-in-python-requests – YasserKhalil Feb 20 '21 at 19:41
  • ok.. but how do I get direct to the problem url without reading all the code please? – QHarr Feb 20 '21 at 19:49
  • 1
    I don't know as the page is related to entering the case number and the captcha then go to the specific page. I have put the HTML for that page. – YasserKhalil Feb 20 '21 at 19:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229001/discussion-between-qharr-and-yasserkhalil). – QHarr Feb 20 '21 at 19:54