|
|
Across the Web, many websites offer users the ability to retrieve stock information for a specific company. To comply with securities regulations, the stock information is delayed by 15 minutes.
The StockQuote web service, available from the XMethods website at www.xmethods.com, retrieves delayed stock prices for the company that corresponds to a stock symbol, such as MSFT for Microsoft.
Note | The XMethods website at www.xmethods.com provides many web services you can integrate into your applications. Take time to visit the XMethods website—you will likely find several web services you can put to immediate use. |
The ASP.NET page StockPrice.aspx, which you can run from this book’s companion website, displays the form shown in Figure 1.7 that contains buttons corresponding to several software companies. When the user clicks a button, the page will display the company’s (delayed) stock price.
The StockQuote web service supports one method, getQuote, which returns a value of type Float that corresponds to a company’s stock price. Your programs pass the stock symbol, such as MSFT for Microsoft, to the method as a parameter:
float getQuote(string symbol)
Figure 1.7: Using the StockQuote web service to retrieve stock prices
If the symbol your program passes to getQuote is invalid, getQuote will return the value -1. The source code in Listing 1.2 implements the ASP.NET page StockPrice.aspx.
Listing 1.2 StockPrice.aspx
![]() |
Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents Button2 As System.Web.UI.WebControls.Button Protected WithEvents Button3 As System.Web.UI.WebControls.Button Protected WithEvents Button4 As System.Web.UI.WebControls.Button Protected WithEvents Button5 As System.Web.UI.WebControls.Button Protected WithEvents Label2 As System.Web.UI.WebControls.Label Protected WithEvents Button6 As System.Web.UI.WebControls.Button #Region " Web Form Designer Generated Code " 'Code not show #End Region Private Sub Button1_Click(ByVal sender As System.Object, _ Ä ByVal e As System.EventArgs) Handles Button1.Click ShowStockPrice("MSFT") End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ Ä ByVal e As System.EventArgs) Handles Button2.Click ShowStockPrice("ORCL") End Sub Private Sub Button3_Click(ByVal sender As System.Object, _ Ä ByVal e As System.EventArgs) Handles Button3.Click ShowStockPrice("YHOO") End Sub Private Sub Button4_Click(ByVal sender As System.Object, _ Ä ByVal e As System.EventArgs) Handles Button4.Click ShowStockPrice("BMC") End Sub Private Sub Button5_Click(ByVal sender As System.Object, _ Ä ByVal e As System.EventArgs) Handles Button5.Click ShowStockPrice("INTC") End Sub Private Sub Button6_Click(ByVal sender As System.Object, _ Ä ByVal e As System.EventArgs) Handles Button6.Click ShowStockPrice("CSCO") End Sub Private Function ShowStockPrice(ByVal Symbol As String) _ Ä As String Dim StockQuote As New _ Ä net.xmethods.services.netxmethodsservicesstockquoteStockQuoteService()_ Dim Price As String Price = StockQuote.getQuote(Symbol) Label2.Text = "Current Price: " & Price End Function End Class
![]() |
As you can see, the code provides event handlers for each of the buttons. Within the handler, the code calls the ShowStockPrice function, passing to the function a stock symbol that corresponds to a specific company. Within the ShowStockPrice function, the following statement creates a variable named StockQuote that corresponds to the object the code will use to interact with the StockQuote object:
Dim StockQuote As New _ Ä net.xmethods.services.netxmethodsservices_ Ä stockquoteStockQuoteService()
To call the getQuote method, the code uses the StockQuote variable as follows:
Price = StockQuote.getQuote(Symbol)
The following C# program, GetQuote.cs, displays a form that prompts the user for a company stock symbol. After the user enters the symbol and clicks the Get Stock Price button, the program will display the stock price, as shown in Figure 1.8.
Figure 1.8: Using the StockQuote web service within a C# program
To create the GetQuote.cs program, perform these steps:
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
Within the New Project dialog box Project Types list, click Visual C# Projects. Then, within the Templates field, click Windows Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name GetQuote. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls (label, buttons, and text box).
Using the Toolbox, drag and drop the label, buttons, and text box previously shown in Figure 1.8 onto the form.
To assign a Web Reference that corresponds to the object, select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.
Within the Address field, type the URL of the service’s WSDL file. In this case, type http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.
Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code (near the bottom of the class definition), add the following program statements:
private void button1_Click(object sender, System.EventArgs e) { float Price; net.xmethods.services.netxmethodsservicesstockquote Ä StockQuoteService Quote; Quote = new Ä net.xmethods.services.netxmethodsservicesstockquoteStockQuoteService(); if (textBox1.Text.Length == 0) label2.Text = "Must specify stock symbol"; else { try { Price = Quote.getQuote(textBox1.Text); if (Price == -1) label2.Text = "Invalid symbol"; else label2.Text = "Current price: " + Price.ToString(); } catch(Exception ex) { label2.Text = "Web service exception" + ex.Message; } } }
The previous program used the StockQuote web service to retrieve a stock price for display. The following C# program, NotifyMe.cs, again prompts the user to enter a stock symbol. After the user enters the stock information, the user can minimize the program. The program, behind the scenes, will “wake up” every 15 seconds and compare the stock’s current price to the original price. If the stock’s price has changed by one dollar (either up or down), the program will bring the form to the top of any active programs. If the user has minimized the program, the program will highlight the program’s icon within the Taskbar. See Figure 1.9.
Figure 1.9: Using a web service within a program that performs background processing
To create the NotifyMe.cs program, perform these steps:
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
Within the New Project dialog box Project Types list, click Visual C# Projects. Then, within the Templates field, click Windows Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name NotifyMe. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls (label, buttons, and text box).
Using the Toolbox, drag and drop the label, buttons, and text box previously shown in Figure 1.9 onto the form. Then, drag a Timer control onto the form.
Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.
Within the Address field, type the URL http://services.xmethods.net/soap/ urn:xmethods-delayed-quotes.wsdl and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.
Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code (near the bottom of the class definition), add the following program statements:
float OriginalPrice; float DollarChange; net.xmethods.services.netxmethodsservicesstockquote Ä StockQuoteService Quote; private void button1_Click(object sender, System.EventArgs e) { float Price; Quote = new Ä net.xmethods.services.netxmethodsservicesstockquoteStockQuoteService(); if (textBox1.Text.Length == 0) label4.Text = "Must specify stock symbol"; else { textBox1.ReadOnly = true; button1.Enabled = false; try { Price = Quote.getQuote(textBox1.Text); OriginalPrice = Price; if (Price == -1) label4.Text = "Invalid symbol"; else { label4.Text = "Original price: " + OriginalPrice.ToString(); timer1.Interval = 15000; timer1.Enabled = true; DollarChange = 0; } } catch(Exception ex) { label4.Text = "Web service exception" + ex.Message; } } } private void timer1_Tick(object sender, System.EventArgs e) { float Price; try { Price = Quote.getQuote(textBox1.Text); if (Price == -1) { label5.Text = "Invalid symbol"; this.Activate(); } else { label5.Text = "Current price: " + Price.ToString(); if (((Price - OriginalPrice) > DollarChange) || ((OriginalPrice - Price) > DollarChange)) { this.Activate(); this.BringToFront(); } } } catch(Exception ex) { label5.Text = "Web service exception" + ex.Message; } }
The program uses a timer set to 15-second intervals. Each time the timer occurs, the code uses the web service to retrieve the stock’s current price. If the stock price has increased or decreased by a dollar or more since the user first requested the price, the code will bring the form to the top of any open applications:
if (((Price - OriginalPrice) > DollarChange) || ((OriginalPrice - Price) > DollarChange)) { this.Activate(); this.BringToFront(); }
|
|