XMLHTTP

xiaoxiao2021-03-06  19

XMLHTTP object and method thereof

Reprinted: http://blog.9cbs.net/pazll/archive/2004/11/182551.aspx ------------------MsXML provides Microsoft.xmlhttp Objects can complete the conversion from packets to the REQUEST object and send tasks. The following statement creates XMLHTTP object: Set objXML = CreateObject ( "Msxml2.XMLHTTP") or Set objXML = CreateObject ( "Microsoft.XMLHTTP") 'Or, for version 3.0 of XMLHTTP, use:' Set xml = Server.CreateObject ( " MSXML2.ServerXmlhttp ") Object creation After calling the Open method to initialize the Request object, the syntax format is: poster.open http-method, url, asken http-method, URL, ASYNC, UserId, the Password Open method contains 5 parameters, the top three are necessary The latter two is optional (provided when the server needs to be authenticated). The meaning of the parameter is as follows: http-method: HTTP communication method, such as a GET or POST URL: The URL address of the server that receives XML data. Typically in the URL to indicate an ASP or CGI program async: a Boolean identity, indicating whether the request is asynchronous. If it is asynchronous communication mode (TRUE), the client will not wait for the server response; if it is a synchronous mode (FALSE), the client will wait until the server returns the message to perform other operations Userid user ID, for server authentication Password User password, the Send method for server authentication XMLHTTP object After initialization of the REQUEST object with the Open method, call the Send method to send XML data: Poster.Send XML-Data Send method's parameter type is Variant, which can be a string, DOM Tree or any data stream. The way to send data is divided into synchronous and asynchronous. In an asynchronous mode, once the data package is sent, end the Send process, the client performs other operations; and in the synchronization mode, the client will wait until the server returns a confirmation message to end the Send process. The ReadyState property in the XMLHTTP object reflects the progress of the server during processing the request. The program of the client can set the corresponding event processing method according to this status information. The attribute value and its meaning are shown in the following table: Value Description 0 Response object has been created, but the XML document upload process has not ended 1 XML document has been loaded 2 XML document has been loaded, and the 3 part XML document has parsed 4 documents already After the analysis is complete, the client can accept the return message client to process the response information client to receive a return message, basically complete a interaction cycle between C / S. The client receiving response is implemented by the attribute of the XMLHTTP object: ● ResponseTxt: Take the message as a text string; ● ResponseXML: Use the return message as an XML document, use XML data in the server response message; ● ResponseStream: The return message is treated as a Stream object.

<%

HEHE = Hello ("http://mmsg.qq.com/cgi-bin/gddylist?type=13" "" "," ",". * ( (. *) ) [./ n] *", " $ 2
") Response.write HEHE

Function Hello (Strull, Strstart, Strend, PATRN, REPLSTR) Str = getBody (Strurl) Str = MyMID (Str, StRSTART, STREND) STR = Replacestest (PATRN, REPLSTR, STR) Hello = Strend Function

Function mymid (str, strstart, strend) if strstart = "" THEN i = 0 else i = instr (str, str, str, str, str) ELSE J = INSTR (I, STR, STREND) End if mymid = MID (STR, I, J - I 1) End Function

Function ReplaceTest (patrn, replStr, str1) Dim regEx, match, matches Set regEx = New RegExp regEx.Pattern = patrn regEx.IgnoreCase = True regEx.Global = True Set matches = regEx.Execute (str1) For Each match in matches ReplaceTest = Replacestest & Regex.replace (Match.Value, Replstr) Nextend Function

Function GetBody (Url) Set objXML = CreateObject ( "Microsoft.XMLHTTP") With objXML .Open "Get", Url, False, "", "" .SEnd GetBody = .ResponseBody End With GetBody = BytesToBstr (GetBody, "GB2312" ) Set objxml = Nothingend function

Function BytesToBstr (strBody, CodeBase) Set objStream = Server.CreateObject ( "Adodb.Stream") With objStream .Type = 1 .Mode = 3 .Open .Write strBody .Position = 0 .Type = 2 .Charset = CodeBase BytesToBstr =. ReadText .close endhingend function%> Other call example: Hehe = Hello ("http://list.mp3.baidu.com/song/a.htm", "

","
"," (
) (. *) ( ) [./ n ] * "," $ 2
") <%

HEHE = Hello ("http://mmsg.qq.com/cgi-bin/gddylist?type=13" "" "," ",". * (

(. *) ) [./ n] *", " $ 2
") Response.write HEHE

Function Hello (Strull, Strstart, Strend, PATRN, REPLSTR) Str = getBody (Strurl) Str = MyMID (Str, StRSTART, STREND) STR = Replacestest (PATRN, REPLSTR, STR) Hello = Strend Function

Function mymid (str, strstart, strend) if strstart = "" THEN i = 0 else i = instr (str, str, str, str, str) ELSE J = INSTR (I, STR, strend) End If MyMid = Mid (Str, i, j - i 1) End FunctionFunction ReplaceTest (patrn, replStr, str1) Dim regEx, match, matches Set regEx = New RegExp regEx.Pattern = patrn regEx.IgnoreCase = True regEx .Global = true set matches = regex.execute (str1) for each match in matches replacestest = Replacestest & Regex.replace (Match.Value, Replstr) Nextend Function

Function GetBody (Url) Set objXML = CreateObject ( "Microsoft.XMLHTTP") With objXML .Open "Get", Url, False, "", "" .SEnd GetBody = .ResponseBody End With GetBody = BytesToBstr (GetBody, "GB2312" ) Set objxml = Nothingend function

转载请注明原文地址:https://www.9cbs.com/read-39779.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.035, SQL: 9