Introduction to VB Net Internet Tools Downloading Internet
Introduction to VB. Net Internet Tools
Downloading Internet Resources • Download the HTML from a web page and display it in a text box. – System. URI • A class for expressing a Uniform Resource Identifier. – System. Net. Web. Request • Makes a request to a Uniform Resource Identifier. – Web. Response • Provides a response from a Uniform Resource Identifier. • Demo: My. Browser/Get. Web. Page
Imports System. IO Imports System. Net Imports System. Text Private Sub Btn. Go_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles Btn. Go. Click Dim URI As New Uri(txt. URL. Text) Dim request As Web. Request = Web. Request. Create(URI) Dim response As Web. Response = request. Get. Response Dim stream As Stream = response. Get. Response. Stream Dim read. Stream As New Stream. Reader(stream) Dim web. Data As String = read. Stream. Read. To. End stream. Close() read. Stream. Close() txt. Data. Text = web. Data End sub
Change Downloaded Page • For example, use string’s Replace method to change page content. – web. Data = web. Data. Replace("Chao", "You")
Using the Web. Client Class • This class wraps the Request and Response classes. • Methods: – Download. Data: Returns a byte array from an Internet address. – Download. File: Save a downloaded file. – Open. Read: Returns a stream from an Internet address.
UTFEncoding Class • This class encodes Unicode characters using UCS Transformation Format, 8 -bit form (UTF-8). This encoding supports all Unicode character • Method: Get. String – Decodes the specified byte array into a string.
Web. Client/Download Dim wc As New Web. Client() Dim utf 8 As New UTF 8 Encoding() Dim web. Data As String web. Data = utf 8. Get. String(wc. Download. Data(txt. URL. Text)) txt. Data. Text = web. Data
Web. Client/Download. File Dim wc As New Web. Client() wc. Download. File(txt. URL. Text, "c: test. Down. Load. txt")
Web. Client/Open. Read Dim wc As New Web. Client() Dim stream As Stream stream = wc. Open. Read(txt. URL. Text) Dim read. Stream As New Stream. Reader(stream) Dim web. Data As String = read. Stream. Read. To. End txt. Data. Text = web. Data
Hosting Internet Explorer in Windows Forms • Internet Explorer COM control – Right click Tool Box Windows Form tab and select Customize Tool. Box – Select COM Component tab, and Scroll down to select Microsoft Web Browser.
Internet Explorer COM control • Events – Down. Load. Complete – Down. Load. Begin • Methods: – Navigate • Dim Home. URL As String = "http: //dchaolaptop" • Private Sub My. Browser_Load(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles My. Base. Load • • – – IE. Navigate(Home. URL) End Sub Goback, Go. Forward Stop Refresh 2 Go. Home, Go. Search
Introduction to Web Form
Web Form vs HTML Form • HTML Form: A web page that contains one or more HTML form controls such as textbox, checkbox, dropdown list, and button inside an HTML <form> tag. • Web Form: A web page that contains: – ASP. NET server controls, and/or HTML form controls inside a <form runat=“server”> tag. – ASP. NET code that produces dynamic content to be displayed within the web form.
ASP. NET Server Controls, ASP: • Intrinsic Controls: These controls correspond to their HTML counterparts. – Ex. Textbox, listbox, button, etc. • Data-Centric Controls: Controls used for binding and displaying data from a data source, such as the Data. Grid control. • Rich Controls: Such as Calendar, Ad. Rotator. • Validation Controls: Such as Required. Field. Validator. • Namespace: System. Web. UI. Webcontrols
Web Server • Default directory – C: Inet. Pubwwwroot – Computer lab: Zip drive • dchao • Default home page – Default. aspx, default. asp, default. htm
Web Project • File/New/ ASP. Net Application • Website folder • Web form: – Webform. aspx • Design view and HTML view – Web. Form. Aspx. VB • Code. Behind
Web Data Form • Web Data Form Wizard: – Project/Add Web Form/Data Form Wizard • Web Form with Server Explorer or Data Tools. – Data binding • Ole. Db. Data. Adapter 1. Fill(Ds 21) • Data. Grid 1. Data. Bind()
Use Data Reader to Create a Drop. Down List • Demo: Web. Form/Web. Form 2 – Update rating for selected customer
Introduction to ASP. Net
ASP. NET • ASP. NET is a server-side technology for creating dynamic web pages. • ASP. NET allows you to use a selection of full programming languages. The default language is VB. NET. • ASP. NET files have a. aspx extension.
ASP. NET in the. NET Framework • 1. The client requests a web page. • 2. The web server locates the page. • 3. If the page is an ASP. NET page, it is sent to the Common Language Runtime for compilation and execution. • 4. The HTML produced by the CLR is returned to the browser.
Benefits of Server-Side Technology • Browser compatibility: Every browser reads HTML. • Protection of source code. • Controls are server-side objects with properties, methods and events. • Separating code from content. – Embedded code – Code. Behind • Data entered into form fields is preserved in View State.
Elements of an ASP. Net Page • • • Directives Code blocks ASP. NET controls HTML tags and text. Server-side include directives
Directives • A directive controls how an ASP. Net page is compiled. – Page directives: Specify default language, enable tracing and debugging for a page. – <%@ Page Language=“VB” %>, <%@ Page Language=“C#” %> – <%@ Page Trace=“True” %> – – Imports name spaces To process Access database, we need to import: <%@ Import Namespace=“System. Data” %> <%@ Import Namespace=“System. Data. Oledb” %>
Inserting ASP. NET Code into Web Pages • Place ASP. NET code between <script> and </script> with a RUNAT attribute. – <script language=“VB” runat=“server”> • Your script – </script> • Inline Code Block: ASP code is placed between <% and %>. – <p>The time is now <%=Now. Time. Of. Day()%></p> – <p>The time is now <% response. write(Now. Time. Of. Day())%></p> • “=“ is shorthand for response. write • Server-side comments: – <%- • Comments – --%> • Code. Behind
Demo: Tine. Now. aspx <p>The time is now <%=Now. Time. Of. Day()%></p> <p>The time is now <% response. write(Now. Time. Of. Day())%></p> <% dim i. Hour=Now. Hour() if i. Hour < 12 then response. write("good morning") else response. write ("<h 1>good afternoon</h 1> ") end if %>
ASP. NET Object Model Client Request Object Server Object Response Object Server Application Object Session Object
ASP. NET Request Object • When a page is requested, much information is passed along with the request, such as the URL, query. String, and data from a form. The request object allows you to get the information passed along with the request. • It is created from the System. Web. Http. Request class.
FORM Tag • Form attribute: – Action: Specify the URL of a program on a server or an email address to which a form’s data will be submitted. – Method: • Get: the form’s data is appended to the URL specified by the Action attribute as a Query. String. • Post: A prefered method for database processing. Form’s data is sent separately from the URL. – Name: Form’s name
Query. String • A Query. String is a set of name=value pairs appended to a target URL. • It can be used to pass information from one webpage to another. • Example: • <A Href=“http: //my. com/Target. htm? Cust. ID=C 1&Cname=Ch ao”>
Request Object Collections • Query. String – http: //my. com/Target. htm? Cust. ID=C 1&Cust. Name=Chao – cid = Request. query. String(“Cust. ID”) – c. Name=Request. query. String(“Cust. Name”) • Form – A form with two text boxes: Cust. ID, Cust. Name – cid = Request. Form(“Cust. ID”) – c. Name=Request. Form(“Cust. Name”) • Cookies
ASP. NET Response Object • This object allows you to send information back to client. • It is created from the System. Web. Http. Response class. • Properties: – Cookies (a collection) • Methods: – Response. Write – Response. Redirect (“URL”) • Demo: test. Req. Form. htm, test. Req. Form. aspx
Postback • Postback is the process by which the browser posts information back to the server telling the server to handle the event, the server does so and sends the resulting HTML back to the browser. • Page. ISPost. Back property. – IF Not Page. ISPost. Back Then • This is the first time the page is loaded. • Auto. Post. Back property – Button – true – Drop. Down list, textbox, radiobutton, checkbox: False
Front. Page/VS. Net Demo • www. Root/Webform – Webform 1: Copy from VS. Net to. Front. Page • Handles • On. Click – Webform 5: • Auto. Post. Back • Page. Is. Post. Back • Use a Button’s on. Click event instead of Listbox’s selected. Index. Changed event. • Copy to Front. Page.
VB/Web. Form/Front. Page • Demo: – VB: My. Browser/Get. Web. Page – Web. Form: Rich. Control – Front. Page: ASPNet/Get. Page. Change. aspx
Bind the Data. Reader to a Server Control • Data. Grid: – – dim obj. Data. Reader as oledb. Data. Reader obj. Data. Reader=obj. Comm. execute. Reader() dg. Customer. datasource=obj. Data. Reader dg. Customer. Data. Bind() • The Datagrid is defined as: – <asp: datagrid id="dg. Customer" runat="server" /> Demo: Command. Read. Cust. ASPX
Web Service • XML Web Service • Web services are classes that are stored on the web which can instantiate and use in both Windows and Web applications.
Creating a Web Service Using a Text Editor • Web. Service directive: – <%@ Web. Service Class="Customer. Info" %> • Import namespaces: – imports System. Web. Services – imports System • Define the web service class. • Add Web. Method attribute to the function declaration. • Note: Web service file extension is ASMX
A Web Service Example ASPET/Test. Web. Service. ASMX <%@ Web. Service Class="Customer. Info" %> imports System. Web. Services imports System. Data. Oledb Public Class Customer. Info <web. Method()> public Function Get. Cname(By. Val CID as String) as String c: sales 2 k. mdb" dim str. Conn as string ="Provider=Microsoft. Jet. OLEDB. 4. 0; Data Source = dim obj. Conn as new Oledb. Connection(str. Conn) dim str. SQL as string = "select * from customer where CID = '" & CID & "'; " dim obj. Comm as new Oledb. Command(str. SQL, obj. Conn) dim Results as string obj. Conn. open() dim obj. Data. Reader as oledb. Data. Reader obj. Data. Reader=obj. Comm. execute. Reader() obj. Data. Reader. read() return obj. Data. Reader("Cname") end function
Creating a Web Service Using VS • New Project/ASP. Net Web Service
Web Service Description Language (WSDL) • A WSDL file is an XML document containing a complete description of the web service. It shows a web service’s name, methods, and parameter types. • Help page: After entering web service’s URL, a help page is displayed. You can click the Service Description link to see the WSDL file.
HTTP • HTTP Request: – Request line: Method – Get, Post – Header – Body • HTTP Response: – Respons line: HTTP version and status code – Header – Body
Invoking a Web Service with HTTP • Http – Get: – http: //dchaolaptop/aspnet/testwebservice. asmx/Get. Rating? cid=1 • Http – Post: – Use the Action attribute to call the function. • Http-SOAP: Use Http’s Post method to send an XML document in Https’ body section. • Demo: Help page
Simple Object Access Protocol SOAP • SOAP Request and Response message: – Envelop: An envelop element surrounds the message. It is the root element of a SOAP XML document. – Body: • Request: Contains the method call name and parameter names and values. • Response: Contains the results returned by the method.
Consuming Web Services from a Windows Application • Add a web reference to the web service. • Declare a web service class variable. – Dim my. Web. Service As New dchaolaptop. Customer. Info() • Demo: Use. Web. Service
Universal Description, Discovery, and Integration (UDDI) • A directory service for web services. – http: //uddi. org
Consuming Web Services from a Web Application • Creating a web service proxy class: – A local representation of the web service. • Steps to creating a proxy class: – Use the Wsdl. exe tool to generate the source code file for the proxy class. – Compile the source code file into a DLL file. – Copy the DLL file to the application’s Bin folder.
Use the Wsdl. Exe Tool to Generate the Source Code File • Issue the wsdl. exe command at the Visual Studio command prompt: Start/Programs/microsoft VS. Net/VS. Net Tools/ VS. Net Command prompt The following command example create a Customer. Info. vb file for the testwebservice. asmx web service. wsdl. exe /l: vb http: //dchaolaptop/aspnet/testwebservice. asmx? wsdl
Compile the Source Code File Into a DLL File • The command below create a Customer. Info. dll Vbc /t: library /r: system. dll, system. web. services. dll, system. xml. dll Customer. Info. vb
<script runat="server"> sub Call. Service(s as object, e as event. Args) dim my. Proxy as new Customer. Info lbl. Cname. text=my. Proxy. Get. Cname(txt. CID. text) lbl. Rating. text=my. Proxy. Get. Rating(txt. CID. text) end sub </script> <html> </head> <body> <form runat="server"> Enter CID: <asp: textbox id="txt. CID" runat="server" /> <asp: label id="lbl. Cname" runat="server"/> <asp: label id="lbl. Rating" runat="server"/> <asp: button text="Get. Service" on. Click="Call. Service" runat="server" /> </form> </body> </html> Demo: test. Proxy. aspx
- Slides: 50