2013-03-29 172 views
1

顯示如何將我的VB6表單POST 2個瓦爾,拉從一個URL的結果,然後分配一個VB6 VAR的結果?VB6 - 使用POST和從URL GET和VB6形式

我需要有人來告訴我非常基本的VB6的示例代碼或點我在正確的方向。這是最簡單的形式 - 在最終產品中,PHP變量將寫入MySQL,但這不是我需要幫助的。

我有一個接受兩個參數一個簡單的PHP頁面:

test.php?var1=secret&var2=pass 

這是我非常簡單的PHP代碼

<?php 

$var1 = $_GET['var1']; 
$var2 = $_GET['var2']; 

$varAcc = "ACCEPTED"; 
$varDen = "DENIED"; 

if ($var1 === "secret" && $var2 === "pass") 
    { 
    echo $varAcc; 
    } 
else 
    { 
    echo $varDen; 
    } 
?> 

這背後的邏輯是「username」的會是VB6登錄,「 passWord「和」hardWareID「,併發送一個散列。將對照MySQL檢查哈希以查看它是否存在,並返回YES或NO進行訪問,帳戶剩餘多少天以及其他一些詳細信息,例如FULL NAME,ACOUNT INFO等。

(NO。我不希望使用XML,只是想我會把那個在那裏..只要POST &接收到增值經銷商)

謝謝你......

+0

看的答案,「如何GET/POST在VB6 RESTful Web服務」 http://stackoverflow.com/questions/3516119/get-post -to-REST風格的Web服務 – MarkJ

回答

4

VB形式沒有任何內置的機制用於發送HTTP請求。有些人可能會建議您使用Internet Transfer Control。但是,VB UserControl有一個HTTP機制,可以在不需要第三方控件的情況下使用,假設您使用GET方法,並使用查詢字符串傳遞參數。如果您必須使用POST,則必須使用Internet Transfer Control。

創建爲「Microsoft腳本運行」一參一VB項目(參見菜單項目=>參考)。添加一個UserControl。稱它爲「HttpService」。設置InvisibleAtRuntime = True。將下面的代碼添加到UserControl:

Option Explicit 

Private Const m_ksProperty_Default    As String = "" 

Private m_sHost         As String 
Private m_nPort         As Long 
Private m_sPath         As String 
Private m_dctQueryStringParameters    As Scripting.Dictionary 

Private m_sOutput        As String 

' Ensure that all parts of the query string are deleted. 
Public Sub ClearQueryString() 

    Set m_dctQueryStringParameters = New Scripting.Dictionary 

End Sub 

' Executes "GET" method for URL. 
Public Function Get_() As String 

    ' Read in data from URL. UserControl_AsyncReadComplete will fire when finished. 
    UserControl.AsyncRead "http://" & m_sHost & ":" & CStr(m_nPort) & "" & m_sPath & "?" & GetQueryString(), vbAsyncTypeByteArray, m_ksProperty_Default, vbAsyncReadSynchronousDownload 

    ' Return the contents of the buffer. 
    Get_ = m_sOutput 

    ' Clear down state. 
    m_sOutput = vbNullString 

End Function 

' Returns query string based on dictionary. 
Private Function GetQueryString() As String 

    Dim vName         As Variant 
    Dim sQueryString       As String 

    For Each vName In m_dctQueryStringParameters 
     sQueryString = sQueryString & CStr(vName) & "=" & m_dctQueryStringParameters.Item(vName) & "&" 
    Next vName 

    GetQueryString = Left$(sQueryString, Len(sQueryString) - 1) 

End Function 

' Sets the remote host. 
Public Property Let Host(ByVal the_sValue As String) 

    m_sHost = the_sValue 

End Property 

' Sets the directory and filename part of the URL. 
Public Property Let Path(ByVal the_sValue As String) 

    m_sPath = the_sValue 

End Property 

' Sets the port number for this request. 
Public Property Let Port(ByVal the_nValue As Long) 

    m_nPort = the_nValue 

End Property 

' Sets a name/value pair in the query string. Supports duplicate names. 
Public Property Let QueryStringParameter(ByVal the_sName As String, ByVal the_sValue As String) 

    m_dctQueryStringParameters.Item(the_sName) = the_sValue 

End Property 

' Fired when the download is complete. 
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty) 

    ' Gets the data from the internet transfer. 
    m_sOutput = StrConv(AsyncProp.Value, vbUnicode) 

End Sub 

Private Sub UserControl_Initialize() 

    ' Initialises the scripting dictionary. 
    Set m_dctQueryStringParameters = New Scripting.Dictionary 

End Sub 

要使用此用戶控件,將其添加到您的窗體。稱它爲「HttpService」。添加一個名爲「爲txtOutput」測試表上的下面的代碼文本框:

HttpService.Host = "localhost" 
HttpService.Port = 80 
HttpService.Path = "/test.php" 
HttpService.QueryStringParameter("var1") = "secret" 
HttpService.QueryStringParameter("var2") = "pass" 

txtOutput.Text = HttpService.Get_ 
1

如果你必須使用POST,那麼你將不得不使用Internet傳輸控制。在VB6 IDE中,按CTL-T,然後選擇「Microsoft Internet Transfer Control 6.0」。按確定。

將控件的一個實例添加到窗體。稱它爲「Inet」。將一個名爲「cmdPost」的CommandButton添加到表單中。添加對「Microsoft Scripting Runtime」的引用(請參閱菜單Project => References)。

下面的代碼添加到您的窗體:

Option Explicit 

Private Declare Function InternetCanonicalizeUrl Lib "Wininet.dll" Alias "InternetCanonicalizeUrlW" (_ 
    ByVal lpszUrl As Long, _ 
    ByVal lpszBuffer As Long, _ 
    ByRef lpdwBufferLength As Long, _ 
    ByVal dwFlags As Long _ 
) As Long 

Private m_sData       As String 
Private m_nDataReceived     As Long 
Private m_bPostActive     As Boolean 
Private m_bDataReceived     As Boolean 
Private m_bError      As Boolean   ' For error handling. 
Private m_bDisconnected     As Boolean 

Private Sub cmdPost_Click() 

    Dim dctParameters     As Scripting.Dictionary 

    txtOutput.Text = vbNullString 

    m_sData = vbNullString 
    Set dctParameters = New Scripting.Dictionary 

    dctParameters.Add "var1", "secret" 
    dctParameters.Add "var2", "pass" 

    txtOutput.Text = Post("http://localhost:80/test.php", dctParameters) 

End Sub 

' Returns post data string based on dictionary. 
Private Function GetPostDataString(ByRef the_dctParameters As Scripting.Dictionary) As String 

    Dim vName         As Variant 
    Dim sPostDataString       As String 

    For Each vName In the_dctParameters 
     sPostDataString = sPostDataString & UrlEncode(CStr(vName)) & "=" & UrlEncode(CStr(the_dctParameters.Item(vName))) & "&" 
    Next vName 

    GetPostDataString = Left$(sPostDataString, Len(sPostDataString) - 1) 

End Function 

Private Sub Inet_StateChanged(ByVal State As Integer) 

    ' Ignore state change if we are outside the Post function. 
    If m_bPostActive Then 

     Select Case State 
     Case StateConstants.icResponseReceived 
      ReceiveData False 
     Case StateConstants.icResponseCompleted 
      ReceiveData True 
     Case StateConstants.icDisconnected 
      m_bDisconnected = True 
     Case StateConstants.icError 
      m_bError = True 
     End Select 

    End If 

End Sub 

' Synchronous Post function. 
Private Function Post(ByRef the_sURL As String, ByRef the_dctParameters As Scripting.Dictionary) 

    Dim sPostData        As String 
    Dim sHeaders        As String 

    ' Flag that we are in the middle of this function. 
    m_bPostActive = True 

    ' Create a string containing the POST parameters. 
    sPostData = GetPostDataString(the_dctParameters) 

    ' Create a headers string to allow POST. 
    sHeaders = _ 
     "Content-Type: application/x-www-form-urlencoded" & vbNewLine & _ 
     "Content-Length: " & CStr(Len(sPostData)) & vbNewLine & _ 
     "Connection: Keep-Alive" & vbNewLine & _ 
     "Cache-Control: no-cache" & vbNewLine 

    Inet.Execute the_sURL, "POST", GetPostDataString(the_dctParameters), sHeaders 

    ' Allow Inet events to fire. 
    Do 
     DoEvents 
    Loop Until m_bDataReceived Or m_bDisconnected 

    If m_bDataReceived Then 
     Post = m_sData 
    End If 

    ' Clear all state flags to defaults. 
    m_bDataReceived = False 
    m_bDisconnected = False 
    m_bError = False 
    m_sData = vbNullString 
    m_nDataReceived = 0 

    ' Flag that we have exited this function. 
    m_bPostActive = False 

End Function 

' Receive as much data as we can. 
' <the_bCompleted> should be True if the response is completed i.e. all data is available. 
Private Sub ReceiveData(ByVal the_bCompleted As Boolean) 

    Const knBufferSize     As Long = 1024 
    Dim nContentLength     As Long 
    Dim sContentType     As String 
    Dim sChunk       As String 
    Dim nChunkSize      As Long 

    ' If we haven't yet created our buffer, do so now, based on the size of the incoming data. 
    If m_nDataReceived = 0 Then 
     nContentLength = CLng(Inet.GetHeader("Content-length")) 
     m_sData = Space$(nContentLength) 

     ' You might want to do a check on the content type here, and if it is wrong, cancel the request with Inet.Cancel . 
     sContentType = Inet.GetHeader("Content-type") 
    End If 

    ' Retrieve data until we have all the data. 
    Do Until m_nDataReceived = Len(m_sData) 

     ' If called when not all data has been received, then exit function if it is currently executing. 
     If Not the_bCompleted Then 
      If Inet.StillExecuting Then 
       Debug.Print "Exiting" 
       Exit Sub 
      End If 
     End If 

     ' Get a chunk, copy it into the output buffer, and increment the amount of data received. 
     sChunk = Inet.GetChunk(knBufferSize, DataTypeConstants.icString) 
     nChunkSize = Len(sChunk) 
     Mid$(m_sData, m_nDataReceived + 1, nChunkSize) = sChunk 
     m_nDataReceived = m_nDataReceived + nChunkSize 

    Loop 

    ' Flag that all data has been retrieved. 
    m_bDataReceived = True 

End Sub 

' Encode the URL data. 
Private Function UrlEncode(ByVal the_sURLData As String) As String 

    Dim nBufferLen      As Long 
    Dim sBuffer       As String 

    ' Only exception - encode spaces as "+". 
    the_sURLData = Replace$(the_sURLData, " ", "+") 

    ' Try to #-encode the string. 
    ' Reserve a buffer. Maximum size is 3 chars for every 1 char in the input string. 
    nBufferLen = Len(the_sURLData) * 3 
    sBuffer = Space$(nBufferLen) 
    If InternetCanonicalizeUrl(StrPtr(the_sURLData), StrPtr(sBuffer), nBufferLen, 0&) Then 
     UrlEncode = Left$(sBuffer, nBufferLen) 
    Else 
     UrlEncode = the_sURLData 
    End If 

End Function