Basically, an HTTP client initiates a request through web browser. It establishes a Transmission Control Protocol (TCP) connection to a particular port on a host server (port 80 by default). An HTTP server that port waits for the client's request message. After receiving the request, the server sends back a status line, such as "HTTP/1.1 200 OK", and a message of its own, the body of which is may be the requested resource, an error message, or some other information.
Resource accessing by the HTTP are identified using Uniform Resource Identifier (URI)or, more specifically, Uniform Resource Locator (URL) by the http: or https URI schemas.
1.Request line (such as GET)
2.Header ( such as Accept-Language: en)
3.Empty Line
4.Optional message body
HTTP REQUEST METHODS
Hyper Text Transfer Protocol (HTTP) defines eight methods for the next action for existing resource.I am giving you a short description on that eight methods.
HEAD
It is almost like a get request without the response body. This is useful for retrieving meta-information written in response headers, without sending the entire content.
GET
Get requests a representation of the specified resource.
POST
Post submits data to be processed from your form to the requesting resources. The data is included in the body of the request. It may create a new resource or update the existing resources or both.
PUT
Put uploads a representation of the specified resource.
DELETE
It deletes the specified resource.
TRACE
It returns back to the received request, so that a client can see what intermediate servers are adding or changing in existing the request.
OPTIONS
It returns the HTTP methods that the server supports for specified URL. This can be used to check the functionality of a web server by requesting '*' instead of a specific resource.
CONNECT
Converts the request connection to a transparent TCP/IP tunnel, usually to facilitate SSL-encrypted communication (HTTPS) through an unencrypted HTTP proxy.
CREATING REQUEST AND GETTING RESPONSES
// Create a request for the URL.
WebRequest request = WebRequest.Create("http://www.google.com");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status.
if (response.StatusDescription == "OK")
{
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
}
CREATING REQUEST AND GETTING RESPONSES WITH AUTHENTICATION
strId = UserId_TextBox.Text;
string strName = Name_TextBox.Text;
ASCIIEncoding encoding=new ASCIIEncoding();
string postData="userid="+strId;
postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
stOut.Write(strNewValue);
stOut.Close();
//send the request, read the response
HttpWebResponse strResponse = (HttpWebResponse)req.GetResponse();
Stream IPNResponseStream = strResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(IPNResponseStream, encode);
char[] read = new char[257];
// Reads 256 characters at a time.
int count = readStream.Read(read, 0, 256);
readStream.Close();
strResponse.Close();

2 comments:
nice article
yes good post..it is really helpfull
Post a Comment