Solved

Program freezes up...

Posted on 1998-09-23
7
697 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 12

Accepted Solution

by:
mark2150 earned 20 total points
Comment Utility
sprinkle DoEvents inside your loops...
0
 

Author Comment

by:dokken
Comment Utility
mark2150,

That worked perfect! I forgot about the DoEvents.

Thanks.
0
 
LVL 3

Expert Comment

by:HATCHET
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now