Forget that ;-)
Main Topics
Browse All TopicsExpert gentlebeings,
I am using the following method (in a Jetty webserver) to exchange information with another webserver:
String exchMsg(HttpResponse response,StringBuffer sb,boolean sendok)
{ HttpURLConnection conn=null;
try
{ URL address=new URL("https://"+ipxml+":"+p
conn=(HttpURLConnection)ad
if(conn==null)
{ Log.warning("error connecting");
sendResp(response,"send error!");
return(null);
}
} catch(Exception e)
{ Log.warning("error connecting: "+e);
sendResp(response,"send error!");
return(null);
}
try
{ conn.setRequestMethod("POS
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
OutputStream outs=conn.getOutputStream(
outs.write(sb.toString().g
outs.flush();
outs.close();
} catch(Exception e)
{ Log.warning("error sending: "+e);
sendResp(response,"send error!");
return(null);
}
Log.event("SENT to "+ipxml+":"+portxml+" "+sb);
if(sendok)sendResp(respons
try
{ InputStream ins=conn.getInputStream();
byte bs[]=new byte[1000];
sb=new StringBuffer(1000);
int n;
do
{ if((n=ins.read(bs))!=-1)sb
} while(!sb.toString().endsW
ins.close();
} catch(Exception e)
{ Log.warning("error receiving: "+e);
return(null);
}
Log.event("RECV "+sb.toString());
return(sb.toString());
}
Now, when I was connecting to an unencrypted port, everything worked fine. However, in attempting to get it encrypted, I changed the URL to be https, connected to a different port on their end (that provides an encrypted connection), and now I get an exception in the second try/catch block, presumably at the conn.connect() statement:
08:12:51.921 WARN!! error sending: java.net.ConnectException:
The guys at the other end indicate that I am not processing their public key, and that they don't have provision for processing a public key from us (not that I was intending to provide one), and they are getting a handshake exception:
HTTPRequestHandler.getRequ
Received fatal alert: certificate_unknown (HTTP Request handler) 2005-09-30 14:36:57,073 [HTTPRequest.java] DEBUG - Data has not been sent from another process, sending data... (HTTP Request handler) 2005-09-30 14:36:57,074 [HTTPResponse.java] DEBUG - Status code 400 (HTTP Request handler) 2005-09-30 14:36:57,074 [HTTPResponse.java] DEBUG - Sending standard request (HTTP Request handler) 2005-09-30 14:36:57,074 [HTTPResponse.java] DEBUG - Checking security, authorization string null (HTTP Request handler) 2005-09-30 14:36:57,075 [HTTPConst.java] DEBUG - Encoding URL: /~HTTPServerImages/notvali
HTTPResponse.send()javax.n
shutdown: javax.net.ssl.SSLHandshake
What must I do/add to get this to work?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
> What must I do/add to get this to work?
You need the proper key to connect to the server and from the error message you are getting it seems to me that the server does not recognize your client. In order to connect to a secure server (https) it is not only enough to just change the url from http to https but you also need a proper certificate. Make sure that you have a certificate that the server you are trying to connect to recognizes. If you do not know what this is get in touch with the people who provided you with the keystore.
Basically what is going on is that you are trying to exchange key information with the server and the information you send and the information that the server has does not match.
You can have a look at this link to get an idea of how things work: http://www.andrew.cmu.edu/
CEHJ: Jetty is running on 1.4.2_06
praveen: As the URL.openConnection method will return an appropriate object, which in this case will be an HttpURLConnection object or something derived from it, and since I am only using methods defined in HttpURLConnection, and since the cast worked, I can see no reason for making the cast more specific.
girionis: I was not (rightly or wrongly) provided with a keystore. Doesn't some "automatic" mechanism exist to manage this stuff? When I go to look at a secure site with my web browser I don't have to first have the web owners "provide me with a keystore". The browser does some stuff, obtains a certificate (I assume this is something that contains the public key of the entity one is trying to connect to), holds it up to the browser user for inspection in a dialog, and then (if the user OK's it) everything proceeds.
Many of the common browser already implement support for https which allows access to secured communications without requiring the socket-level API provided with JSSE, but there are also cases where you are prompted with a pop up box that informs you if you want to accept the certificate.
Try this approach: http://javaalmanac.com/egs
I always say this but use the jakarta http client.. it already has the SSL stuff figured out and will save you many headaches..
http://jakarta.apache.org/
imladris: the standard java.net.URL class doesn't support SSL. SSL involves encrypting the stream with PKCS, which involves a handshake to be established between the client and the server to exchange certificates/keys for the encryption. During the connection, the client and server exchange credentials and negotiate the security parameters.
You would have to install the public key certificate on the client(jetty webserver) of the host(server, "https://"+ipxml) you're establishing the connection with.
I totally agree with girionis's perception. the "automatic" mechanism exists for web browsers, which would prompt whether you want to install the certificate, but for programmatic JSSE connections, the certificate needs to be installed and valid before the connection can be made.
http://www.onjava.com/pub/
girionis: just completed the trustall approach with no apparent effect. I added the following to the end of the my jetty HttpHandler:
TrustManager[] trustAllCerts=new TrustManager[]
{ new X509TrustManager()
{ public java.security.cert.X509Cer
{ return(null);
}
public void checkClientTrusted(java.se
{
}
public void checkServerTrusted(java.se
{
}
}
};
try
{ SSLContext sc=SSLContext.getInstance(
sc.init(null,trustAllCerts
HttpsURLConnection.setDefa
} catch(Exception e)
{
}
Also, I assume that what I would be losing with this approach is the assurance that I am talking to the correct host. I don't know whether that will be acceptable in the long run or not. I guess I'll try talking to the guys on the other side and see about getting a certificate from them.
You don't need to do anything.. the httpclient acts just like a browser. Can you connect to their site using a browser? If so using a java "browser" like httpclient will work in a similar fashion. If you connect to their site and you get an untrusted popup asking you to trust they cert signer (e.g. it isn't verisign, thawte, etc), httpclient can handle these scenarios as well.
Investigation continues.
I have requested coding help from the guys on the other end; but they don't appear to see any problems with the code. It appears, in retrospect, that the other end is not an existing, performing, commercial site, yet, either. They listed a new ip and set of ports and requested I try those. Without the trustall code I got this exception: "javax.net.ssl.SSLHandshak
Does that suggest any useful action to take?
They will also be looking at the logs on their side.
Mathew, I will keep the httpClient in mind. I would prefer to not get involved in yet another license (no matter how mild) if I can avoid it. But I'll try it next week sometime if there isn't any progress.
The key here is to figure out which side the problem is on.. To do that I would use a process of elimination.. First things first.. which side is having the problem client or server?
To eliminate the server.. use a known client:
Have you tried accessing the site via a browser? If so, what kind of warnings to you get? Is the server cert signed by a known CA?
Next move down a layer to java:
Try the httpClient, is that it is a well tested and frequently used client and see if it works.. If it does and you don't want to use the apache license.. you can at least download the source code to see where your code isn't up to muster.
Investigation continues to continue.
Latest error I get back from the other side is:
java.io.IOException: HTTPS hostname wrong: should be <xxx.xxx.xxx.xxx>
between the angle brackets is the ip that I was using to reach it.
Girionis, does that suggest any useful action to take?
Mathew,
If I access the address with https://<ip>:port, then I first get the dialog indicating I am about to view pages over a secure connection, then I hit OK and it comes up with a Security Alert dialog asking me if I want to trust the certificate. If I proceed it then comes up with a dialog indicating there is both secure and nonsecure content on the page. When I indicate that YES I want to see the nonsecure items too, I get a page with an Error 403: Access forbidden; it is impossible to view this page. It is not accessible from this service.
I remain convinced that the standard Java tools ought to work. However, since I'm not getting anywhere, I took the plunge on setting up a parallel project with the Apache client. I won't bore with what it took to find the various pieces and figure out how to run them. Since the tutorial is for 3.0, I am running the 3.0 client which is from 3 days ago (11 Oct 2005) (Yikes!). Once I got it compiled it worked with http without any trouble.
However, setting the address to https got me the old
"HttpClient exception e: javax.net.ssl.SSLHandshake
and I got that, even if I added girionis's TrustAll dodge. I assume, in retrospect, that code won't affect the internals of how the HttpClient operates. It's resetting something in HttpsURLConnection.
Here is the Apache version for the exchMsg method:
String exchMsg(HttpResponse response,StringBuffer sb,boolean sendok)
{ String postrsp=null;
boolean scs=true;
HttpClient client=new HttpClient();
PostMethod post=new PostMethod("https://"+ipxm
post.setRequestEntity(new StringRequestEntity(sb.toS
try
{ int retcode=client.executeMeth
if(retcode!=HttpStatus.SC_
{ Log.warning("error Posting: "+post.getStatusLine());
scs=false;
}
if(sendok)sendResp(respons
postrsp=post.getResponseBo
} catch(IOException e)
{ Log.warning("HttpClient exception e: "+e);
scs=false;
}
if(scs)Log.event("RECV "+postrsp);
else sendResp(response,"error")
return(postrsp);
}
What would you suggest as a next step?
OK, so much for this week. I'm off for the weekend. Back at it on Monday.
> java.io.IOException: HTTPS hostname wrong: should be <xxx.xxx.xxx.xxx>
Try this: http://forum.java.sun.com/
Ok here are your problems then. ...
>>asking me if I want to trust the certificate
and
>>Error 403: Access forbidden;
The first issue is that the CA that the server side folks used to sign their cert is not one of the few automagically trusted CAs.. they probably used a self-signed cert. So you will need to import that certificate into your truststore before any java app will accept the cert.. this is analagous to you pushing OK from your browser.
OR
using the apache http client you can use the EasySSLSocketProtocolFacto
The 403 will give you problems no matter which client you attempt to use so that will need to be resolved on the server side to work properly. You should be able to hit the server with your browser in any event without error.
Update: still working on this. Unfortunately there was a company move last week, so I didn't get anything done while the network was being taken down, moved, and set back up at the new location. I had to move to of course, and by the time I was up and running again, the company network was up, but then I had to get them to poke holes in the firewall again so I could continue testing this. Hopefully some new results in the next week or so.
Something about not being able to parse.
They requested another attempt this morning, and this time I got the same response as I did on the HTTP link. So it looks like we're underway. I specifically attempted to remove both of girionis's dodges one at a time. But both are necessary to make this work.
Thanks girionis.
I explicitly raised the issue with them, that I am bypassing the checks on whether the software is connecting to the right host, but, so far, they seem unconcerned. I assume that the problem is that something along the following lines could occur: if someone could access the PC running this code and change the configuration to point it to another ip, they could fire up a program on that ip that receives the requests, records the information, passes the request to 3C, and passes back the response. That is, despite the fact that the traffic is encrypted, a single configuration change would enable someone to gain access to all the traffic from a particular site. Is that right? Is there something worse that you could think of happening?
Business Accounts
Answer for Membership
by: CEHJPosted on 2005-09-30 at 08:35:32ID: 14993297
Try
n();
URLConnection conn=address.openConnectio
and then cast as per conn's type