Tuesday, April 15, 2014

WEB DEVELOPMENT - HTTP(PART 2)

In the HTTP series, in last post Http Fundamentals, I have explained about the basic of HTTP, In this post i will explain about the , message exchanged between in an HTTP transaction. I will explain here about the message types, methods incorporated, Http headers, and status codes that server respond after getting the request from the client.

Message types:
consider a scenario , you are traveling somewhere and during your travel you asked someone about time. Now to get answer from that person, that person should understand the language you have asked and he/she should must have access to some of time keeping device. The HTTP works same way.our browser is client and our browser will make request to server to get some resource. 
The HTTP/1.1 specification defines a language so that everyone on web like all clients and servers can understand each other.

According to HTTP/1.1 specification , there is two types of messages
Http request: This is first type of message that is sent by the client to the server and there is some format and message type that should be included while sending to the server.

Http response: In response to the request received by server, server respond to the client and that will contain some formatted message which should understand by the client.

The HTTP standard defines what goes into these messages, so that everyone who speaks Http will understand each other and will able to exchange resources. And when resource does not exist , the HTTP response will contain an error message in some format that client will understand. 

To Understand what it contains in Http request message and what it contains in Http response message, then install a addons for firefox called Live Http Headers.

Http Methods
Every request message have to include one of the available HTTP method and these methods tells the server what requests want to do.

Some of the common Http Methods
 In web development only two methods are important, these are : Get and Post.
A web browser issue get request to retrieve resources from server means basically to read the data. while post usually used, when we want to send some data to server. Post requests are typically generated by a form element on a web page like the form that we fill out with input elements for address and credit card information.
Post method is use to process a credit card transaction, update an account, submit an order or perform other operation that will bring changes on server side.

I will demonstrate this with an example:
Suppose while visiting a website from web browser you request signup.html, then it will request to server and will bring back HTML and will display this form. In this case you refresh this page as many times as you want it will present same thing and it will not change anything.
Now you fill that form and submit that , here http request will issue post method and once submit you refresh that again then browser will issue a warning because browser this time know that while refreshing you can submit a duplicate information and later its consequences can have alter affect, That's why browser issue warning for the user.

Being a developer we can improve with coding and we can store these at server side in session and this technique called as post/redirect/get. To know more follow this link.

When we use get method to send http request then browser put the input the query string. so most of time if you see any link in any document, that link is something incorporated with the get method, that's why when you click on that link that directly open in web browser.

A full request message consist of : 
Example: GET http://server.com/articles/741.jsp HTTP/1.1
The common request header:
If-Modified-Since is used to retrieve mostly in case of accessing JPEG times, if the image the request trying to access has not modified then the server will not send that image again, browser can retrieve that from cache of browser.

Example:
GET / HTTP/1.1
Host: server.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) Chrome/16.0.912.75 Safari/535.7
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://www.google.com/url?&q= java
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3


Here you can notice something written in bold that represents Q value which represents the relative quality value. the default Q value is 1.0.

Complete response message:
 
Example:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Sat, 14 apr 2014 04:00:08 GMT
Content-Length: 17151
<html>
   content ...
 </html>
 
Status code:
 
Details:

In case of 304 status code, client will fetched the resource from the cached locally.
403 status code tells that you have tried to gain access to something that server do not want you to have access to.
status code 500 means internal server error and that could be due to exception in application code and it could be also possible due to problem in server software itself.
Status code 503 will be issued when server is under heavy load and can not handle any additional connections and can not process any additional request.
 

No comments:

Post a Comment