VB6-使用URL的POST&GET并以VB6形式显示

Sec*_*oud 1 vb6 api winapi wininet

我的VB6如何形成POST 2变量,如何从URL中提取结果,然后将VB6变量分配给结果?

我需要有人向我展示非常基本的VB6示例代码,或为我指明正确的方向。这是最简单的形式-在最终产品中,PHP变量将写入MySQL,但这不是我需要的帮助。

我有一个简单的PHP页面,它接受2个参数:

test.php?var1=secret&var2=pass
Run Code Online (Sandbox Code Playgroud)

这是我非常简单的PHP代码

<?php

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

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

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

其背后的逻辑是使用“ userName”,“ passWord”和“ hardWareID”进行VB6登录,并发送哈希。将对照MySQL检查该散列,以查看其是否存在,并返回YES或NO以进行访问,其帐户还剩多少天以及其他一些详细信息,例如FULL NAME,ACCOUNT INFO等。

(不。我不想使用XML,只是以为我会把它放在那里。只需POST&Receive to vars)

谢谢...

Mar*_*haw 5

VB表单没有用于发送HTTP请求的任何内置机制。有些人可能建议您使用Internet传输控制。但是,VB UserControl具有用于HTTP的机制,假设您使用GET方法并使用查询字符串来传递参数,则无需第三方控件就可以使用该机制。如果必须使用POST,则必须使用Internet传输控制。

创建一个参考“ Microsoft Scripting Runtime”的VB项目(请参阅菜单Project => References)。添加一个用户控件。称之为“ 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
Run Code Online (Sandbox Code Playgroud)

要使用此UserControl,请将其添加到表单中。称之为“ HttpService”。添加一个名为“ txtOutput”的文本框,以测试表单上的以下代码:

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

txtOutput.Text = HttpService.Get_
Run Code Online (Sandbox Code Playgroud)