C/S模式一般都是用winsock来相互通信,实现双方互动。
用ASP网页来连接(不能称之为通信),只能C对S发送信息或请求一些信息,
S是不能主动来连接C,发信息给C。
一、C端
既然是要访问ASP页,这里有二种方式。
1、API写的访问模块
Option Explicit
''<API 宣告,API 常数>@H_403_15@Public Declare Function InternetOpen Lib "wininet.dll" _@H_403_15@ Alias "InternetOpenA" _@H_403_15@ (ByVal lpszCallerName As String,_@H_403_15@ ByVal dwAccessType As Long,_@H_403_15@ ByVal lpszProxyName As String,_@H_403_15@ ByVal lpszProxyBypass As String,_@H_403_15@ ByVal dwFlags As Long) As Long@H_403_15@ @H_403_15@ Public Declare Function InternetConnect Lib "wininet.dll" _@H_403_15@ Alias "InternetConnectA" _@H_403_15@ (ByVal hInternetSession As Long,_@H_403_15@ ByVal lpszServerName As String,_@H_403_15@ ByVal nProxyPort As Integer,_@H_403_15@ ByVal lpszUsername As String,_@H_403_15@ ByVal lpszPassword As String,_@H_403_15@ ByVal dwService As Long,_@H_403_15@ ByVal dwFlags As Long,_@H_403_15@ ByVal dwContext As Long) As Long@H_403_15@ @H_403_15@ Public Declare Function InternetReadFile Lib "wininet.dll" _@H_403_15@ (ByVal hFile As Long,_@H_403_15@ ByVal sBuffer As String,_@H_403_15@ ByVal lNumBytesToRead As Long,_@H_403_15@ lNumberOfBytesRead As Long) As Integer@H_403_15@ @H_403_15@ Public Declare Function HttpOpenRequest Lib "wininet.dll" _@H_403_15@ Alias "HttpOpenRequestA" _@H_403_15@ (ByVal hInternetSession As Long,_@H_403_15@ ByVal lpszVerb As String,_@H_403_15@ ByVal lpszObjectName As String,_@H_403_15@ ByVal lpszVersion As String,_@H_403_15@ ByVal lpszReferer As String,_@H_403_15@ ByVal lpszAcceptTypes As Long,_@H_403_15@ ByVal dwContext As Long) As Long@H_403_15@ @H_403_15@ Public Declare Function HttpSendRequest Lib "wininet.dll" _@H_403_15@ Alias "HttpSendRequestA" _@H_403_15@ (ByVal hHttpRequest As Long,_@H_403_15@ ByVal sHeaders As String,_@H_403_15@ ByVal lHeadersLength As Long,_@H_403_15@ ByVal sOptional As String,_@H_403_15@ ByVal lOptionalLength As Long) As Boolean@H_403_15@ @H_403_15@ Public Declare Function InternetCloseHandle Lib "wininet.dll" _@H_403_15@ (ByVal hInternetHandle As Long) As Boolean@H_403_15@ @H_403_15@ Public Declare Function HttpAddRequestHeaders Lib "wininet.dll" _@H_403_15@ Alias "HttpAddRequestHeadersA" _@H_403_15@ (ByVal hHttpRequest As Long,_@H_403_15@ ByVal lModifiers As Long) As Integer@H_403_15@ @H_403_15@ Public Function PostInfo(ByVal WebServerAddress As String,_@H_403_15@ ByVal Port As String,_@H_403_15@ ByVal WebPageName As String,_@H_403_15@ ByVal CommandString As String,_@H_403_15@ Optional ByVal PostData As String = "") As String@H_403_15@ @H_403_15@ Dim hInternetOpen As Long@H_403_15@ Dim hInternetConnect As Long@H_403_15@ Dim hHttpOpenRequest As Long@H_403_15@ Dim bRet As Boolean@H_403_15@ @H_403_15@ hInternetOpen = 0@H_403_15@ hInternetConnect = 0@H_403_15@ hHttpOpenRequest = 0@H_403_15@ @H_403_15@ 'Use registry access settings.@H_403_15@ Const INTERNET_OPEN_TYPE_PRECONFIG = 0@H_403_15@ @H_403_15@ hInternetOpen = InternetOpen("http generic",_@H_403_15@ INTERNET_OPEN_TYPE_PRECONFIG,_@H_403_15@ vbNullString,_@H_403_15@ 0)@H_403_15@ @H_403_15@ If hInternetOpen <> 0 Then@H_403_15@ 'Type of service to access.@H_403_15@ Const INTERNET_SERVICE_HTTP = 3@H_403_15@ Const INTERNET_DEFAULT_HTTP_PORT = 80@H_403_15@ 'Change the server to your server name@H_403_15@ hInternetConnect = InternetConnect(hInternetOpen,_@H_403_15@ WebServerAddress,_@H_403_15@ Port,_@H_403_15@ "HTTP/1.0",_@H_403_15@ INTERNET_SERVICE_HTTP,_@H_403_15@ 0,_@H_403_15@ 0)@H_403_15@ @H_403_15@ If hInternetConnect <> 0 Then@H_403_15@ 'Brings the data across the wire even if it locally cached.@H_403_15@ Const INTERNET_FLAG_RELOAD = &H80000000@H_403_15@ hHttpOpenRequest = HttpOpenRequest(hInternetConnect,_@H_403_15@ "POST",_@H_403_15@ WebPageName & CommandString,_@H_403_15@ INTERNET_FLAG_RELOAD,_@H_403_15@ 0)@H_403_15@ @H_403_15@ If hHttpOpenRequest <> 0 Then@H_403_15@ Dim sHeader As String@H_403_15@ Const HTTP_ADDREQ_FLAG_ADD = &H20000000@H_403_15@ Const HTTP_ADDREQ_FLAG_REPLACE = &H80000000@H_403_15@ sHeader = "Content-Type: application/x-www-form-urlencoded" & vbCrLf@H_403_15@ 'sHeader = "Content-Type: multipart/form-data" & vbCrLf@H_403_15@ bRet = HttpAddRequestHeaders(hHttpOpenRequest,_@H_403_15@ sHeader,Len(sHeader),HTTP_ADDREQ_FLAG_REPLACE _@H_403_15@ Or HTTP_ADDREQ_FLAG_ADD)@H_403_15@ @H_403_15@ Dim lpszPostData As String@H_403_15@ Dim lPostDataLen As Long@H_403_15@ @H_403_15@ lpszPostData = PostData@H_403_15@ 'lPostDataLen = Len(lpszPostData)@H_403_15@ lPostDataLen = LenB(StrConv(lpszPostData,vbFromUnicode))@H_403_15@ bRet = HttpSendRequest(hHttpOpenRequest,_@H_403_15@ lpszPostData,_@H_403_15@ lPostDataLen)@H_403_15@ @H_403_15@ Dim bDoLoop As Boolean@H_403_15@ Dim sReadBuffer As String * 4096@H_403_15@ Dim lNumberOfBytesRead As Long@H_403_15@ Dim sBuffer As String@H_403_15@ bDoLoop = True@H_403_15@ While bDoLoop@H_403_15@ sReadBuffer = vbNullString@H_403_15@ bDoLoop = InternetReadFile(hHttpOpenRequest,_@H_403_15@ sReadBuffer,Len(sReadBuffer),lNumberOfBytesRead)@H_403_15@ sBuffer = sBuffer & _@H_403_15@ Left(sReadBuffer,lNumberOfBytesRead)@H_403_15@ If Not CBool(lNumberOfBytesRead) Then bDoLoop = False@H_403_15@ Wend@H_403_15@ PostInfo = sBuffer@H_403_15@ bRet = InternetCloseHandle(hHttpOpenRequest)@H_403_15@ End If@H_403_15@ bRet = InternetCloseHandle(hInternetConnect)@H_403_15@ End If@H_403_15@ bRet = InternetCloseHandle(hInternetOpen)@H_403_15@ End If@H_403_15@ PostInfo = sBuffer@H_403_15@ End Function@H_403_15@ @H_403_15@ Public Sub SplitAddr(ByVal addr$,srv$,script$)@H_403_15@ 'Inputs: The full url including http://@H_403_15@ ' Two variables that will be changed@H_403_15@ '@H_403_15@ 'Returns: Splits the addr$ var into the server name@H_403_15@ ' and the script path@H_403_15@ @H_403_15@ Dim i%@H_403_15@ @H_403_15@ i = InStr(addr$,"/")@H_403_15@ srv$ = Mid(addr$,i + 2,Len(addr$) - (i + 1))@H_403_15@ i = InStr(srv$,"/")@H_403_15@ script$ = Mid(srv$,i,Len(srv$) + 1 - i)@H_403_15@ srv$ = Left$(srv$,i - 1)@H_403_15@ @H_403_15@ End Sub@H_403_15@以上代码可以在网上搜索到,我修改了一下,支持中文传输。
把以上代码放到一个模块中即可。
调用PostInfo时,给出正确的参数即可。
2、可以用XMLHTTP来访问,我给出一个我实际用的upload file的函数
Public Function UploadFile() As Boolean@H_403_15@On Error GoTo UploadFile_Error
m_UploadMessage = ""@H_403_15@ If Len(m_sFileName) = 0 Then@H_403_15@ m_UploadMessage = "未设置上传的文件。"@H_403_15@ UploadFile = False@H_403_15@ Exit Function@H_403_15@ End If@H_403_15@ @H_403_15@ Dim XMLDocument As New DOMDocument@H_403_15@ Dim ADOStream As New Stream@H_403_15@ Dim DOMElement As IXMLDOMElement@H_403_15@ Dim MyXMLHttp As New XMLHTTP@H_403_15@ @H_403_15@ XMLDocument.loadXML ("<?xml version=""1.0"" ?> <root/>")@H_403_15@ XMLDocument.documentElement.setAttribute "xmlns:dt","urn:schemas-microsoft-com:datatypes"@H_403_15@ Set DOMElement = XMLDocument.createElement("File")@H_403_15@ DOMElement.dataType = "bin.base64"@H_403_15@ ADOStream.Type = adTypeBinary@H_403_15@ ADOStream.Open@H_403_15@ ADOStream.LoadFromFile m_sFileName@H_403_15@ DOMElement.nodeTypedValue = ADOStream.Read@H_403_15@ ADOStream.Close@H_403_15@ XMLDocument.documentElement.appendChild DOMElement
@H_403_15@ MyXMLHttp.Open "POST",m_sWebPage,False,m_sUserName,m_sPassWord@H_403_15@ MyXMLHttp.send XMLDocument@H_403_15@ m_UploadMessage = MyXMLHttp.responseText@H_403_15@ If m_UploadMessage = "OK" Then@H_403_15@ m_UploadMessage = ""@H_403_15@ UploadFile = True@H_403_15@ Else@H_403_15@ UploadFile = False@H_403_15@ End If@H_403_15@ @H_403_15@UploadFile_Exit:@H_403_15@ m_sFileName = ""@H_403_15@ Set XMLDocument = Nothing@H_403_15@ Set ADOStream = Nothing@H_403_15@ Set DOMElement = Nothing@H_403_15@ Set MyXMLHttp = Nothing@H_403_15@ Exit Function@H_403_15@ @H_403_15@UploadFile_Error:@H_403_15@ UploadFile = False@H_403_15@ Debug.Print CStr(Now()) & "--->" & vbCrLf & _@H_403_15@ "ErrAddress: VanFileTransfer->VanFileDownLoad->UploadFile" & vbCrLf & _@H_403_15@ "ErrNumber: " & Err.Number & vbCrLf & _@H_403_15@ "Description: " & Err.Description & vbCrLf@H_403_15@ Err.Clear@H_403_15@ Resume UploadFile_Exit@H_403_15@ @H_403_15@End Function
同样,代码中的m_UploadMessage就是返回来的信息.
二、接着说S端
需要引用microsoft active server pages object library
1、添加ASP页的接口类
Private gMyServer As Server@H_403_15@Private gMySession As Session@H_403_15@Private gMyApplication As Application@H_403_15@Private gMyRequest As Request@H_403_15@Private gMyResponse As Response
这几个是ASP的对象,会ASP的人一看就知道。
Public Function OnStartPage(MyScriptingContext As ScriptingContext)@H_403_15@ Set gMyServer = MyScriptingContext.Server@H_403_15@ Set gMySession = MyScriptingContext.Session@H_403_15@ Set gMyApplication = MyScriptingContext.Application@H_403_15@ Set gMyRequest = MyScriptingContext.Request@H_403_15@ Set gMyResponse = MyScriptingContext.Response@H_403_15@End Function
Public Function OnEndPage()@H_403_15@ Set gMyServer = Nothing@H_403_15@ Set gMySession = Nothing@H_403_15@ Set gMyApplication = Nothing@H_403_15@ Set gMyRequest = Nothing@H_403_15@ Set gMyResponse = Nothing@H_403_15@End Function@H_403_15@这二个是ASP加载时和关闭时事件。我们用来得到对象实例和释放对象。
下面就是入口函数
public function MySubMain()
'gMyRequest中就是C给过来的一些数据,处理他
gMyResponse.Write “返回给C的数据”
end function
三、ASP页内容
<%@H_403_15@ setMyObject = server.createobject("工程名.接口类名")@H_403_15@ MyObject.MySubMain@H_403_15@ set MyObject = nothing@H_403_15@%>
把ASP页架起来就OK了。
C/S就可以通信了。
PS:如果有兴趣的朋友,可以向我索要最简单的完整实例。