Use XMLHTTP to add the stock market query function for websites

Source: Internet
Author: User
Tags dns2

In general, if you want to know the stock market, you must log on to the professional stock website and enter the company code to see the specific stock price. In fact, with the XMLHTTP Protocol, each of us can provide the stock market details query service on our personal website. This article describes how to use ASP, VBScript, and XMLHTTP Programming to Implement this function.

I. Principles
The principle is very simple. In many large websites, stock market information is updated in real time. We use our web server to send XMLHTTP commands to access the stock market database server of Yahoo financial website, query the required content from the database, and then combine the content into a webpage through ASP and send it back to the client.
At the same time, we use the XMLHTTP protocol to achieve the refreshing effect of client web pages and to request data retrieval from the server.
XMLHTTP allows you to open an HTTP connection to any server, send some data and retrieve some data. All of this can be implemented in a few scripts. XMLHTTP objects are usually used for XML data exchange, but data in other formats is also allowed.
The standard mode of using this exchange type in business programs is that the client sends a text string to the server, and then the server loads the string into an xmldom object and explains it, then, return an HTML section to the client, or an XML code section to the client and let the client's browser explain it. This method is very effective for information transmission, especially when DHTML allows you to dynamically display the returned information.
The specific process is: the client requests the stock market query through XMLHTTP --> the ASP listening page on the web server receives the request and uses XMLHTTP to send the retrieval command to the stock market database server --> the database performs the query operation, return results to our web server (in HTML format) --> after the Web server receives the content, it filters out the HTML excess and selects the pure stock data, after the combination, the result is sent to the client.> the client browser displays the result using VBScript.

Ii. asp XMLHTTP Programming
1、client dns2.htm page:
<Script language = "VBScript"> "〉
Sub submit1_onmouseup
Dim objxml, objxsl, objfso, strfile, strfilename, strxsl, strurl, thestring, theform
Set theform = Document. wordfind
Window. Status = "retrieving data ??? "
Strurl = "dns2.asp" 'server listening page address
Set objxml = Createobject ("Microsoft. XMLHTTP") 'creates the XMLHTTP component of Ms.
Stra = "Submit = submit & FQDN =" & theform. webname. Value
Objxml. Open "Post", strurl, and false' are submitted in post mode;
Objxml. setRequestHeader "Content-Length", Len (stra)
Objxml. setRequestHeader "Content-Type", "application/X-WWW-form-urlencoded"
Objxml. Send stra 'send message
Xmlget = objxml. responsebody: Wait a moment and get the result from the server;
Xmlget = bytes2bstr (xmlget) 'converts a single byte to a text format function, which can be found in the dns2.asp program below;
Theform. Comments. value = xmlget
Set objxml = nothing
End sub
</SCRIPT> 〉

<HTML> </Head> 〉
<Body bgcolor = # a1bae6> 〉
<Table border = "0" cellpadding = "0" cellspacing = "0" width = "100%"> "〉
<Tr> 〉
<TD width = "100%" align = center> 〉
<Form method = "Post" name = wordfind onsubmit = "javascript: Return false"> "〉
<Font color = # 002e5b face = Geneva, Arial, sans-serif size = 2> 〉
<B> select the stock name (NASDAQ, USA) </B> </font> 〉
<Font color = # 002e5b face = Geneva, Arial, sans-serif size = 2> <B>: CHR (13) & CHR (10)
<Select name = webname style = "height: 22px; width: 80px"> "〉
<Option value = "^ ixic"> NASDAQ </option> <option value = "Asia"> Asiainfo </option> 〉
<Option value = "China"> chinacnet </option> <option value = "ntes"> Netease </option> 〉
<Option value = "Sina"> Sina </option> <option value = "Sohu"> Sohu </option> 〉
</SELECT> <input type = "Submit" value = "query" id = submit1 name = submit1> </B> 〉
<Textarea name = comments readonly rows = 10 Cols = 76> </textarea> 〉
</Form> </TD> </tr> </table> </body> 2. server-side dns2.asp program:
<%
Dim xmlget
Xmlget = ""
If Len (TRIM (request. Form ("FQDN")> 1 then
Dim objxml, objxsl, objfso
Dim strfile, strfilename, strxsl
Dim strurl, thestring

'Address of the stock market Database Server
Strurl = "http://cn.finance.yahoo.com/Q? S = "+ trim (request. Form (" FQDN ") +" & D = 2B"
Set objxml = Createobject ("Microsoft. XMLHTTP") 'creates the XMLHTTP component of Ms.
Objxml. Open "get", strurl, false
Objxml. Send' send message
Xmlget = objxml. responsebody wait a moment and you will receive the results from the stock market data server;
Xmlget = bytes2bstr (xmlget) 'is converted to a text format function, which can be found in the source program;

'Filter useless HTML components and select stock details;
Strfind1 = instr (1, xmlget, "Last transaction", 1) + 8
Strfind2 = instr (strfind1, xmlget, "<B>", 1) + 3
Strfind3 = instr (strfind2, xmlget, "</B> </TD>", 1)
Xmlget1 = "final transaction:" + mid (xmlget, strfind2, strFind3-strFind2) + CHR (13) + CHR (10) 'final transaction price

Strfind1 = instr (1, xmlget, "ups and downs", 1) + 4
Strfind2 = instr (strfind1, xmlget, "<font color =", 1) + 19
Strfind3 = instr (strfind2, xmlget, "</font>", 1)
Xmlget2 = "ups and downs:" + mid (xmlget, strfind2, strFind3-strFind2) + CHR (13) + CHR (10) 'ups and downs

Set objxml = nothing
End if
Response. Write trim (request. Form ("FQDN") + "Stock Market:" + CHR (13) + CHR (10) + xmlget1 + xmlget2
Function bytes2bstr (VIN)
Strreturn = ""
For I = 1 to lenb (VIN)
Thischarcode = ASCB (midb (Vin, I, 1 ))
If thischarcode <& h80 then
Strreturn = strreturn & CHR (thischarcode)
Else
Nextcharcode = ASCB (midb (VIN, I + 1, 1 ))
Strreturn = strreturn & CHR (clng (thischarcode) * & h100 + CINT (nextcharcode ))
I = I + 1
End if
Next
Bytes2bstr = strreturn
End Function
%> 〉

In the above process, we can find that the ASP program in our web server actually serves only one intermediate transmission function. In actual application, this intermediate layer can also be omitted, VBScript is used to send and receive data directly to the stock market database through XMLHTTP. However, in this case, the execution may fail because the permission is insufficient due to the disabled (default) Access to data resources through (other) domains in IE, therefore, the method for removing the ASP intermediate layer in this province is not very common.

This program runs on iis5.0 and ie6.0 Based on Windows2000 platform. In actual use, you can copy the above Code to Your webpage and edit it on a simple page, you can add the stock information query function without blinking on the page on your webpage. At first glance, it looks a bit like the service provided by the website of a professional securities service provider.

Appendix:
The source code of dns2.htm is as follows:

<Script language = "VBScript">
Sub submit1_onmousedown
Layer2.style. Visibility = "hidden"
Layer4.style. Visibility = "visible"
Document. wordfind. comments2.value = "Nasdaq (NASDAQ) is short for the name of the Automatic Quotation system created by the National Association of Securities Dealers in 1968. NASDAQ is characterized by the collection and release of quotations from securities dealers for foreign and non-listed stocks. It has become the world's largest securities trading market. Currently, there are more than 5200 listed companies. NASDAQ is the world's first stock market to adopt electronic trading. It has more than 0.26 million computer sales terminals in 55 countries and regions. "+ CHR (13) + CHR (10) + CHR (13) + CHR (10) +" retrieving data ···"
End sub

Sub submit1_onmouseup
Dim objxml, objxsl, objfso
Dim strfile, strfilename, strxsl
Dim strurl, thestring

Dim theform
Set theform = Document. wordfind
Window. Status = "retrieving data ···"

'Get the domain name address
'Strurl = "http://www.domainbank.net/whoisresults_gen.cfm? Show = 1"
Strurl = "dns2.asp"

'Create an XMLHTTP component for MS
Set objxml = Createobject ("Microsoft. XMLHTTP ")

'Send message

Stra = "Submit = submit & FQDN =" & theform. webname. Value
Objxml. Open "Post", strurl, false
Objxml. setRequestHeader "Content-Length", Len (stra)
Objxml. setRequestHeader "Content-Type", "application/X-WWW-form-urlencoded"
Objxml. Send stra

'Xmlget = objxml. responsetext
Xmlget = objxml. responsebody
Xmlget = bytes2bstr (xmlget)

Theform. Comments. value = xmlget

Set objxml = nothing
Layer2.style. Visibility = "visible"
Layer4.style. Visibility = "hidden"
Window. Status = ""
End sub

Function bytes2bstr (VIN)
Strreturn = ""
For I = 1 to lenb (VIN)
Thischarcode = ASCB (midb (Vin, I, 1 ))
If thischarcode <& h80 then
Strreturn = strreturn & CHR (thischarcode)
Else
Nextcharcode = ASCB (midb (VIN, I + 1, 1 ))
Strreturn = strreturn & CHR (clng (thischarcode) * & h100 + CINT (nextcharcode ))
I = I + 1
End if
Next
Bytes2bstr = strreturn
End Function
</SCRIPT>

<HTML> </Head>
<Body bgcolor = # a1bae6>
<Table border = "0" cellpadding = "0" cellspacing = "0" width = "100%">
<Tr>
& Lt; TD width = "100%" align = center & gt;
<Form method = "Post" name = wordfind onsubmit = "javascript: Return false">
<Font color = # 002e5b face = Geneva, Arial, sans-serif size = 2>
<B> select the stock name </B> </font>
<Font color = # 002e5b face = Geneva, Arial, sans-serif size = 2>
<B>, and then click "query. </B> </font>
<Font color = # 002e5b face = Geneva, Arial, sans-serif size = 2> <B >:< br>
<Select name = webname style = "height: 22px; width: 80px">
<Option value = "^ ixic"> NASDAQ </option>
<Option value = "Asia"> Asiainfo </option>
<Option value = "China"> Chinanet </option>
<Option value = "ntes"> Netease </option>
<Option value = "Sina"> Sina </option>
<Option value = "Sohu"> Sohu </option>
<Option value = "^ DJI"> dajens </option>
<Option value = "^ ssec"> Shanghai certificate delivery </option>
<Option value = "^ szsc1"> Shenzhen ingredients </option>
</SELECT>
<Input type = "Submit" value = "query" id = submit1 name = submit1> </B>
</Font>
<HR>
<Div id = "Layer2" style = "position: absolute; left: 100; width: 190; Height: 400; top: 100; Z-index: 2; visibility: hidden ">
<Textarea name = comments readonly rows = 10 Cols = 76 style = "border: 1px dashed #999999; Background-color: Transparent"> </textarea>
</Div>
<Div id = "layer4" style = "position: absolute; left: 100; width: 190; Height: 400; top: 100; Z-index: 2; visibility: visible ">
<Textarea name = comments2 readonly rows = 10 Cols = 76 style = "border: 1px dashed #999999; Background-color: Transparent">
Nasdaq (NASDAQ) is the name of the automatic quote system created by the National Association of Securities Dealers in 1968. NASDAQ is characterized by the collection and release of quotations from securities dealers for foreign and non-listed stocks. It has become the world's largest securities trading market. Currently, there are more than 5200 listed companies. NASDAQ is the world's first stock market to adopt electronic trading. It has more than 0.26 million computer sales terminals in 55 countries and regions.
</Textarea>
</Div>
<Br>
</Form>
</TD>
</Tr>
</Table>
</Body>

Dns2.asp source code:

<%
Dim xmlget
Xmlget = ""
If Len (TRIM (request. Form ("FQDN")> 1 then
Dim objxml, objxsl, objfso
Dim strfile, strfilename, strxsl
Dim strurl, thestring

'Get the address of the domain name bank
Strurl = "http://cn.finance.yahoo.com/Q? S = "+ trim (request. Form (" FQDN ") +" & D = 2B"
'Response. Write strurl
'Response. End

'Create an XMLHTTP component for MS
Set objxml = Createobject ("Microsoft. XMLHTTP ")

'Send message
Objxml. Open "get", strurl, false
Objxml. Send
'Xmlget = objxml. responsetext
Xmlget = objxml. responsebody

Xmlget = bytes2bstr (xmlget)

Strfind1 = instr (1, xmlget, "Last transaction", 1) + 8
Strfind2 = instr (strfind1, xmlget, "<B>", 1) + 3
Strfind3 = instr (strfind2, xmlget, "</B> </TD>", 1)
Xmlget1 = "final transaction:" + mid (xmlget, strfind2, strFind3-strFind2) + CHR (13) + CHR (10) 'final transaction price

Strfind1 = instr (1, xmlget, "ups and downs", 1) + 4
Strfind2 = instr (strfind1, xmlget, "<font color =", 1) + 19
Strfind3 = instr (strfind2, xmlget, "</font>", 1)
Xmlget2 = "ups and downs:" + mid (xmlget, strfind2, strFind3-strFind2) + CHR (13) + CHR (10) 'ups and downs

Strfind1 = accept B (1, xmlget, "accept", 0)
Strfind3 = reverse B (strfind1, xmlget, "</TD>", 0)
Xmlget3 = midb (xmlget, strfind1, strFind3-strFind1) + CHR (13) + CHR (10) 'receive
Xmlget3 = Replace (xmlget3, "<br> ",":")

Strfind1 = Transaction B (1, xmlget, "transaction volume", 0)
Strfind3 = reverse B (strfind1, xmlget, "</TD>", 0)
Xmlget4 = midb (xmlget, strfind1, strFind3-strFind1) + CHR (13) + CHR (10) 'transaction volume
Xmlget4 = Replace (xmlget4, "<br> ",":")

Set objxml = nothing

End if
Response. write "Beijing Time" + CSTR (now () + CHR (13) + CHR (10) + trim (request. form ("FQDN") + "Stock Market:" + CHR (13) + CHR (10) + xmlget1 + xmlget2 + xmlget3 + xmlget4

Function bytes2bstr (VIN)
Strreturn = ""
For I = 1 to lenb (VIN)
Thischarcode = ASCB (midb (Vin, I, 1 ))
If thischarcode <& h80 then
Strreturn = strreturn & CHR (thischarcode)
Else
Nextcharcode = ASCB (midb (VIN, I + 1, 1 ))
Strreturn = strreturn & CHR (clng (thischarcode) * & h100 + CINT (nextcharcode ))
I = I + 1
End if
Next
Bytes2bstr = strreturn
End Function
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.