introduction to vb.net internet tools. downloading internet resources download the html from a web...

50
Introduction to VB.Net Internet Tools

Post on 22-Dec-2015

225 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 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

Introduction to VB.Net Internet Tools

Page 2: 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

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.WebRequest• Makes a request to a Uniform Resource Identifier.

– WebResponse• Provides a response from a Uniform Resource

Identifier.

• Demo: MyBrowser/GetWebPage

Page 3: 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

Imports System.IO

Imports System.Net

Imports System.TextPrivate Sub BtnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGo.Click

Dim URI As New Uri(txtURL.Text)

Dim request As WebRequest = WebRequest.Create(URI)

Dim response As WebResponse = request.GetResponse

Dim stream As Stream = response.GetResponseStream

Dim readStream As New StreamReader(stream)

Dim webData As String = readStream.ReadToEnd

stream.Close()

readStream.Close()

txtData.Text = webData

End sub

Page 4: 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

Change Downloaded Page

• For example, use string’s Replace method to change page content.– webData = webData.Replace("Chao", "You")

Page 5: 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

Using the WebClient Class

• This class wraps the Request and Response classes.

• Methods:– DownloadData: Returns a byte array from an

Internet address.– DownloadFile: Save a downloaded file.– OpenRead: Returns a stream from an Internet

address.

Page 6: 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

UTFEncoding Class

• This class encodes Unicode characters using UCS Transformation Format, 8-bit form (UTF-8). This encoding supports all Unicode character

• Method: GetString– Decodes the specified byte array into a string.

Page 7: 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

WebClient/Download

Dim wc As New WebClient()

Dim utf8 As New UTF8Encoding()

Dim webData As String

webData = utf8.GetString(wc.DownloadData(txtURL.Text))

txtData.Text = webData

Page 8: 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

WebClient/DownloadFile

Dim wc As New WebClient()

wc.DownloadFile(txtURL.Text, "c:\testDownLoad.txt")

Page 9: 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

WebClient/OpenRead

Dim wc As New WebClient()

Dim stream As Stream

stream = wc.OpenRead(txtURL.Text)

Dim readStream As New StreamReader(stream)

Dim webData As String = readStream.ReadToEnd

txtData.Text = webData

Page 10: 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

Hosting Internet Explorer in Windows Forms

• Internet Explorer COM control– Right click Tool Box Windows Form tab and

select Customize ToolBox– Select COM Component tab, and Scroll down

to select Microsoft Web Browser.

Page 11: 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

Internet Explorer COM control

• Events– DownLoadComplete– DownLoadBegin

• Methods:– Navigate

• Dim HomeURL As String = "http://dchaolaptop"• Private Sub MyBrowser_Load(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles MyBase.Load

• IE.Navigate(HomeURL)• End Sub

– Goback, GoForward– Stop– Refresh2– GoHome, GoSearch

Page 12: 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

Introduction to Web Form

Page 13: 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

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.

Page 14: 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

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 DataGrid control.

• Rich Controls: Such as Calendar, AdRotator.• Validation Controls: Such as

RequiredFieldValidator.• Namespace:System.Web.UI.Webcontrols

Page 15: 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

Web Server

• Default directory– C:\InetPub\wwwroot– Computer lab: Zip drive

• dchao

• Default home page– Default.aspx, default.asp, default.htm

Page 16: 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

Web Project

• File/New/ ASP.Net Application

• Website folder

• Web form:– Webform.aspx

• Design view and HTML view

– WebForm.Aspx.VB• CodeBehind

Page 17: 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

Web Data Form

• Web Data Form Wizard:– Project/Add Web Form/Data Form Wizard

• Web Form with Server Explorer or Data Tools.– Data binding

• OleDbDataAdapter1.Fill(Ds21)

• DataGrid1.DataBind()

Page 18: 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

Use Data Reader to Create a DropDown List

• Demo: WebForm/WebForm2– Update rating for selected customer

Page 19: 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

Introduction to ASP.Net

Page 20: 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

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.

Page 21: 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

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.

Page 22: 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

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– CodeBehind

• Data entered into form fields is preserved in View State.

Page 23: 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

Elements of an ASP.Net Page

• Directives

• Code blocks

• ASP.NET controls

• HTML tags and text.

• Server-side include directives

Page 24: 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

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” %>

Page 25: 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

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.TimeOfDay()%></p>– <p>The time is now <% response.write(Now.TimeOfDay())%></p>

• “=“ is shorthand for response.write

• Server-side comments:– <%--

• Comments

– --%>

• CodeBehind

Page 26: 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

Demo: TineNow.aspx<p>The time is now <%=Now.TimeOfDay()%></p>

<p>The time is now <% response.write(Now.TimeOfDay())%></p>

<%

dim iHour

iHour=Now.Hour()

if iHour < 12 then

response.write("good morning")

else

response.write ("<h1>good afternoon</h1><br>")

end if

%>

Page 27: 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

ASP.NET Object Model

Client Server

Request Object

Response Object

Server Object

SessionObject

Application

Object

Page 28: 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

ASP.NET Request Object• When a page is requested, much

information is passed along with the request, such as the URL, queryString, 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.HttpRequest class.

Page 29: 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

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 QueryString.

• Post: A prefered method for database processing. Form’s data is sent separately from the URL.

– Name: Form’s name

Page 30: 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

QueryString

• A QueryString 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?CustID=C1&Cname=Chao”>

Page 31: 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

Request Object Collections

• QueryString– http://my.com/Target.htm?CustID=C1&CustName=Chao

– cid = Request.queryString(“CustID”)

– cName=Request.queryString(“CustName”)

• Form– A form with two text boxes:CustID, CustName– cid = Request.Form(“CustID”)

– cName=Request.Form(“CustName”)

• Cookies

Page 32: 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

ASP.NET Response Object

• This object allows you to send information back to client.

• It is created from the System.Web.HttpResponse class.

• Properties:– Cookies (a collection)

• Methods:– Response.Write – Response.Redirect (“URL”)

• Demo: testReqForm.htm, testReqForm.aspx

Page 33: 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

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.ISPostBack property.– IF Not Page.ISPostBack Then

• This is the first time the page is loaded.

• AutoPostBack property– Button – true– DropDown list, textbox, radiobutton, checkbox: False

Page 34: 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

FrontPage/VS.Net Demo

• wwwRoot/Webform– Webform1: Copy from VS.Net toFrontPage

• Handles

• OnClick

– Webform5:• AutoPostBack

• Page.IsPostBack

• Use a Button’s onClick event instead of Listbox’s selectedIndexChanged event.

• Copy to FrontPage.

Page 35: 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

VB/WebForm/FrontPage

• Demo:– VB: MyBrowser/GetWebPage– WebForm: RichControl– FrontPage:ASPNet/GetPageChange.aspx

Page 36: 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

Bind the DataReader to a Server Control

• DataGrid:– dim objDataReader as oledbDataReader– objDataReader=objComm.executeReader()– dgCustomer.datasource=objDataReader– dgCustomer.DataBind()

• The Datagrid is defined as:– <asp:datagrid id="dgCustomer" runat="server"

/><br />

Demo: CommandReadCust.ASPX

Page 37: 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

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.

Page 38: 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

Creating a Web Service Using a Text Editor

• WebService directive:– <%@ WebService Class="CustomerInfo" %>

• Import namespaces:– imports System.Web.Services– imports System

• Define the web service class.• Add WebMethod attribute to the function

declaration.• Note: Web service file extension is ASMX

Page 39: 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

A Web Service ExampleASPET/TestWebService.ASMX

<%@ WebService Class="CustomerInfo" %>

imports System.Web.Services

imports System

imports System.Data

imports System.Data.Oledb

Public Class CustomerInfo

<webMethod()> public Function GetCname(ByVal CID as String) as String

dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb"

dim objConn as new OledbConnection(strConn)

dim strSQL as string = "select * from customer where CID = '" & CID & "';"

dim objComm as new OledbCommand(strSQL,objConn)

dim Results as string

objConn.open()

dim objDataReader as oledbDataReader

objDataReader=objComm.executeReader()

objDataReader.read()

return objDataReader("Cname")

end function

Page 40: 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

Creating a Web Service Using VS

• New Project/ASP.Net Web Service

Page 41: 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

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.

Page 42: 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

HTTP

• HTTP Request:– Request line: Method – Get, Post– Header– Body

• HTTP Response:– Respons line: HTTP version and status code– Header– Body

Page 43: 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

Invoking a Web Service with HTTP

• Http – Get:– http://dchaolaptop/aspnet/testwebservice.asmx/GetRating?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

Page 44: 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

Simple Object Access ProtocolSOAP

• 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.

Page 45: 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

Consuming Web Services from a Windows Application

• Add a web reference to the web service.

• Declare a web service class variable.– Dim myWebService As New dchaolaptop.CustomerInfo()

• Demo: UseWebService

Page 46: 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

Universal Description, Discovery, and Integration (UDDI)

• A directory service for web services.– http://uddi.org

Page 47: 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

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.

Page 48: 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

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 CustomerInfo.vb file for the testwebservice.asmx web service.

wsdl.exe /l:vb http://dchaolaptop/aspnet/testwebservice.asmx?wsdl

Page 49: 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

Compile the Source Code File Into a DLL File

• The command below create a CustomerInfo.dll Vbc /t:library /r:system.dll,system.web.services.dll,system.xml.dll CustomerInfo.vb

Page 50: 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

<script runat="server">

sub CallService(s as object, e as eventArgs)

dim myProxy as new CustomerInfo

lblCname.text=myProxy.GetCname(txtCID.text)

lblRating.text=myProxy.GetRating(txtCID.text)

end sub

</script>

<html>

</head>

<body>

<form runat="server">

Enter CID: <asp:textbox id="txtCID" runat="server" />

<asp:label id="lblCname" runat="server"/>

<asp:label id="lblRating" runat="server"/>

<asp:button text="GetService" onClick="CallService" runat="server" />

</form>

</body>

</html>

Demo: testProxy.aspx