Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WinHTTP question

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
AU
I have been using n WinHttp with success to quickly download small amounts of code (5k) but find if the address can't be found because it is off line, the computer hangs for about 5 or so seconds. (WINDOWS 7)
Is there a way of using this like it is asynchronous or any other method in Vb6 without third party software?
 
The Open method of the winhttprequest allows you to specify whether you want to connect synchronously or asynchronously.
 
Thanks.
What value should I use for [Async]?
The following doesn't give any data whereas 0 instead of True does (but I presume is Synch)-

Code:
Function GetDataFromInternet ()
With New WinHttpRequest
    .Open "GET", txtAddressString, True
    .SetCredentials txtUsername, txtPassword, 0
    .Send  'send an HTTP request
    GetDataFromInternet = StrConv(.ResponseBody, vbUnicode) ' get the binary data
End With

'ResponseText works just a well.
 
Well, no - it wouldn't.

You are treating an async call synchronously by immediately trying to read the data.

You need to wait for one of WinHttp's OnResponse events.

 
Thanks
That's why I asked if there was a simple way of getting the "sync version" to work asynchronously!
I guess I'll have to work out more code - I was just being lazy as usual!
 
Well of course you'll have to refactor.

Making a call asynchronous doesn't magically speed it up, which is what your code seems to have expected.

So, yep, a teeny bit more code :)

 
Thanks
There is no problem with speed of the actual download.
It is the pregnant pause when the site is off line where all other activity of the program is suspended. Even the keyboard is frozen for up to 10 seconds.

In this case I am automatically querying the data (only < 100 bytes of GPS data) every 20 seconds so you can see the problem.
When available it only takes 0.3 seconds.
 
>no problem with speed of the actual download

I should have been clearer. I wasn't referring to the speed of download, but the speed with which the call returns.
 
Oh.
Are you saying that even with asynchronous, I might still get the hang when the site is off line.
I thought the idea of async is to allow other event to happen until you get a result completed?

I don't get this effect with Firefox and can still load other sites while waiting for connections .
 
That's because Firefox is multithreaded. VB6 is not. As a result if VB's single thread is blocked whilst waiting for a call to complete then the application stops running until that call returns (including processing the message queue, which amongst many, many other things handles mouse and keyboard input*).

>Are you saying that even with asynchronous, I might still get the hang when the site is off line.

Well, yes. Sort of. The call still hangs - but it hangs in a different thread, so the VB thread can continue running. That separate thread then signals VB (with an event) when it is ready (or times out)

*BTW, this is one of the reasons for the existence of DoEvents in VB.
 
There isn't any "refactoring" involved whatsoever. The word you want is "rearchitecting."

About the worst thing you can do is to start spattering DoEvents call band-aids all over your code. This newbie deathtrap can lead to everything from data corruption to stack overflow exceptions when used in the typical blind manner.

Why on Earth is it so difficult for you to crack a manual? The reference material on the WinHttpRequest (which is very likely what you are actually using) clearly documents the SetTimeouts method. Use this in combination with an async open and handle the events. There is no good reason to make synchronous requests in a real application, this isn't QBasic or something.

This entire thread seems to be about something we already went over months ago here.
 
I think I'd argue the toss with you re refactoring versus rearchitecting, but I don't think it is important here.
 
Please let me know where this 'manual" is. I haven't been able to find one other than the ones that only refer to C+ which is still largely a mystery to me and probably always will seeing I probably only have a few years left before alchy sets in.
If I were 60 years younger I would probably have ditched all forms of vb years ago.
 
It is documented in the Windows SDK Help system. If you don't have a recent SDK installed, you might try a Google search which should turn up a link to the MSDN Library article:

WinHttpRequest object
 
Ah, you forget that's the sort of documentation Ted doesn't like, because it leads to pages "that only refer to C+" when you drill down to the methods and properties documentation!

Ted, I guess your problem is that you find the C+ declarations a foreign language. But for your purposes the declarations themselves are not (all that) important, since the WinHTTPRequest object presents them with the VB equivalents. You can view that VB declaration of the relevant Method or Property in the Object Browser, and you can therefore easily mentally map between the C+ declarations and the VB declarations, and thus make sense of the documentation (which normally is simply describing the purpose of each of the parameters)

For example: Open

MSDN
HRESULT Open(
[in] BSTR Method,
[in] BSTR Url,
[in, optional] VARIANT Async
);

VB Object Browser
Sub Open(Method As String, Url As String, [Async])



 
Thanks, but the declarations are not the problem.
I understand how to do what you have illustrated and many times used the object browser to find out this info and sometimes I have even found a useful procedure I didn't know was there.
My problem is how to work out which declaration to look for and use and I can't always see how to use it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top