HTTP, TCP/IP in Windows
A web request is a communicative message that is transmitted between the client (web browsers), to the servers. This request is essential in providing the user with the correct and preferred web pages that the server will then display on the user’s interface. The server then retrieves the content requested by the client that will result in the web pages, images, or interactive features displayed. The underlying protocol is used here HTTP. HTTP is a request-response protocol that allows users to communicate data on the World Wide Web. HTTP clients generally use Transmission Control Protocol (TCP) connection to communicate with servers.
Following are the major operation that we can do over HTTP.
GET = Requests a specific source in its entirety.
In an HTTP GET request, parameters are sent as a query string:
HEAD = A specific resource with nobody content.
POST = Adds articles, messages, and information to another page under an existing web resource.
The content is put after the HTTP headers. The format of an HTTP POST is to have the HTTP headers, followed by a blank line, followed by the request body. The POST variables are stored as key-value pairs in the body.
PUT = Directly modifies a current web source and creates a new URL if need be.
DELETE = Eliminates a specified source.
TCP is a connection-oriented protocol which states a connection is established and maintained until the application data at each end have finished exchange. TCP breaks application data into packets. This packet delivers to the transport layer. Layer 4 manages flow control and provide error free data transmission and handles retransmission of dropped or garbled packets and acknowledges all packets that arrive. In the OSI model, TCP covers parts of Layer 4 the transport layer and Layer 5 covers the session layer. TCP’s job is to ensure that all data sent in a stream moves from client to server in a correct order and is intact. TCP uses a technique known as positive acknowledgement with retransmission, requiring the receiving end of a transmission to give a response as to what data has been received. The bytes sent can exactly match the bytes received. No data is altered or lost along the way.
//Get the whole content of a web page (with password or without password)
// Create a request for the URL.
WebRequest request = WebRequest.Create("https://www.hemlix.com/");
// If required by the server, set the credentials.
NetworkCredential mycredential = new NetworkCredential();
mycredential.UserName = "username";
mycredential.Password = "password";
request.Credentials = mycredential; // CredentialCache.DefaultCredentials; //If password is not used
WebResponse response = request.GetResponse();
using (Stream dataStream = response.GetResponseStream())
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
//Access with query string
WebRequest req2 = WebRequest.Create("https://www.hemelix.com/getinfo?userid=12345&address=Bombay,India");
req2.Credentials = mycredential; //Get from the previous example
WebResponse response2 = req2.GetResponse();
using (Stream dataStream2 = response2.GetResponseStream())
StreamReader reader2 = new StreamReader(dataStream2);
string responseFromServer2 = reader2.ReadToEnd();
//POSTing data with HTTP
var myData = new
address = "Bombay,India",
info = "A test stuff",
agegroup = "junior"
string jsonData = JsonConvert.SerializeObject(myData);
string webAddrpost = "https://www.hemelix.com/infopost";
var httpWebRequestpost = WebRequest.CreateHttp(webAddrpost);
httpWebRequestpost.ContentType = "application/json; charset=utf-8";
httpWebRequestpost.Method = "POST";
httpWebRequestpost.Credentials = mycredential;
using (var streamWriter = new StreamWriter(httpWebRequestpost.GetRequestStream()))
var httpResponse = (HttpWebResponse)httpWebRequestpost.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
var responseText = streamReader.ReadToEnd();