重要提示:本文章是 Microsoft 軟件自動翻譯的結(jié)果,而非專業(yè)譯者翻譯的結(jié)果。 Microsoft 提供專業(yè)人員翻譯的文章、由自動翻譯生成的文章以及來自 Microsoft 社區(qū)的文章翻譯,因此你能夠以自己的語言閱讀所有知識庫文章。 需要注意的是,由自動翻譯生成的文章(包括 Microsoft 社區(qū)審閱的文章)可能包含詞匯、句法或語法錯誤。 對于因不正確的內(nèi)容翻譯或使用不正確的內(nèi)容翻譯而造成的不準確或錯誤或任何損害,Microsoft 概不負責(zé)。
VALUE = Environ$(NAME)
Public Declare Function stdin Lib "kernel32" Alias "GetStdHandle" _
(Optional ByVal Handletype As Long = STD_INPUT_HANDLE) As Long
Public Declare Function stdout Lib "kernel32" Alias "GetStdHandle" _
(Optional ByVal Handletype As Long = STD_OUTPUT_HANDLE) As Long
Public Declare Function ReadFile Lib "kernel32" _
(ByVal hFile As Long, ByVal lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, Optional ByVal lpOverlapped As Long = 0&) As Long
Public Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, ByVal lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, Optional ByVal lpOverlapped As Long = 0&) As Long
Public Const STD_INPUT_HANDLE = -10&
Public Const STD_OUTPUT_HANDLE = -11&
Option Explicit
Public Const STD_INPUT_HANDLE = -10&
Public Const STD_OUTPUT_HANDLE = -11&
Public Const CGI_AUTH_TYPE As String = "AUTH_TYPE"
Public Const CGI_CONTENT_LENGTH As String = "CONTENT_LENGTH"
Public Const CGI_CONTENT_TYPE As String = "CONTENT_TYPE"
Public Const CGI_GATEWAY_INTERFACE As String = "GATEWAY_INTERFACE"
Public Const CGI_HTTP_ACCEPT As String = "HTTP_ACCEPT"
Public Const CGI_HTTP_REFERER As String = "HTTP_REFERER"
Public Const CGI_HTTP_USER_AGENT As String = "HTTP_USER_AGENT"
Public Const CGI_PATH_INFO As String = "PATH_INFO"
Public Const CGI_PATH_TRANSLATED As String = "PATH_TRANSLATED"
Public Const CGI_QUERY_STRING As String = "QUERY_STRING"
Public Const CGI_REMOTE_ADDR As String = "REMOTE_ADDR"
Public Const CGI_REMOTE_HOST As String = "REMOTE_HOST"
Public Const CGI_REMOTE_USER As String = "REMOTE_USER"
Public Const CGI_REQUEST_METHOD As String = "REQUEST_METHOD"
Public Const CGI_SCRIPT_NAME As String = "SCRIPT_NAME"
Public Const CGI_SERVER_NAME As String = "SERVER_NAME"
Public Const CGI_SERVER_PORT As String = "SERVER_PORT"
Public Const CGI_SERVER_PROTOCOL As String = "SERVER_PROTOCOL"
Public Const CGI_SERVER_SOFTWARE As String = "SERVER_SOFTWARE"
Public Declare Function Sleep Lib "kernel32" _
(ByVal dwMilliseconds As Long) As Long
Public Declare Function stdin Lib "kernel32" Alias "GetStdHandle" _
(Optional ByVal Handletype As Long = STD_INPUT_HANDLE) As Long
Public Declare Function stdout Lib "kernel32" Alias "GetStdHandle" _
(Optional ByVal Handletype As Long = STD_OUTPUT_HANDLE) As Long
Public Declare Function ReadFile Lib "kernel32" _
(ByVal hFile As Long, ByVal lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, Optional ByVal lpOverlapped As Long = 0&) As Long
Public Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, ByVal lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, Optional ByVal lpOverlapped As Long = 0&) As Long
Sub Main()
Dim sReadBuffer As String
Dim sWriteBuffer As String
Dim lBytesRead As Long
Dim lBytesWritten As Long
Dim hStdIn As Long
Dim hStdOut As Long
Dim iPos As Integer
' sleep for one minute so the debugger can attach and set a break
' point on line below
' Sleep 60000
sReadBuffer = String$(CLng(Environ$(CGI_CONTENT_LENGTH)), 0)
' Get STDIN handle
hStdIn = stdin()
' Read client's input
ReadFile hStdIn, sReadBuffer, Len(sReadBuffer), lBytesRead
' Find '=' in the name/value pair and parse the buffer
iPos = InStr(sReadBuffer, "=")
sReadBuffer = Mid$(sReadBuffer, iPos + 1)
' Construct and send response to the client
sWriteBuffer = "HTTP/1.0 200 OK" & vbCrLf & "Content-Type: text/html" & _
vbCrLf & vbCrLf & "Hello "
hStdOut = stdout()
WriteFile hStdOut, sWriteBuffer, Len(sWriteBuffer) + 1, lBytesWritten
WriteFile hStdOut, sReadBuffer, Len(sReadBuffer), lBytesWritten
End Sub
<HTML>
<HEAD>
<TITLE>Testing VB CGI</TITLE>
</HEAD>
<BODY>
<FORM action="/cgi-bin/hello.exe" method="POST">
<INPUT TYPE="TEXT" NAME="Name"> Name<BR>
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>
標(biāo)準的.exe 項目創(chuàng)建一個新項目。
從項目中刪除窗體。
將模塊添加到該項目并將其命名為 HELLO。
設(shè)為啟動對象 (在項目屬性中) 的 Sub Main。
復(fù)制上面的 Visual Basic 代碼并將其粘貼到模塊。
使 Hello.exe。
該代碼示例演示如何處理 HTTP POST 請求。若要處理一個 GET 請求,CGI 應(yīng)用程序需要檢索 QUERY_STRING 環(huán)境變量。QUERY_STRING 變量包含和格式由分隔的名稱/值對"名稱 = Joe 和顏色 = Red。"請注意,使用 URL 編碼,所有的空格將被轉(zhuǎn)換為+,如!的所有特殊字符會轉(zhuǎn)換為其十六進制 ASCII 值。換句話說,將字符串"Hello,世界 !"表示為"Hello,+ 全球 21%。"Visual Basic CGI 應(yīng)用程序必須實現(xiàn)所有分析的代碼。
服務(wù)啟動時的 CGI 應(yīng)用程序,因為它可能不能訪問網(wǎng)絡(luò)共享。
請注意 CGI 運行作為一個服務(wù),它與服務(wù)器進行通信。因此,視覺界面窗體、 控件和消息框是完全沒有意義。事實上,消息框?qū)?dǎo)致一個 CGI 應(yīng)用程序停止響應(yīng)。
以便不會顯示默認的錯誤消息框應(yīng)該在 Visual Basic 中的 CGI 代碼進行錯誤處理。可以在服務(wù)器上的錯誤消息記錄,也可以將它們寫到用戶的瀏覽器。
Visual C 調(diào)試器可以調(diào)試在 Visual Basic 編寫的應(yīng)用程序。因此,您可以使用 CGI 調(diào)試下面提到的技術(shù)。調(diào)試與 Visual C Visual Basic 應(yīng)用程序,選擇將編譯為本機代碼,,然后選擇創(chuàng)建符號調(diào)試信息并無優(yōu)化。完成并生成.exe 時,Visual C 可以附加到運行 CGI 應(yīng)用程序用 Visual Basic 編寫的。
若要測試 CGI 應(yīng)用程序,請將其復(fù)制到 IIS 虛擬目錄的執(zhí)行權(quán)限。
請注意,運行時錯誤或?qū)υ捒蛑械?Visual Basic 代碼可能導(dǎo)致 CGI 應(yīng)用程序停止響應(yīng)。 如果 CGI 應(yīng)用程序停止響應(yīng),則可以在 Visual Studio 調(diào)試器中運行。的其他信息,請單擊下面的文章編號,以查看 Microsoft 知識庫中相應(yīng)的文章:
聯(lián)系客服