Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


WinHTTP question

WinHTTP question

WinHTTP question

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?

RE: WinHTTP question

The Open method of the winhttprequest allows you to specify whether you want to connect synchronously or asynchronously.

RE: WinHTTP question

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. 

RE: WinHTTP question

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.

RE: WinHTTP question

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!

RE: WinHTTP question

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 smile

RE: WinHTTP question

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.

RE: WinHTTP question

>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.

RE: WinHTTP question

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 .

RE: WinHTTP question

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.

RE: WinHTTP question

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.

RE: WinHTTP question

I think I'd argue the toss with you re refactoring versus rearchitecting, but I don't think it is important here.

RE: WinHTTP question

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.

RE: WinHTTP question

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

RE: WinHTTP question

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

[in] BSTR Method,
[in] BSTR Url,
[in, optional] VARIANT Async

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

RE: WinHTTP question

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!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close