Click here to Skip to main content
15,886,046 members
Articles / Desktop Programming / MFC

CHttpClient - A Helper Class Using WinInet

Rate me:
Please Sign up or sign in to vote.
4.96/5 (59 votes)
10 Aug 20073 min read 485.6K   24.9K   163  
A C++ class which helps you to interact with a HTTP web server.
'
' file:     HttpGet.vbs
' brief:    A HTTP GET demo script for the HttpClient component edition.
' author:   Jo Hyeong-ryeol
' since:    2006.01.09
' version:  $LastChangedRevision: 94 $
'           $LastChangedDate: 2006-01-30 23:52:44 +0900 (월, 30 1 2006) $
' 
' This file contains a HTTP GET demo script for the HttpClient component edition.
' 
' Copyright (c) 2006 by Jo Hyeong-ryeol (hyeongryeol@gmail.com)
' Permission to copy, use, modify, sell and distribute this software is
' granted provided this copyright notice appears in all copies.
' This software is provided "as is" without express or implied warranty,
' and with no claim as to its suitability for any purpose.
'

    Const   HttpClientParamNormal           = 0
    Const   HttpClientParamFile             = 1
    Const   HttpClientParamEncodedName      = 2
    Const   HttpClientParamEncodedValue     = 4
    Dim     HttpClientParamEncoded
    HttpClientParamEncoded =  HttpClientParamEncodedName Or HttpClientParamEncodedValue

    
    Dim     objHttpClient, objHttpResponse

    ' Create the HttpClient object
    Set objHttpClient = CreateObject ("Ryeol.HttpClient2")
    
    ' Initialize the User Agent
    objHttpClient.InternetUserAgent = "My User Agent v1.0"
    
    ' Specifies whether to use UTF-8 encoding. (This uses ANSI encoding)
    ' Default is FALSE
    objHttpClient.UseUtf8 = False
    
    ' Specifies a code page for ANSI strings. (This uses Korean)
    ' Default is CP_ACP
    objHttpClient.AnsiCodePage = 949

    ' Add user's custom HTTP headers
    Call objHttpClient.AddHeader ("Ryeol-Magic", "My Magic Header")
    Call objHttpClient.AddHeader ("User-Magic", "User's Magic Header")

    ' Add user's parameters
    Call objHttpClient.AddParam ("where", "nexearch")
    Call objHttpClient.AddParam ("frm", "t1")
    Call objHttpClient.AddParam ("query", "%C3%D6%C1%F6%BF%EC", HttpClientParamEncodedValue)

    ' Send a request
    Const   TargetURL       = "http://search.naver.com/search.naver"
    Call WScript.Echo ("HTTP GET " & objHttpClient.MakeGetUrl (TargetURL))

    Set objHttpResponse = objHttpClient.RequestGet (TargetURL)


    '
    ' Reports results
    '

    Call WScript.Echo (CStr (objHttpResponse.Status) & " " & objHttpResponse.StatusText)

    Dim             knownHeaderNames
    knownHeaderNames = Array _
        ( _
            "Server", "Date", "X-Powered-By", "Content-Length", "Set-Cookie" _
            , "Expires", "Cache-control", "Connection", "Transfer-Encoding" _
            , "Content-Type" _
        )
    Dim             knownHeaderString
    knownHeaderString = ""
        
    For i = 0 To UBound (knownHeaderNames) Step 1

        If i <> 0 then
            knownHeaderString = knownHeaderString & vbCrLf
        End if
        
        If objHttpResponse.HeaderExists (knownHeaderNames(i)) then
            knownHeaderString = knownHeaderString & knownHeaderNames(i) & ": " & objHttpResponse.GetHeader (knownHeaderNames(i))
        Else
            ' If the header is not found..
            knownHeaderString = knownHeaderString & "'" & knownHeaderNames(i) & "' header does not exist.."
        End if

    Next

    ' writes known headers
    Call WScript.Echo (knownHeaderString)
    

    ' checks the returned data type
    Dim         isText
    isText = False
    If objHttpResponse.HeaderExists ("Content-Type") then
        
        If InStr (objHttpResponse.GetHeader ("Content-Type"), "text/") = 1 then
            isText = True
        End if

    End if

    ' checks the returned stream size
    Dim         contentLength
    If objHttpResponse.HasContentLength () then
        contentLength = CLng (objHttpResponse.ContentLength)
    Else
        contentLength = "Unknown"
    End if

    
    If isText then
        Call WScript.Echo ("The content stream is a text. (" & contentLength & " bytes)")
    Else
        Call WScript.Echo ("The content stream is a binary. (" & contentLength & " bytes)")
    End if


    ' Saves the returned content stream
    const       FilePath = "ReturnedContent.HttpGet.vbs.dat"

    Call objHttpResponse.SaveContent (FilePath)
    Call WScript.Echo ("The content stream is saved to a file '" & FilePath & "'")

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Korea (Republic of) Korea (Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions