vendredi 6 mars 2015

HTTP/HTTPS client using Java Socket programming



I'm trying to make a simple HTTP/HTTPS client using java. What i have done as of now in my Client.java file is here.


Everything is working good when i try to access www.google.com:80.



I'm getting the full HTML content in my response BufferedReader.



But, when i try to access www.google.com:443



There is no data coming through BufferedReader



For www.facebook.com:80,



HTTP/1.1 302 Found



Also, when i try with www.facebook.com:443, Getting the following error:



Exception in thread "main" java.net.SocketException: Connection reset



Where am i going wrong? Why am i not able to get any response for the HTTPS sites?


Any help here is appreciated.



public class Client {

public static void main(String[] args) throws IOException {

//String host = args[0];
//int port = Integer.parseInt(args[1]);
//String path = args[2];

int port = 80;
String host = "www.google.com";
String path = "/";

//Opening Connection
Socket clientSocket = new Socket(host, port);
System.out.println("======================================");
System.out.println("Connected");
System.out.println("======================================");

//Declare a writer to this url
PrintWriter request = new PrintWriter(clientSocket.getOutputStream(),true);

//Declare a listener to this url
BufferedReader response = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

//Sending request to the server
//Building HTTP request header
request.println("GET "+path+" HTTP/1.1"); //"+path+"
request.println("Host: "+host);
request.println("Connection: close");
request.println("");
request.flush();

System.out.println("Request Sent!");
System.out.println("======================================");

//Receiving response from server
String responseLine;
while ((responseLine = response.readLine()) != null) {
System.out.println(responseLine);
}
System.out.println("======================================");
System.out.println("Response Recieved!!");
System.out.println("======================================");
request.close();
response.close();
clientSocket.close();
}
}



Aucun commentaire:

Enregistrer un commentaire