Link to home
Start Free TrialLog in
Avatar of James
James

asked on

LOOKING for a good Wininet Wrapper

I am looking for a good async wininet wrapper control I can use in VB6! Almost found one in inet32.dll, but it is in german and lacked authintication. The wrapper should be able to use exisiting IE proxy settings so I don't have to fool with it; prompt if auhintication is required; returns the true host address (or least the response headers) so incase a redirect is involved (one of the things that makes Inet useless).

Here is 300 points for anyone who can point me to one!

Avatar of kishore1977
kishore1977

This module is called modWinInet.bas. Use the SplitAddr() function to get the address in the correct format for PostInfo.

Option Explicit

'Author:    Sam Huggill
'Email:     sam@vbsquare.com

Private Declare Function InternetOpen Lib "wininet.dll" _
         Alias "InternetOpenA" _
            (ByVal lpszCallerName As String, _
             ByVal dwAccessType As Long, _
             ByVal lpszProxyName As String, _
             ByVal lpszProxyBypass As String, _
             ByVal dwFlags As Long) As Long

      Private Declare Function InternetConnect Lib "wininet.dll" _
            Alias "InternetConnectA" _
            (ByVal hInternetSession As Long, _
             ByVal lpszServerName As String, _
             ByVal nProxyPort As Integer, _
             ByVal lpszUsername As String, _
             ByVal lpszPassword As String, _
             ByVal dwService As Long, _
             ByVal dwFlags As Long, _
             ByVal dwContext As Long) As Long

   Private Declare Function InternetReadFile Lib "wininet.dll" _
            (ByVal hFile As Long, _
             ByVal sBuffer As String, _
             ByVal lNumBytesToRead As Long, _
             lNumberOfBytesRead As Long) As Integer

   Private Declare Function HttpOpenRequest Lib "wininet.dll" _
            Alias "HttpOpenRequestA" _
            (ByVal hInternetSession As Long, _
             ByVal lpszVerb As String, _
             ByVal lpszObjectName As String, _
             ByVal lpszVersion As String, _
             ByVal lpszReferer As String, _
             ByVal lpszAcceptTypes As Long, _
             ByVal dwFlags As Long, _
             ByVal dwContext As Long) As Long

   Private Declare Function HttpSendRequest Lib "wininet.dll" _
            Alias "HttpSendRequestA" _
            (ByVal hHttpRequest As Long, _
             ByVal sHeaders As String, _
             ByVal lHeadersLength As Long, _
             ByVal sOptional As String, _
             ByVal lOptionalLength As Long) As Boolean

   Private Declare Function InternetCloseHandle Lib "wininet.dll" _
            (ByVal hInternetHandle As Long) As Boolean

   Private Declare Function HttpAddRequestHeaders Lib "wininet.dll" _
             Alias "HttpAddRequestHeadersA" _
             (ByVal hHttpRequest As Long, _
             ByVal sHeaders As String, _
             ByVal lHeadersLength As Long, _
             ByVal lModifiers As Long) As Integer


Public Function PostInfo$(srv$, script$, postdat$)

  Dim hInternetOpen As Long
  Dim hInternetConnect As Long
  Dim hHttpOpenRequest As Long
  Dim bRet As Boolean
 
  hInternetOpen = 0
  hInternetConnect = 0
  hHttpOpenRequest = 0
 
  'Use registry access settings.
  Const INTERNET_OPEN_TYPE_PRECONFIG = 0
  hInternetOpen = InternetOpen("http generic", _
                  INTERNET_OPEN_TYPE_PRECONFIG, _
                  vbNullString, _
                  vbNullString, _
                  0)
 
  If hInternetOpen <> 0 Then
     'Type of service to access.
     Const INTERNET_SERVICE_HTTP = 3
     Const INTERNET_DEFAULT_HTTP_PORT = 80
     'Change the server to your server name
     hInternetConnect = InternetConnect(hInternetOpen, _
                        srv$, _
                        INTERNET_DEFAULT_HTTP_PORT, _
                        vbNullString, _
                        "HTTP/1.0", _
                        INTERNET_SERVICE_HTTP, _
                        0, _
                        0)
 
     If hInternetConnect <> 0 Then
      'Brings the data across the wire even if it locally cached.
       Const INTERNET_FLAG_RELOAD = &H80000000
       hHttpOpenRequest = HttpOpenRequest(hInternetConnect, _
                           "POST", _
                           script$, _
                           "HTTP/1.0", _
                           vbNullString, _
                           0, _
                           INTERNET_FLAG_RELOAD, _
                           0)
 
        If hHttpOpenRequest <> 0 Then
           Dim sHeader As String
           Const HTTP_ADDREQ_FLAG_ADD = &H20000000
           Const HTTP_ADDREQ_FLAG_REPLACE = &H80000000
  sHeader = "Content-Type: application/x-www-form-urlencoded" _
             & vbCrLf
           bRet = HttpAddRequestHeaders(hHttpOpenRequest, _
             sHeader, Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE _
             Or HTTP_ADDREQ_FLAG_ADD)
 
           Dim lpszPostData As String
           Dim lPostDataLen As Long
 
           lpszPostData = postdat$
           lPostDataLen = Len(lpszPostData)
           bRet = HttpSendRequest(hHttpOpenRequest, _
                  vbNullString, _
                  0, _
                  lpszPostData, _
                  lPostDataLen)
 
           Dim bDoLoop             As Boolean
           Dim sReadBuffer         As String * 2048
           Dim lNumberOfBytesRead  As Long
           Dim sBuffer             As String
           bDoLoop = True
           While bDoLoop
            sReadBuffer = vbNullString
            bDoLoop = InternetReadFile(hHttpOpenRequest, _
               sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
            sBuffer = sBuffer & _
                 Left(sReadBuffer, lNumberOfBytesRead)
            If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
           Wend
           PostInfo = sBuffer
           bRet = InternetCloseHandle(hHttpOpenRequest)
        End If
        bRet = InternetCloseHandle(hInternetConnect)
     End If
     bRet = InternetCloseHandle(hInternetOpen)
  End I
nd Function

Public Sub SplitAddr(ByVal addr$, srv$, script$)
'Inputs: The full url including http://
' Two variables that will be changed
'
'Returns: Splits the addr$ var into the server name
' and the script path

  Dim i%

  i = InStr(addr$, "/")
  srv$ = Mid(addr$, i + 2, Len(addr$) - (i + 1))
  i = InStr(srv$, "/")
  script$ = Mid(srv$, i, Len(srv$) + 1 - i)
  srv$ = Left$(srv$, i - 1)

End Sub

Avatar of James

ASKER

This looks like a synchourous wininet post API. I am trying to implement something along the lines of non-blocking asynchourous calls which is hard under Vb6.
It looks like if one wanted asynchourous like behavior under VB6 might have to explore running the wininet session on a thread perhaps. Although I would post and use https when required...predominetly GET would be used.

Basically, I am just requesting a html file and downloading it and saving it. Not retrieving any elements in the file. Where some of the current VB methods for doing this is lacking is in using existing proxy settings which is easy to do using wininet api; lack of redirect brains, e.g., unable to return the location it has been redirected too. This sounds trival, but a many controls I find really do not update their location address or make it easy to fetch. Then there is authorization, many don't handle this in desirable ways. I like some means to be prompt for a realm requesting since there can be many with different credentials required.

Ok, well one can accomplish all the above easy enough using wininet api...its just the dang blocking issue thats horrible. I figured there has to be a control out there that does asynchourous wininet calls that was compiled in C or something.

Like I said before, may be able to work around the issue in VB6 if the calls can be made in seperate thread ?? Only thing I can think of.....

Avatar of Richie_Simonetti
Well, the problem here is the word: Async
Asyn download needs callback function and the use of Addressof operator.
Please, do a check at this to see if meets your needs:

http://www.freevbcode.com/ShowCode.Asp?ID=1593
http://www.freevbcode.com/ShowCode.ASP?ID=1593
http://www.freevbcode.com/ShowCode.Asp?ID=1991
or do a search at www.freevbcode.com with keyword: ftp for a complete listing.
optionally, may be you could get some clues at:
http://www.mvps.org/vbnet/code/internet/ftpdownloadcallback.htm
for callback stuff

Well ohme, you have some pretty simple alternatives available here.

1) Make your own ITC inet control with the features you want and lacking in in the dumb msinet.ocx. I recall you don't have viusal C++, but lot of competent programmers wrap their own ITC using wininet and you can grab one of them that does what you want. I run into them all the time on the web. Very easy to make a wininet wrapper control! You need to use C or MFC so you can get non-blocking functionality that is too hard in VB. VB makes easy things easier while making hard things harder. This is a good example of that<g>

2) Buy one! Many that will do what you want that are cheap or free. Go look on HotScrips under something like Components Networking for a free or cheap HTTP control. Many are written for server/clinet side will work just fine. Don't pay more then $50 for one, they aren't worth it since too easy to make your own! Don't be lazy! Here is a cute $29 one at:

http://www.dynu.com/dynuhttp.asp



Later

Hi ohmeohmy,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Refund points and save as a 0-pt PAQ.
    *** Richie_Simonetti's links were for FTP, but Asker is clearly needing http transfer.  Should check out the XmlHttpRequest object.

ohmeohmy, Please DO NOT accept this comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
==========
DanRollins -- EE database cleanup volunteer
ASKER CERTIFIED SOLUTION
Avatar of SpideyMod
SpideyMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial