Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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.
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.
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.
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.
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.