Beruflich Dokumente
Kultur Dokumente
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
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
CONTENT
public String getPort() it returns the Port Number of the URL.
Classes of
URLConnection:
18
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
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";
Any
Question?
Place your screenshot here
26
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