I'm creating a android proxy server and i use this for HTTP request :
Socket socket = new Socket(InetAddress.getByName("perdu.com"), 80);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.print("GET / HTTP/1.1\r\nHost: perdu.com\r\nConnection: close\r\n\r\n");
out.flush();
I doesn't work for HTTPS request, so my question is : How to replace this for SSL working and get server response ?
Thank you
PS: i'm french, sorry for my english :)
I found solution
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) factory.createSocket(domain, 443);
socket.startHandshake();
BufferedReader in = new BufferedReader(new InpuStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
Related
I am stuck in a serious place..., I am trying to create a secure server. So I had to modify my code to be able to send in a secure way, the problem is the following.
It displays me the error: javax.net.ssl.SSLException: Connection has closed: javax.net.ssl.SSLException: Unsupported or unrecognized SSL message
I already made a certificate via keytool but I don't have the impression that it works
I have looked everywhere but I can't find a solution to my problem...
Here are my codes:
Server :
System.setProperty("javax.net.ssl.keyStore", "test.store");
System.setProperty("javax.net.ssl.keyStorePassword", "password");
ServerSocketFactory serverSocketFactory =
ServerSocketFactory.getDefault();
ServerSocket serverSocket =
serverSocketFactory.createServerSocket(4444);
System.out.println("Started...");
while(true){
new ServerThread(serverSocket.accept()).start();
}
Client
System.setProperty("javax.net.ssl.trustStore", "test.store");
SocketFactory socketFactory = SSLSocketFactory.getDefault();
Socket socket = socketFactory.createSocket("localhost", 4444);
BufferedReader bf1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter username");
writer.println(bf.readLine());
String message = null;
while(true){
System.out.println("enter message send to server");
message = bf.readLine();
if(message.equals("quit")){
socket.close();
break;
}
writer.println(message);
System.out.println("message from server : " + bf1.readLine());
}
I don't thing it is important to give you the serverThread cause it is not usefull.
Thank and have nice day :)
You server is not listening in SSL as you are calling the wrong factory. You would need SSLServerSocketFactory instead. Try
ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();
So I am attempting to send data to myself and receive the data then print it, now I have been testing for a while and I have noticed its not sending anything, in fact, maybe it is and I am not receiving it properly, I need assistance with this please.
This is what I am using to send data
String host = "127.0.0.1";
int port = Options.port;
Socket socket = new Socket(host, port);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(msg + "\n");
bw.flush();
This is what I am using to receive, I always use this method and it never works so I would not be surprised if this was the root cause.
ServerSocket serverSocket = new ServerSocket(Options.port);
System.out.println("[Listening on port] " + Options.port);
while(true){
Socket socket = serverSocket.accept();
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println(message);
}
1) replace:
Socket socket = serverSocket.accept();
socket = serverSocket.accept();
with:
Socket socket = serverSocket.accept();
2) If this does not solve the problem, set ports as int values, without Options.port
i'm working on an application to print to my local wifi printers but over 9100 port i have no response, over the 515 i have "connection refused".
the code is this:
Socket test = new Socket();
Socket sock = new Socket(ip,9100);
PrintWriter oStream = new PrintWriter(sock.getOutputStream());
oStream.printf("========================\n");
oStream.printf("PROVA STAMPA\n");
oStream.printf("HELLO SLOW SUD\n");
oStream.printf("========================"+"\n");
oStream.close();
sock.close();
and also i have try this:
Socket objSocket = new Socket();
InetSocketAddress objEndPoint = new InetSocketAddress(ip, 9100);
DataOutputStream objOutputStream;
objSocket.connect(objEndPoint, 6000);
objOutputStream = new DataOutputStream(objSocket.getOutputStream());
objOutputStream.write(("========================\nPROVA STAMPA\nHELLO SLOW SUD\n========================\n").getBytes());
objOutputStream.close();
objSocket.close();
I have HttpsURLConnection opened.
How can i "connect" my ssl socket to this connection, means not to do another ssl handshake and don't change my ciphers.
URL url = new URL ("https://example.com:8080");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.connect();
SSLSocketFactory ssf = connection.getSSLSocketFactory();
SSLSocket socket = (SSLSocket) ssf.createSocket("example.com", 8080);
// and here i want to send message with socket which will be in the same ssl session as https connection.
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(s.getOutputStream()));
out.write("Hello");
out.flush();
out.close();
Is it really to do such thing in Java?
It should happen that way by default, as long as the session remains valid, and provided you haven't messed around with SSLContexts.
AFAIK, you cannot attach a new socket to the existing SSL session. If you want to send/read data on the original SSL session, use the original socket connection created by the URLConnection object. You can use the URLConnection.getInputStream() and URLConnection.getOutputStream() methods to get the input/output streams for it:
URL url = new URL ("https://example.com:8080");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.connect();
...
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
out.write("Hello");
out.flush();
out.close();
My app is based on Sockets, but regular Sockets, not SSLSockets. Do I have to change all of them to SSL sockets to be able connect to https server?
You must change how the socket is created, by using the SSLSocketFactory, but you don't have to change all your datatypes from Socket to SSLSocket.
Yes i think you have to change for SSL, you can try that :
System.setProperty("javax.net.ssl.trustStore", "clienttrust");
SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
Socket s = ssf.createSocket("127.0.0.1", 8888);
OutputStream outs = s.getOutputStream();
PrintStream out = new PrintStream(outs);
InputStream ins = s.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(ins));
out.println("Hi,How are u!");
out.println("");
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
out.close();
it's from : http://www.java2s.com/Tutorial/Java/0490__Security/HttpsSocketClient.htm
Hope it helps !