Working with URLs CSIE, National University of Tainan.

Slides:



Advertisements
Similar presentations
Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Advertisements

Lecture 10: Networking using Socket Programming. Client-Server Model b The term server applies to any program that offers a service that can be reached.
Prepared By E. Musa Alyaman1 URLs, InetAddresses, and URLConnections Chapter 9.
1 Creating a network app Write programs that  run on different end systems and  communicate over a network.  e.g., Web: Web server software communicates.
Moshe Fresko Bar-Ilan University
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
Programming TCP Clients. InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in.
1 Java Networking CS , Spring 2008/9. 2 Today’s Menu Networking Basics  TCP, UDP, Ports, DNS, Client-Server Model TCP/IP in Java Sockets URL 
1 Networking with Java 2: The Server Side. 2 Some Terms Mentioned Last Week TCP -Relatively slow but enables reliable byte-stream transmission UDP -Fast.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Java sockets. From waiting.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
Networking in Java Representation and Management of Data on the Internet.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
28-Jun-15 Basic Protocols. 2 Sockets Sockets, or ports, are a very low level software construct that allows computers to talk to one another When you.
Java Review 2. The Agenda The following topics were highlighted to me as issues: –File IO (Rem) –Wrappers (Rem) –Interfaces (Rem) –Asymptotic Notation.
1 Java Networking – part I CS , Winter 2007/8.
15-Jul-15 Basic Protocols. 2 Sockets Sockets, or ports, are a very low level software construct that allows computers to talk to one another When you.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
CSE 341, S. Tanimoto Java networking- 1 Java Networking Motivation Network Layers Using Sockets A Tiny Server Applets URLs Downloading Images, MediaTracker.
Building a Web Browser CS1316: Representing Structure and Behavior.
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
1 Java Networking CS , Spring Today’s Menu Networking Basics  TCP, UDP, Ports, DNS, Client-Server Model TCP/IP in Java Sockets URL  The.
Networking Basics Computers running on the Internet communicate to each other using either the Transmission Control Protocol (TCP) or the User Datagram.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
Ryerson University CPS Distributing Computing with Java.
Appendix F: Network Programming in Java ©SoftMoore ConsultingSlide 1.
Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
CS4273: Distributed System Technologies and Programming I Lecture 7: Java Networking.
Import java.net.*; import java.io.*; public class LowPortScanner { public static void main(String[] args) { String host = "localhost"; if (args.length.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
DBI Representation and Management of Data on the Internet.
1 Streams Files are a computer’s long term memory Need ability for programs to –get information from files –copy information from program variables to.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
Servlet Communication Other Servlets, HTML pages, objects shared among servlets on same server Servlets on another server with HTTP request of the other.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
CS 11 java track: lecture 6 This week: networking basics Sockets Vectors parsing strings.
Network Programming With Java Hilal Arslan Hale Müge Kıncak.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
NETWORK PROGRAMMING.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
StreamTokenizer Break up a stream of text into pieces called tokens A token is the smallest unit recognized by a text parsing algorithm (words, symbols,
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
VII. Sockets. 1. What is a Socket? A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
© Marty Hall, Larry Brown Web core programming 1 Using Applets as Front Ends to Server-Side Programs.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
Java Programming II Java Network (I) Java Programming II.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
URL Programming Mimi Opkins CECS277. What is a URL?  URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Using Applets as Front Ends to Server-Side Programs.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Network Programming James Atlas July 10, James Atlas - CISC3702 Review Multi-Threaded Programming Multi-Threaded Programming  Processes  Threads.
Secure Sockets SSL (Secure Sockets Layer) is a standard security technology for establishing an encrypted link between a server and a client—typically.
CSE 341, S. Tanimoto Java networking-
Strings and File I/O.
Networking with Java 2.
„Networking”.
Basic Protocols 24-Nov-18.
URL in Java C343 Lab (Week 13).
Reading and Writing Text Files
Basic Protocols 19-Feb-19.
Uniform Resource Locator: URL
Presentation transcript:

Working with URLs CSIE, National University of Tainan

Creating a URL URL Constructors URL myURL = new URL(" URL page1URL = new URL(myURL, "page1.html"); new URL("http", "example.com", "/pages/page1.html"); try { URL myURL = new URL(...); } catch (MalformedURLException e) { // exception handler code here //... }

Parsing a URL import java.net.*; import java.io.*; public class ParseURL { public static void main(String[] args) throws Exception { URL aURL = new URL(" + "/index.html?name=networking#DOWNLOADING"); System.out.println("protocol = " + aURL.getProtocol()); System.out.println("authority = " + aURL.getAuthority()); System.out.println("host = " + aURL.getHost()); System.out.println("port = " + aURL.getPort()); System.out.println("path = " + aURL.getPath()); System.out.println("query = " + aURL.getQuery()); System.out.println("filename = " + aURL.getFile()); System.out.println("ref = " + aURL.getRef()); }

Parsing a URL Here is the output displayed by the program: protocol = http authority = example.com:80 host = example.com port = 80 path = /docs/books/tutorial/index.html query = name=networking filename = /docs/books/tutorial/index.html?name=networking ref = DOWNLOADING

Reading Directly from a URL import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL oracle = new URL(" BufferedReader in = new BufferedReader( new InputStreamReader(oracle.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); }

Reading from a URLConnection import java.net.*; import java.io.*; public class URLConnectionReader { public static void main(String[] args) throws Exception { URL oracle = new URL(" URLConnection yc = oracle.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); }

Writing to a URLConnection Many HTML pages contain forms — text fields and other GUI objects that let you enter data to send to the server. After you type in the required information and initiate the query by clicking a button, your Web browser writes the data to the URL over the network. At the other end the server receives the data, processes it, and then sends you a response, usually in the form of a new HTML page. For a Java program to interact with a server-side process it simply must be able to write to a URL, thus providing data to the server. It can do this by following these steps: 1.Create a URL. 2.Retrieve the URLConnection object. 3.Set output capability on the URLConnection. 4.Open a connection to the resource. 5.Get an output stream from the connection. 6.Write to the output stream. 7.Close the output stream.

Writing to a URLConnection import java.io.*; import java.net.*; public class Reverse { public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: java Reverse “ + " “ + " string_to_reverse"); System.exit(1); } String stringToReverse = URLEncoder.encode(args[1], "UTF-8"); URL url = new URL(args[0]); URLConnection connection = url.openConnection(); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write("string=" + stringToReverse); out.close(); BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream())); String decodedString; while ((decodedString = in.readLine()) != null) { System.out.println(decodedString); } in.close(); }

Example public class url{ public static void main(String[] args) { url Myurl= new url(); Myurl.search("internet"); } public void search(String str){ try { URL url = new URL(" +URLEncoder.encode(str, "utf-8")); // 網址 BufferedReader buf = new BufferedReader (new InputStreamReader(url.openStream(), "utf-8")); FileWriter fr = new FileWriter("response.txt"); // 原始碼存在 response.txt String line; while ((line = buf.readLine()) != null) { fr.write(line+”\r\n”); } fr.close(); buf.close(); } catch (Exception e) {}; }