Sie sind auf Seite 1von 26

URL Connection Ministry of Higher Education

Sur University of Applied Science


Project Assignment
Course Code: ITNW 4115

and
Course: Network and Security Programming

HttpURL
Connection
By: Maryam Ali Alharbi
ID: 2016493051
Dr. Mohammed Arif
2

CONTENT:
» URL Connection and
HttpURL Connection
» Query string
» The URI components
» Methods of URL
» Classes of URLConnection
» Programming Examples
3

Definition of URL:
» URL short for Uniform
Resource Locator. It is used to
identify address locations on the
World Wide Web (WWW). It
also used for database access
(JDBC), email (mailto), file
4

Definition of Http:
It acronym for HyperText Transfer Protocol. It is a
standard protocol. It used to communicate web
browsers with web servers. This protocol define
many process such as, response cods, how a
browser sends additional data along with the
request (e.g. the data formats it can accept), how a
browser requests a file from a web server and
how the server sends data back to the client.
5

HttpURL Connection:
 HttpURLConnection is an abstract subclass
of URLConnection that provides some
additional methods specific to the HTTP
protocol. URL connection objects that are
returned by an http URL will be instances of
java.net.HttpURLConnection.
6
Locator  (URL) that assigns
Query string
values to specified
parameters. A query string
commonly includes fields
added to a base URL by a web
browser  or other client
application, for example as
part of an HTML form.  
7

• Alphanumeric ASCII characters (a-


z, A-Z, and 0-9) and the $-_.!*'(),
punctuation symbols are left
unchanged.

• The space character is converted


into a plus sign (+).

• Other characters (e.g. &, =, ^, #,


8

For example:
The comma is ASCII character 44 (decimal) or 2C
(hex). Therefore if the comma appears as part of a
URL it is encoded as %2C.
The query string "Author=Sadie, Julie&Title=Women
Composers" is encoded as:
Author=Sadie
%2C+Julie&Title=Women+Composers
9

The URI
components:
10

 A scheme:
It specifies the protocol that will be used to
access the resource on the Internet. It can be
HTTPS (with SSL) or HTTP (without SSL). For
example, http://google.com.
11

Path:
Specifies the resource path
identified in the host that the web
client wants to access. It detached
by a slash (/).
12

» Query String:
It provides a sequence of information that the
resource can use for some purpose. If a query
string is used, it follows the path component. It
preceded by a question mark (?). For example,
http://metalab.unc.edu/nywc/comps.phtml?category=Choral+Wor
13

» Authority:
That content user information (user
name, password and etc.), hostname,
and address port. For example,
http://elharo@java.oreilly.com/.
14

» The ref(reference):):
a.k.a section or anchor. It come after hash (#).
For example, file:///Macintosh
%20HD/Java/Docs/JDK
%201.1.1%20docs/api/java.net.InetAddress.html
#_top_
15

Methods
of URL:
16
Method Description

public String getProtocol() it returns the protocol of the URL.

YOU CAN ALSO SPLIT


public String getHost() it returnsYOUR
the host name of the URL.

CONTENT
public String getPort() it returns the Port Number of the URL.

public String getFile() it returns the file name of the URL.

public String getAuthority() Blackit returns the authority of the URL.


public String toString() Is the color
Methodsit of
of ebony and of
returns
URL: the string representation of the
outerURL.space. It has been the
public String getQuery() symbolic colorthe
it returns of elegance,
query string of the URL.

public String getDefaultPort()


solemnity and authority.
it returns the default port of the URL.

public URLConnection openConnection() it returns the instance of URLConnection


i.e. associated with this URL.
public Object getContent() it returns the content of the URL.

public String getRef() it returns the anchor or reference of the


URL.
17

Classes of
URLConnection:
18

» The URLConnection class can be used to write the data


and read data referred by the URL. This class represents
a communication link between the application and the
URL.

» The URL class contains methods to:


⋄ Get content from the server as a Java object.
⋄ Get an input stream from a URL so you can read data from a
server
⋄ Create new URLs.
⋄ Parse the different parts of a URL.
URL Encoding class:
19
The java.net.URLEncoder class
contains a single static method
which encodes strings in x-
www-form-url-encoded
format: URLEncoder.encode
The URLDecoder class: (String s).
In Java 1.2 the  
java.net.URLDecoder class
contains a single static
method which decodes
strings in x-www-form-url-
encoded format:
URLEncoder.decode (String s)
20

Programming
Examples:
21

Example 1
Write a Java program which uses
a methods of URL(getFile(),
getHost(), getPort(),
getProtocol(), getRef(),
getQuery(), getPath(),
getUserInfo(), and
getAuthority()) to split URLs
22

package urlmethods;
import java.io.*;
import java.net.*;
public class URLMethods {
public static void main(String[] args) {
try {
URL u= new URL("http://www.c-sharpcorner.com/authors/fd0172/sandeep-sharma.aspx");
System.out.println("the File is " + u.getFile());
System.out.println("the host name is " + u.getHost());
System.out.println("the Port Number is " + u.getPort());
System.out.println("the protocol is "+ u.getProtocol());
System.out.println("the refrence is " + u.getRef());
System.out.println("the query string is " + u.getQuery());
System.out.println("the Path is " + u.getPath());
System.out.println("the User Information are " + u.getUserInfo());
System.out.println("the authority is " + u.getAuthority());
}
catch(Exception e){
System.out.println(e);
}
}
23

Example 2

Write a program to encode the


query string "
https://example.com/over/there?
name=ferret “
and decode the URL
"http://allmarkedup.com%2cfolder
24

package urlencode&decode;
import java.net.URLDecoder;
import java.net.URLEncoder;
public class URLEncoseDecode {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String gstring= "https://example.com/over/there?name=ferret";

String encodedString = URLEncoder.encode(gstring);


System.out.println("Encoded quesry string:" + encodedString);
String decodedString = "http://allmarkedup.com%2cfolder%2dir+%2chome.html?
item=value";
System.out.println("Original String:" + URLDecoder.decode(decodedString));
}
}
25

Any
Question?
Place your screenshot here
26

Free templates for all your presentation needs

For PowerPoint and 100% free for personal or Ready to use, professional Blow your audience away
Google Slides commercial use and customizable with attractive visuals

Das könnte Ihnen auch gefallen