[Certificate] How to Communicate/Connect to https via proxy

Hi,

I am trying to do a HTTPS post to a website through javacode. I am receiving the error Unable connect to server-https. Proxy returns "HTTP/1.1 500 Server Error"!

package sslproject;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.MalformedURLException;

import java.net.URL;

import javax.net.ssl.*;

import java.security.KeyManagementException;

import java.security.NoSuchAlgorithmException;

publicclass HtpsConnection{

publicstatic String urlHost ="https://X.X.X.X:443/servletHTTPS";

publicstaticvoid main (String[] args)throws IOException{

System.out.println(getPage(urlHost).toString());

}

publicstatic StringBuffer getPage(String urlString)

{

publicstatic String proxyPwd ="proxyPWD";

publicstatic String proxyUser ="proxyUSER";

publicstatic String proxyHost ="proxyIP";

publicstaticintproxyPort = port_proxy;

System.getProperties().put("javax.net.debug","ssl" );

System.getProperties().put("https.proxySet","true");

System.getProperties().put("https.proxyHost", proxyHost);

System.getProperties().put("https.proxyPort", proxyPort);

TrustManager[] trustAllCerts =new TrustManager[]{

new X509TrustManager(){

publicboolean checkClientTrusted(java.security.cert.X509Certificate[] chain){

returntrue;

}

publicboolean isServerTrusted(java.security.cert.X509Certificate[] chain){

returntrue;

}

publicboolean isClientTrusted(java.security.cert.X509Certificate[] chain){

returntrue;

}

public java.security.cert.X509Certificate[] getAcceptedIssuers(){

returnnull;

}

publicvoid checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType){}

publicvoid checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType){}

}

};

try

{

SSLContext sc = SSLContext.getInstance("SSL");

sc.init(null, trustAllCerts,null);

HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

}

catch(NoSuchAlgorithmException nsae)

{}

catch(KeyManagementException kme)

{

kme.printStackTrace();

}

try

{

HttpsURLConnection connec =null;

URL url =new URL(urlString);

connec = (HttpsURLConnection)url.openConnection();

connec.setDoInput(true);

connec.setUseCaches(false);

String authentication = proxyUser +":" + proxyPwd;

String encodedPassword ="Basic " +new sun.misc.BASE64Encoder().encode(authentication.getBytes());

connec.setRequestProperty("Proxy-Authorization", encodedPassword);

connec.setRequestMethod("POST");

connec.setDoOutput(true);

String msg;

msg=""+"\r\n";

int statusCode = connec.getResponseCode();

System.err.println("Certificats >"+connec.getServerCertificates());

System.err.println("HEADER >"+connec.getHeaderFields());

StringBuffer pageContents =new StringBuffer();

if(statusCode==HttpsURLConnection.HTTP_OK)

{

System.err.println("Connected ...!");

BufferedReader in =new BufferedReader(new InputStreamReader(connec.getInputStream()));

PrintWriter out =new PrintWriter(connec.getOutputStream(),true );

out.println(msg);

String curLine = in.readLine();

while(curLine!=null)

{

pageContents.append(curLine);

curLine = in.readLine();

}

}

return pageContents;

}

catch(MalformedURLException mue)

{

mue.printStackTrace();

}

catch(IOException ioe)

{

ioe.printStackTrace();

}

returnnull;

}

}

when i execute this code java HtpsConnection :

{[HTTP/1.1 500 Internal Server Error], Content-Length=[101], Connection=[Close], Date=[Fri, 01 Dec 2006 11:38:11 GMT], Content-Type=[text/html]}

we usedwget command inunix/linux environnement :

root@becane:~# wget ip_server_https 443

...

Connecting to X.X.X.X:443... connected.

ERROR: Certificate verification error for X.X.X.X : unable to get local issuer certificate

ERROR: certificate common name `X.X.X.X' doesn't match requested host name `X.X.X.X:443'.

To connect to X.X.X.X:443 insecurely, use `--no-check-certificate'.

Unable to establish SSL connection.

--17:19:09-- http://443/

=> `index.html.1'

Resolving 443... 0.0.1.187

Connecting to 443|0.0.1.187|:80... failed: Invalid argument.

FINISHED --17:19:09--

Downloaded: 0 bytes in 0 files

Can you let me know how to solve ?

How to use certificate in my code ?

Thank you very much in advance.

[8910 byte] By [devmed2006@yahoo.fra] at [2007-10-3 11:28:47]
# 1

> System.getProperties().put("https.proxySet", , "true");

This does nothing.

> public boolean checkClientTrusted(java.security.cert.X509Certificate[] chain){

> return true;

> }

> public boolean

> isServerTrusted(java.security.cert.X509Certificate[] chain){

> return true;

> }

> public boolean

> isClientTrusted(java.security.cert.X509Certificate[] chain){

> return true;

> }

These three methods aren't part of the X509TrustManager interface and will never be called.

> public java.security.cert.X509Certificate[] getAcceptedIssuers() {

> return null;

> }

This is specified to return a 'non-null (possibly empty) array'.

I don't understand the rest of your post, in particular how this code prints anything if the result status isn't HTTP_OK.

ejpa at 2007-7-15 13:55:05 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 2

ejpwrote :

>

I don't understand the rest of your post, in

particular how this code prints anything if the

result status isn't HTTP_OK.

Please see this portion of code :

System.err.println("HEADER >"+connec.getHeaderFields());

devmed2006@yahoo.fra at 2007-7-15 13:55:05 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 3
So who prints this:java HtpsConnection :{[HTTP/1.1 500 Internal Server Error], Content-Length=[101], Connection=[Close], Date=[Fri, 01 Dec 2006 11:38:11 GMT], Content-Type=[text/html]}
ejpa at 2007-7-15 13:55:05 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 4

ejpwrote :

So who prints this:

java HtpsConnection :

{[HTTP/1.1 500 Internal Server Error], Content-Length=[101], Connection=[Close], Date=[Fri, 01 Dec 2006 11:38:11 GMT], Content-Type=[text/html]}

I'm sorry, i forgot to write HEADER > in output message, so :

System.err.println("HEADER >"+connec.getHeaderFields());

Print in prompt :

HEADER >{[HTTP/1.1 500 Internal Server Error], Content-Length=[101], Connection=[Close], Date=[Fri, 01 Dec 2006 11:38:11 GMT], Content-Type=[text/html]}

devmed2006@yahoo.fra at 2007-7-15 13:55:05 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 5
could someone help me please ?
devmed2006@yahoo.fra at 2007-7-15 13:55:05 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 6
You are getting a 500 Internal Server Error. This is an internal error in the server. You have to investigate that. You can't do anything about it from the client end.BUThave you fixed the errors in your TrustManager?
ejpa at 2007-7-15 13:55:05 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 7
ejpwrote :have you fixed the errors in your TrustManager?I dont know how to proceed ?Could you help me please ?Thanks
devmed2006@yahoo.fra at 2007-7-15 13:55:05 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 8
I described these errors in reply #1.
ejpa at 2007-7-15 13:55:05 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...