Solved

Program freezes up...

Posted on 1998-09-23
7
698 Views
Last Modified: 2012-06-27
I'm currently working on a program that connects to a web site and saves it as a text file.  I use the wininet.bas from Microsoft along with it.  The problem is, the program freezes up when it starts and unfreezes when it has finished.  Is there any way to fix this problem?  Here is my code:

Option Explicit

Private hInternetSession As Long
Private hInternetConnect As Long
Private hHttpOpenRequest As Long
Private txtURL As String


Private Sub cmdGO_Click()
hInternetSession = 0
hHttpOpenRequest = 0
hInternetConnect = 0
hInternetSession = InternetOpen(scUserAgent,
INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
If CBool(hInternetSession) Then
    txtHTML = "Ready"
Else
    txtHTML = "InternetOpen failed."
End If

txtURL = txtUrls.Text
'---------------------------------------------------------------------------
---------

Dim iRetVal     As Integer
Dim sBuffer     As String * 1024
Dim lBufferLen  As Long
Dim vDllVersion As tWinInetDLLVersion
Dim sStatus     As String
Dim sOptionBuffer   As String
Dim lOptionBufferLen As Long
lBufferLen = Len(sBuffer)
If CBool(hInternetSession) Then
'    SetStatus "InternetQueryOption"
    InternetQueryOption hInternetSession, INTERNET_OPTION_VERSION,
vDllVersion, Len(vDllVersion)
        hInternetConnect = InternetConnect(hInternetSession, CheckUrl,
INTERNET_DEFAULT_HTTP_PORT, _
                            "", "", INTERNET_SERVICE_HTTP, 0, 0)
    If hInternetConnect > 0 Then

        'SetStatus "HttpOpenRequest"
        'If optGet.Value = True Then
            sOptionBuffer = vbNullString
            lOptionBufferLen = 0
                hHttpOpenRequest = HttpOpenRequest(hInternetConnect,
"GET", GetUrlObject, "HTTP/1.0", vbNullString, 0, _
                INTERNET_FLAG_RELOAD Or INTERNET_FLAG_KEEP_CONNECTION,
0)
        'Else
        '    sOptionBuffer = txtPost.Text
        '    lOptionBufferLen = Len(sOptionBuffer)
        '        hHttpOpenRequest = HttpOpenRequest(hInternetConnect,
"POST", GetUrlObject, "HTTP/1.0", vbNullString, 0, _
        '        INTERNET_FLAG_RELOAD Or INTERNET_FLAG_MULTIPART, 0)
        'End If

        If CBool(hHttpOpenRequest) Then
            'SetStatus "HttpSendRequest"
            Debug.Print sOptionBuffer
            Dim sHeader As String

            'sHeader = "Accept: image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/vnd." & vbCrLf
            'iRetVal = HttpAddRequestHeaders(hHttpOpenRequest, sHeader,
Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE Or HTTP_ADDREQ_FLAG_ADD)
            'Debug.Print iRetVal & " " & Len(sHeader)

            sHeader = "Accept-Language: en" & vbCrLf
            iRetVal = HttpAddRequestHeaders(hHttpOpenRequest, sHeader,
Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE Or HTTP_ADDREQ_FLAG_ADD)
            Debug.Print iRetVal & " " & Len(sHeader)

            sHeader = "Connection: Keep-Alive" & vbCrLf
            iRetVal = HttpAddRequestHeaders(hHttpOpenRequest, sHeader,
Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE Or HTTP_ADDREQ_FLAG_ADD)
            Debug.Print iRetVal & " " & Len(sHeader);

            sHeader = "Content-Type: application/x-www-form-urlencoded"
& vbCrLf
            iRetVal = HttpAddRequestHeaders(hHttpOpenRequest, sHeader,
Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE Or HTTP_ADDREQ_FLAG_ADD)
            Debug.Print iRetVal & " " & Len(sHeader);
           'sHeader = "Content-Type: text/html" & vbCrLf ' "Accept =
image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd." &
vbCrLf
            'iRetVal = HttpAddRequestHeaders(hHttpOpenRequest, sHeader,
Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE Or HTTP_ADDREQ_FLAG_ADD)
            'Debug.Print iRetVal & " " & Len(sHeader)

'Actually only INTERNET_OPTION_RECEIVE_TIMEOUT works. More info see the
following KB:
'BUG: InternetSetOption Does Not Set Timeout Values            [axsdk]
'ID: Q176420    CREATED: 06-NOV-1997   MODIFIED: 06-NOV-1997
           Dim dwTimeOut As Long
            dwTimeOut = 30000
            iRetVal = InternetSetOption(hHttpOpenRequest,
INTERNET_OPTION_CONNECT_TIMEOUT, _
        dwTimeOut, 4)
            Debug.Print iRetVal & " " & Err.LastDllError & " " &
"INTERNET_OPTION_CONNECT_TIMEOUT"
            iRetVal = InternetSetOption(hHttpOpenRequest,
INTERNET_OPTION_RECEIVE_TIMEOUT, _
        dwTimeOut, 4)
            Debug.Print iRetVal & " " &
"INTERNET_OPTION_RECEIVE_TIMEOUT"
            iRetVal = InternetSetOption(hHttpOpenRequest,
INTERNET_OPTION_SEND_TIMEOUT, _
        dwTimeOut, 4)
            Debug.Print iRetVal & " " & "INTERNET_OPTION_SEND_TIMEOUT"

Resend:
            iRetVal = HttpSendRequest(hHttpOpenRequest, vbNullString, 0,
sOptionBuffer, lOptionBufferLen)
            Dim dwStatus As Long, dwStatusSize As Long
            dwStatusSize = Len(dwStatus)
            HttpQueryInfo hHttpOpenRequest, HTTP_QUERY_FLAG_NUMBER Or
HTTP_QUERY_STATUS_CODE, dwStatus, dwStatusSize, 0
            Select Case dwStatus
                Case HTTP_STATUS_PROXY_AUTH_REQ
                'make sure change it to your user name and password.
                'Note Poxy authentication only works for IE40 wininet.
For IE3.0x, you need to
                'manually add Proxy-Authentication header.
                 iRetVal = InternetSetOptionStr(hHttpOpenRequest,
INTERNET_OPTION_PROXY_USERNAME, _
        "IUSR_WEIHUA1", Len("IUSR_WEIHUA1") + 1)
                 iRetVal = InternetSetOptionStr(hHttpOpenRequest,
INTERNET_OPTION_PROXY_PASSWORD, _
        "IUSR_WEIHUA1", Len("IUSR_WEIHUA1") + 1)
              Case HTTP_STATUS_DENIED
                iRetVal = InternetSetOptionStr(hHttpOpenRequest,
INTERNET_OPTION_USERNAME, _
        "IUSR_WEIHUA1", Len("IUSR_WEIHUA1") + 1)
                iRetVal = InternetSetOptionStr(hHttpOpenRequest,
INTERNET_OPTION_PASSWORD, _
        "IUSR_WEIHUA1", Len("IUSR_WEIHUA1") + 1)
            GoTo Resend
           End Select

            If iRetVal Then
            Else
                ' HttpSendRequest failed
                sStatus = "HttpSendRequest call failed; Error code: " &
Err.LastDllError & "."
            End If
        Else
            ' HttpOpenRequest failed
            sStatus = "HttpOpenRequest call failed; Error code: " &
Err.LastDllError & "."
        End If
    Else
        ' InternetConnect failed
        sStatus = "InternetConnect call failed; Error code: " &
Err.LastDllError & "."
    End If
Else
    ' hInternetSession handle not allocated
    sStatus = "InternetOpen call failed: Error code: " &
Err.LastDllError & "."
End If

GetPage

InternetCloseHandle (hHttpOpenRequest)
InternetCloseHandle (hInternetConnect)
InternetCloseHandle (hInternetSession)


End Sub

Private Function GetPage()
Dim bDoLoop             As Boolean
Dim sReadBuffer         As String * 2048
Dim lNumberOfBytesRead  As Long
Dim sBuffer             As String
On Error Resume Next

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
    'If CBool(Val(lblContentLength)) Then ProgressBar1.Value =
ProgressBar1.Value + lNumberOfBytesRead
Wend
txtHTML = sBuffer
'ProgressBar1.Value = 0
End Function

Private Function CheckUrl() As String
If Len(txtURL) = 0 Then txtURL = "www.microsoft.com"
Dim posSlash As Long
posSlash = InStr(txtURL, "/")
If InStr(txtURL, "/") <> 0 Then
CheckUrl = Left(txtURL, InStr(txtURL, "/") - 1)
Else
CheckUrl = txtURL
End If
End Function

Private Function GetUrlObject() As String
If InStr(txtURL, "/") <> 0 Then
GetUrlObject = Right(txtURL, Len(txtURL) - InStr(txtURL, "/") + 1)
Else
GetUrlObject = ""
End If
End Function

--------------------------

I probably have extra un-needed code in there also... I got the code from a web site and don't have alot of experience with API calls so I haven't cleaned it up.

Any ideas how to fix this problem?
0
Comment
Question by:dokken
7 Comments
 
LVL 3

Expert Comment

by:HATCHET
ID: 1436369
dokken,

Do you honestly expect someone to weed through your program and fix it for you for 25 points?!  That code is a MESS!!  State what you need done and wait for responses.  Restate the question as needed based upon feedback.  Don't just dump your code out here and say "WILL SOMEONE FIX THIS FOR ME?!"

If you're trying to pull down HTML code from a Web Page and save it to a .txt file, I have a snippit of code about 5 lines long that will do it quick ly and efficiently, but I'm not gunna bother with your code up there for 25 points, sorry.

HATCHET
0
 
LVL 3

Expert Comment

by:a111a111a111
ID: 1436370
HATCHET: Good Point.
Can you Please email me the  code topull down HTML code from a Web Page and save it to a .txt file .

Please thank you.
0
 

Author Comment

by:dokken
ID: 1436371
I would have made it more points but thats all I have at the moment.  Could I have the 5 line code? and if it goes through it more than once will it freeze the program until it has finished?

Thanks.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 12

Accepted Solution

by:
mark2150 earned 20 total points
ID: 1436372
sprinkle DoEvents inside your loops...
0
 

Author Comment

by:dokken
ID: 1436373
mark2150,

That worked perfect! I forgot about the DoEvents.

Thanks.
0
 
LVL 3

Expert Comment

by:HATCHET
ID: 1436374
Here's the 5 lines of code that I mentioned to save a file on the internet to a file on your computer.  You can replace the http:// with ftp:// and get a file from an FTP server too.  Hope this helps a fe people.   =]


  Dim strSaveThisInfo as String

  Inet1.AccessType = icUseDefault
  strSaveThisInfo =
Inet1.OpenURL("http://www.microsoft.com/index.html")

  Open "C:\Test.txt" For Output As #1
    Print #1, strSaveThisInfo
  Close #1



HATCHET
0
 

Author Comment

by:dokken
ID: 1436375
I used that code like that before... the reason I switched was so I could set the UserAgent.  With the Internet Control, Internet Log files show: MS URL Control 5.0 or something like that.
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question