Sei sulla pagina 1di 20

Communication Management Using HTTP Commands

HTTP is used by two computers to communicate with each other over a socket connection.
HTTP is considered a request/response protocol, meaning that a client is the requestor of information contained on a server and the server responds to the clients request. An HTTP request, called a request entity, It consists of three components. request method request header request body

Request method:
It describes the way in which information is to be sent to the client by the server. There are three request methods. These are GET

POST HEAD.

Request GET POST HEAD

Description Send requested data as part of the URL Send requested data in a separate stream Send only meta-information about the specified resource

There are 2 ways in which data is sent..GET & POST. GET The GET method requires a URL and data separated by a question mark, which is also known as URL encoding. Data is grouped into one or more fields, each of which has a value associated with the field. A value is associated by using an assignment operator (=), and each field value pair is separated by an ampersand (&).

Example:

http://www.myclient.com/inventory?prod123=100&pro d456=150 Client requests for inventory status of product 123 and product 456.

The POST method has Advantages over the GET method.


1. Data sent using the GET method is visible whenever the URL is visible. The POST method causes the data not to be visible as part of the URL. 2. POST method is to be able to send an unlimited amount of data in response to a request. The size of data sent using the GET method is limited by the maximum size of data that can be held by the environment variable.

The HEAD method requests metadata about the resource.


Metadata is information about data. The MIDlet uses the HEAD method and follows up with a GET or POST method if the inventory has changed. Metadata is returned using the same technique as the GET request method.

HttpConnection
HttpConnection is used to send an HTTP request method. Ex: HttpConnection connection = (HttpConnection)Connector.open("http://www.myserver.com/in ventory.dat");

Once an HttpConnection is opened, you can use HttpConnection client request methods to interact with the remote server. There are two ways to interact with the remote server by setting or retrieving the request method --- by setting or retrieving the HTTP header information.

i) request method ----is set by calling the setRequestMethod() Parameter: string containing GET, POST, or HEAD The getRequestMethod() is called to retrieve a string that contains the current request method. setRequestProperty() method is used to write a value to a field in the HTTP header. 2 parameters - is a string containing the field name, called a key, which is being set. - is a string containing the new value of the specified header field.

ii) A request header------ is information that describes the request using one or a combination of 40 header fields. Many header fields are used only rarely. header fields: Accept Cache-Control Content-Type Expires IF-Modified-Since User-Agent
iii) Request body: The body of an HTTP request contains any information that is sent to the server as part of the request. This information is transferred to the server using either the GET or POST request method.

Example: HttpConnection connection = (HttpConnection) Connector.open("http://www.myserver.com/inventory.dat"); connection.setRequestMethod(HttpConnection.GET); connection.setRequestProperty("Content-Type","//text/plain");

A response entity consists of 3 components: The status line header response body of the response. i) The status line: 2 parts status code status message

Status code: A three-digit number represents a status code. The first digit represents the category of the status code remaining digits represent a specific status within a category.

Status Code 100199 200299 300399 400499 500599

Category Description Information Success Redirection Client Error Server Error

Response Status Code Categories

retrieve the status code by calling the getResponseCode(). returns an int whose value is the status code. status message is retrieved by calling getResponseMessage(). returns a string containing the status message.
Example: HttpConnection connection = (HttpConnection) Connector.open("http://www.myserver.com/inventory.dat"); System.out.println(connection.getResponseCode()); System.out.println(connection.getResponseMessage());

ii) The response header contains header fields and related values similar to the request header. Methods to retrieve the value of a header field are two versions of the getHeaderField() method. 1st version retrieves the value by referencing the index of the header field, which is passed to the getHeaderField() as an int. Each header field is assigned an index beginning with zero. getHeaderField(0) retrieves the value of the first header field of the response header. 2nd version retrieves the value of a header field by referencing the name of the header field. which is passed as a string to the getHeaderField() method as a parameter.

getHeaderFieldInt() to retrieve the name of a header field, by passing it a string that contains the name of the header field getHeaderFieldKey() -- retrieve the name of a header field by passing it an int that represents the index of the header field.

iii) Response body--- which contains the information that the client requested from the server.

Sending Data Along with an HTTP Request


HTTP request to a server is accompanied by information needed by the server to process the request. when a server must authenticate a client using the clients user ID and password. The client sends the user ID and password along with the HTTP request. Data sent to a server must be in a pair value set. where the first element in the pair value is the field name and the second element is the value associated with the field.

2 techniques is used to send data to the server depending on whether the request method use GET or POST for transmission. The GET request method requires that data be concatenated to the URL of the server. The POST request method requires that each pair value be written to the output stream.

how to send data using the GET request


The URL includes the name of the program that executes on the server to process the data associated with the request. A Java Servlet called my.LoginServlet processes this request by reading both pair value sets sent to the server. The pair value sets are sent as a query string that is separated from the URL by a question mark. Each pair value set is separated from other pair value sets by an ampersand (&). Example: HttpConnection http = (HttpConnection) Connector.open("http://www.myserver.com/my.LoginServlet?UserI D=jim&password=keogh;

how to send data using the POST request method


byte data[] = ("userID=jim"); outputstream.write(data); data = ("&password=Keogh"); outputstream.write(data); outputstream.flush(); each pair value is assigned to a byte array. The byte array is then written to the output stream by calling the write() method. The flush() method is called after the last pair value set is written.

Potrebbero piacerti anche