Implement HTTP request in Java using socket | How to perform HTTP Request

Algorithm for Http request
Start the http request program
Import the necessary package
Get the url
By using url object open the connection- openConnection( )
And using the following function get the data
ucon.getResponseCode( )
ucon.getResponseMessage( )
ucon.getInputStream( )
ucon.getHeaderFieldKey()
Using the while loop display the data
Finally terminate the program
source code java programming HTTP Request
import java.net.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
public class ViewSource2 {
public static void main (String[] args) {
for (int j = 0; j < args.length; j++) {
try {
URL u = new URL(args[j]);
HttpURLConnection ucon = (HttpURLConnection)
u.openConnection( );
int code = ucon.getResponseCode( );
String response = ucon.getResponseMessage( );
System.out.println("HTTP/1.x " + code + " " +
response);
for (int k = 1; ; k++) {
String header = ucon.getHeaderField(k);
String key = ucon.getHeaderFieldKey(k);
if (header == null key == null) break;
System.out.println(uc.getHeaderFieldKey(k) + ": " +
header);
}
InputStream in = new
BufferedInputStream(uc.getInputStream( ));
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read( )) != -1) {
System.out.print((char) c);
}
}
catch (MalformedURLException er) {
System.err.println(args[0] + " is not a parseable
URL");
}
catch (IOException er) {
System.err.println(er);
}
}
}
}
Output HTTP Request CS1404 INTERNET PROGRAMMING LABORATORY
C:\IPLAB>javac ViewSource2.java
C:\IPLAB>java ViewSource2 http://172.16.0.15/default.htm
HTTP/1.x 200 OK
Key Content-Length: 14895
Key Content-Type: text/html
Key Last-Modified: Sat, 20 Sep 2008 05:55:01 GMT
Key Accept-Ranges: bytes
Key ETag: "80a87d6be51ac91:489"
Key Server: Student-IIS/6.0
Key X-Powered-By: ASP.NET
Key Date: Mon, 06 Oct 2008 02:59:26 GMT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
OBJECT

To implement the Http Request using socket in java CS1404 INTERNET PROGRAMMING LABORATORY. HTTP –Hyper Text Transfer Protocol. It is maintain the data in the web server. By using the web server it transfer the data. And in these way http it does not perform any authentication



Related post