dcsimg
Problems when trying to post to a server with URLConnection using SSL...
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Claes_Theander
Posted On:   Monday, April 29, 2002 05:52 AM

Hello! I'm trying to post to a server with java.net.URLConnection over SSL(https). The problem is that the server seems to only get the header( POST / HTTP/1.1 ) not the content which is a xml string. I have created a keystore/certificate on the serverside and I have used the java keytool to generate the key. The client/server(se sample code below) works fine when I'm using the ordinary http protocol. Any suggestions someone? Thanks in advance. Claes. //------------------------------------------------------ //Client: URL u = new URL(url); HttpsURLConnection conn = (HttpsURLConnection).openConnection(); conn.setDoInput(true);    More>>

Hello!

I'm trying to post to a server with java.net.URLConnection over SSL(https). The problem is that the server seems to only get the header( POST / HTTP/1.1 ) not the content which is a xml string. I have created a keystore/certificate on the serverside and I have used the java keytool to generate the key. The client/server(se sample code below) works fine when I'm using the ordinary http protocol.
Any suggestions someone?



Thanks in advance.

Claes.

			
//------------------------------------------------------
//Client:


URL u = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection).openConnection();



conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.connect();


PrintWriter out = new PrintWriter(conn.getOutputStream());


//Send HTTP-Post request...
out.println("data="+URLEncoder.encode(xmlMessage));
out.close();

//------------------------------------------------------
//Server


//SSL initializing...
SSLContext ctx;
KeyManagerFactory kmf;
KeyStore ks;
ctx = SSLContext.getInstance("TLS");
kmf = KeyManagerFactory.getInstance("SunX509");
ks = KeyStore.getInstance("JKS");


char[] pass = "xxx".toCharArray();
ks.load(new FileInputStream("keystore"),pass);
kmf.init(ks, passphrase);
ctx.init(kmf.getKeyManagers(), null, null);


SSLServerSocketFactory sslSrvFact = ctx.getServerSocketFactory();
_ss = (SSLServerSocket)sslSrvFact.createServerSocket(_port);


//...


_socket = (SSLSocket)_ss.accept();


_in = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
String line = null;
StringBuffer message = new StringBuffer();


while((line = _in.readLine()) != null) {
if(_urlEncoding) {
message.append(URLDecoder.decode(line));
}


//...


System.out.println(message.toString());


//------------------------------------------------------
   <<Less
About | Sitemap | Contact