Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 3rd, 2005, 10:58 AM
nthoethata nthoethata is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: Johannesburg, South Africa
Posts: 4 nthoethata User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 6 sec
Reputation Power: 0
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

Hi everyone,



I am new to JSSE and I have to develop a java client that is going to send and receive data to a https server. I am using java version "1.4.2_04" on windows XP. The administrator on the server side has given me a file called "cacerts.pem" , I assume this is certificate, and another one called "nat.cer". I have written my client as follows :



import java.net.*;
import javax.net.ssl.*;
import java.util.*;
import javax.net.*;
import java.io.*;
import java.security.*;
import java.security.cert.X509Certificate;


public class TestHttpsClient {




public static void main(String[] args) {
try
{


//String natis_url = "https://servername:8443/";

//initialising the string to send.
String send_string = "string t send to the server ";


//verify if the hostname exixts
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: "+urlHostName+" vs. "+session.getPeerHost());
return true;
}
};


HttpsURLConnection.setDefaultHostnameVerifier(hv);


System.out.println("connect.....");

URL url = new URL("
https://servername:8443/");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();


System.out.println("POST parameters.....");
String parms = send_string;
byte[] bytes = parms.getBytes();


conn.setRequestMethod("POST");
conn.setRequestMethod(send_string);
conn.setRequestProperty("Content-type", "text/xml");


conn.setDoInput(true);
conn.setDoOutput(true);
PrintWriter out_write = new PrintWriter(new BufferedOutputStream(conn.getOutputStream()));
out_write.println(parms);


OutputStream out = conn.getOutputStream();
out.write(bytes);
out.flush();
out.close();



System.out.println("response........");


BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String response = reader.readLine();


do {
System.out.println(response);
response = reader.readLine();
} while (response != null);



System.out.println("OK");

}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e);
//serverSocket.close();
}


}



The code compiles fine, and when I try to run it, I get the following error :

c:\_dev> java TestHttpsClient

POST parameters.....
Warning: URL Host: servername vs. servername
response........
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.a(D ashoA6275)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.b(D ashoA6275)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.b(Dasho A6275)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(Dasho A6275)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(Dasho A6275)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(D ashoA6275)
at java.io.BufferedInputStream.fill(BufferedInputStre am.java:183)
at java.io.BufferedInputStream.read1(BufferedInputStr eam.java:222)
at java.io.BufferedInputStream.read(BufferedInputStre am.java:277)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpCl ient.java:756)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.j ava:711)
at sun.net.www.protocol.http.HttpURLConnection.getInp utStream(HttpURLCon
nection.java:626)
at sun.net.www.protocol.https.HttpsURLConnectionImpl. getInputStream(Dash
oA6275)
at NatisHttpsClient.main(TestHttpsClient.java:66)
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure




From the error I fugure that it is able to send something to the server, the problem is with the response, I don't if I am right. I have read through the postings on the net over the past week and haven't been succesfull in getting any where. My main problem is how to tell my program of "nat.cer" of "cacert.pem".



Please help

Nthoethatha

Reply With Quote
  #2  
Old February 24th, 2005, 05:02 AM
rajeshps rajeshps is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 1 rajeshps User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 7 sec
Reputation Power: 0
Having same problem here

Hello,

I am also having same problem, can anyone put some light what might be going wrong with this.

Please help.....
I am with J2sdk1.4.1 and on Linux box with Apache - SSL, Jboss also with JK2 configured.
Through browser all works fine, but when I try to send SOAPMessage via a standalone program having this problem.

any Help is appreciated

raj.

Reply With Quote
  #3  
Old February 28th, 2005, 07:30 AM
nthoethata nthoethata is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: Johannesburg, South Africa
Posts: 4 nthoethata User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 6 sec
Reputation Power: 0
Hi all,

I have successfuly managed to solve this problem.
Rajeshps, please give some details concerning the error you are getting. Also include the code and the error.


Quote:
Originally Posted by rajeshps
Hello,

I am also having same problem, can anyone put some light what might be going wrong with this.

Please help.....
I am with J2sdk1.4.1 and on Linux box with Apache - SSL, Jboss also with JK2 configured.
Through browser all works fine, but when I try to send SOAPMessage via a standalone program having this problem.

any Help is appreciated

raj.

Reply With Quote
  #4  
Old February 28th, 2005, 09:37 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
nthoethata:
I've had my own problems with SSL in the past.
I'm intersted in how you solved your problem...
Just a high-level overview would work.

Reply With Quote
  #5  
Old March 1st, 2005, 07:46 AM
nthoethata nthoethata is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: Johannesburg, South Africa
Posts: 4 nthoethata User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 6 sec
Reputation Power: 0
MadCowDzz :

The problem here is with the certificates. But this depends on how the server is configured as well. My code on this post only sent default certificates that comes with jsse or jdk 1.4x. But my server on the other hand needed a key file with the password, to be specific it is the .pfx file with a password. I include this in my code as follows :

char []passwKey = "Your_password".toCharArray();
KeyStore ts = KeyStore.getInstance("PKCS12");
ts.load(new FileInputStream("Path_to_the_file"), passwKey);
KeyManagerFactory tmf = KeyManagerFactory.getInstance("SunX509");
tmf.init(ts,passwKey);

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(tmf.getKeyManagers(), null, null);
SSLSocketFactory factory =sslContext.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(fact ory);
SSLSocket socket = (SSLSocket) factory.createSocket(host,Port); // Create the ServerSocket

String[] suites = socket.getSupportedCipherSuites();
socket.setEnabledCipherSuites(suites);

//start handshake
socket.startHandshake();


And then afterwards everything worked like magic.

You can email me directly if you have further questions, my email address is : nthoethata@webmail.co.za

nthoethata

Reply With Quote
  #6  
Old March 24th, 2005, 12:55 PM
skmirch skmirch is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 1 skmirch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 32 sec
Reputation Power: 0
I am seeing a similar problem as yours and have tried your solution but to no avail. Is the snippet of code that you sent above applied just once before the client start sending more connection requests? Or, does this have to be repeated for every connection request to the server. Thanks in advance. -- skmirch.

Reply With Quote
  #7  
Old April 11th, 2005, 05:33 AM
nthoethata nthoethata is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: Johannesburg, South Africa
Posts: 4 nthoethata User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 6 sec
Reputation Power: 0
Hi Skmirch,

I am not sure I understand your problem. I don't know if you can connect at all. However if you can't connect, it would help if you can send your code and an error you are getting.

But if you can connect and you want to make more requests on the connection you have established, try thinking of using the server cooki you get after the handshake is succesful to send subsequent requests.

Hope this helps

Quote:
Originally Posted by skmirch
I am seeing a similar problem as yours and have tried your solution but to no avail. Is the snippet of code that you sent above applied just once before the client start sending more connection requests? Or, does this have to be repeated for every connection request to the server. Thanks in advance. -- skmirch.

Reply With Quote
  #8  
Old April 28th, 2006, 06:35 PM
java06_forum java06_forum is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 1 java06_forum User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 53 sec
Reputation Power: 0
Unhappy exception while reading using https

Nthoethatha,

I am having the same prob and I badly need someone help on this

here is the my code to connect to server using Https
//getting the URL from Config file
String sServerName = _pConfig.getProperty("httpUrl");
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session)
{
System.out.println("Warning: URL Host: "+urlHostName+" vs.
"+session.getPeerHost());
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);

oLog.info("Connecting to the server with URL
-->"+sServerName);
URL ur = new URL(sServerName);

sk = (HttpURLConnection) ur.openConnection();
sk.setRequestMethod("POST");
sk.setDoInput(true);
sk.setDoOutput(true);
sk.connect();

after its connect I am trying to read the response using the
following code

BufferedReader inp = new BufferedReader(new
InputStreamReader(sk.getInputStream()));

now I am getting the following error

java.io.IOException: Server returned HTTP response code: 500 for URL:
https://servernameort/
at
sun.net.www.protocol.http.HttpURLConnection.getInp utStream(HttpURLConnection.java:800)
at
sun.net.www.protocol.https.HttpsURLConnectionImpl. getInputStream(DashoA12275)
at com.iphis.util.IphisHTTP.readMessage(IphisHTTP.jav a:97)
at com.iphis.backend.application.Publisher.Publish(IP ublisher.j
ava:134)
at com.iphis.backend.application.Publisher.Publish(IP ublisher.j
ava:134

I am able to do this from local to local server, but not from my
local to actual server

Could you please throw some light on this.

I am using catalina.cer which is provided by my client and I have ran
the following command from my local

C:\>keytool -keystore c:\j2sdk1.4.2_10\jre\lib\security\cacerts
-alias clientCat
alina -import -file catalina.cer
and I am running the client java program using this command
%JAVA_HOME%\bin\java
-Djavax.net.ssl.keyStore=c:\\j2sdk1.4.2_09\jre\lib\ security\cacerts -Djavax.net.ssl.keyStorePassword=changeit javafile


Thanks
SV

Last edited by java06_forum : April 28th, 2006 at 06:36 PM. Reason: gramitical mistakes

Reply With Quote
  #9  
Old June 23rd, 2006, 07:56 AM
jetbrains jetbrains is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 3 jetbrains User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 38 sec
Reputation Power: 0
An example of creating an SSL Client Socket(Java SSL tutorial)
try {
int port = 225;
String hostname = "hotdir.biz";
SocketFactory socketFactory = SSLSocketFactory.getDefault();
Socket socket = socketFactory.createSocket(hostname, port);

InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
in.close();
out.close();
} catch(IOException e) {
}
See here
http://www.developerzone.biz/index.php?option=com_content&task=view&id=97&Itemid=36

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap