Cannot connect to an https server

Hi,

I searched far and wide in the forum for a resolution to my problem but did not find any solution. Hence posting the problem here. Please help.

I need to make a call to an https server using a POST request from a SunOS 5.8 machine. Some parameters are to be passed to it. The server would return me back an xml that I need to process. But on execution the class just hangs and on a ^C , I get

a SocketException : Operation already in progress

Is it because I need to do any configurations at my end so as to make the call go through ? Or is there any thing wrong in my code ?

Thanks in advance. I am really looking forward to any help in this regard ssince its of a very high importance that I get it working

The code am trying out is here

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.URL;

import java.net.URLConnection;

import java.util.StringTokenizer;

import javax.net.ssl.HttpsURLConnection;

public class clsInvokeOgone_backup{

private static final String BASE_URL="https://secure.xxxxx.com/ncol/test/orderdirect.asp";

public static void main(String[] args) {

String inputData="orderID=9999&PSPID=TESTDPR&PSWD=9999&amount=500&currency=EUR&COM=blabla&CN=SG&BRAND=VISA&CRNO=1234567890123456&ED=1

2/07&email=abcd@xyz.com";

try {

java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

System.setProperty("java.protocol.handler.pkgs","javax.net.ssl");

String sURL = BASE_URL, sOut = "", line, uri = "";

StringTokenizer tok = new StringTokenizer(sURL);

String protocol = tok.nextToken(":");

String host = tok.nextToken("://");

//String sPostData = postData;

try {

uri += "/" + tok.nextToken("/");

while (tok.hasMoreTokens())

uri += "/" + tok.nextToken("/");

}

catch(Exception nsee) {

uri += "/";

}

System.out.println("protocol -"+protocol+"host = " + host + "uri = " + uri);

URL url = new URL(protocol, host, uri);

HttpsURLConnection con = (HttpsURLConnection)url.openConnection();

// inform the connection that we will send output and accept input

con.setDoInput(true);

con.setDoOutput(true);

// Don't use a cached version of URL connection.

con.setUseCaches (false);

con.setDefaultUseCaches (false);

con.setRequestMethod("POST");

con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

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

//con.connect();

System.out.println("onnect");

// define a new PrintWriter on the output stream

PrintWriter outWriter = new PrintWriter(con.getOutputStream());

outWriter.print("?"+inputData);

outWriter.close();

System.out.println("onnect");

DataInputStream input = new DataInputStream (con.getInputStream ());

String str;

while (null != ((str = input.readLine())))

{

System.out.println (str);

}

input.close ();

} catch(IOException ioe)

{

ioe.printStackTrace();

}

}

}

[3510 byte] By [sagithaa] at [2007-10-2 19:19:42]
# 1
To give you some more information, I am using JDK 1.4.2. The server implements server side SSL and those guys have informed that I would not need any certificates at my end to open up a communication with them ...but as I said above, am at a dead endSagitha
sagithaa at 2007-7-13 21:02:52 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 2
What exactly happens when you uncomment the con.connect(); ?You definitely need to call the connect() method before you can callgetOutputStream().
cdelikata at 2007-7-13 21:02:52 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 3

Sagitha,

Looking at your code, I would like to make two recommendations. I have done similar things in past. Try to create URL only with the BASE_URL and eliminate giving it host and protocol.

Also, I did not see any

httpsURLConnection.disconnect() for disconnecting your connection.

thanks,

lumeta

lumetaa at 2007-7-13 21:02:52 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 4

Thanks lumeta

But 2 reasons why I used this code

1. Because I did try with URL(BASE_URL) and that too gave me the same error

2. If I want to typecast the connection to an HTTPsURLConnection then I need to use the URL(protocol, host, uri) implementation of URL

You are right about the disconnect call not being there in the code. but do oyu think its possible am not able to make a connection because I dont have a disconnect call in the code ? Will try with the disconnect call in there too

Thanks

Warm regards

Sagitha

sagithaa at 2007-7-13 21:02:52 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 5

Sagitha-

The following code alleviates the need to use URL(prot, host, uri).

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

sc.init(null, null, null);

SSLSocketFactory ssf = sc.getSocketFactory();

HttpsURLConnection.setDefaultSSLSocketFactory(ssf);

URL url = new URL("your server url");

HttpURLConnection http = (HttpURLConnection) url.openConnection();

http.setRequestMethod("POST");

http.setDoOutput(true);

http.setRequestProperty("Content-type", "text/xml");

http.connect();

cdelikata at 2007-7-13 21:02:52 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 6

Hi,

Thanks for that. Will definitely try the suggestions. But somehow I have a small inkling that the environment am working in cannot make https connections. Because I took out a code from sun's site that makes a https connection to sun.com and even that failed with the same error. While I try the suggestions, it would be great if someone could comment on that.

Also, am afraid that even though I would be able to make the connection, there might be a problem in sending the parameters from within SUNOS. Am I correct in sending the parameters separated by & the way we do it on a browser ? Because I saw the code was thinking its something to be executed in the background (& is used to indicate that in unix)

Thanks for everything so far

Sagitha

sagithaa at 2007-7-13 21:02:52 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 7

You do not have to worry about the & symbol.

The & symbol puts things in the background when used on the command

line. You have that value hard-coded in your java program where it is

not affected by the unix shell.

If you'd post a link to whatever code you tried from sun's site that didnt work,

that would help me comment on it.

cdelikata at 2007-7-13 21:02:52 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 8

> java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

> System.setProperty("java.protocol.handler.pkgs","javax.net.ssl");

You can delete these two lines if you are >= JDK 1.4.

> PrintWriter outWriter = new PrintWriter(con.getOutputStream());

> outWriter.print("?"+inputData);

> outWriter.close();

As this is HTTP I would use a PrintStream not a PrintWriter unless you're confident about what's at the other end. Second, you should flush() here but definitely not close().

I suggest you get it working via HTTP first then switch to HTTPS.

ejpa at 2007-7-13 21:02:52 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 9
Hi delikatFrom sun's site I tried the ReadHttpsURL1.java sample available at http://java.sun.com/developer/technicalArticles/Security/secureinternet2/At the prompt I executed the class for www.sun.comThanks in advanceSagitha
sagithaa at 2007-7-13 21:02:52 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 10

Hmm. If you cant get this class to work, maybe you're right- there could be

some firewall issue preventing outgoing https connections. To verify this,

try going to an https site in your browser.

I understand you're on a Solaris box and may not have access to a gui

environment and browser. If that's the case, you could try a command line

program like curl or lynx.

If those arent options, you could try to telnet to port 443 on a machine

that you know is running https.

For example, if you get this, you can make https connections.

~>telnet www.sun.com 443

Trying 72.5.124.61...

Connected to www.sun.com.

Escape character is '^]'.

If it just hangs at the Trying ... and never displays the Connected to...

then you have a problem.

cdelikata at 2007-7-13 21:02:52 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 11
Thanks cdelikatam not able to telnet to www.sun.com to the https port. It hangs at Trying ...Thanks you lumeta & ejp too for your contribution in nailing my problemSagithaMessage was edited by: sagitha
sagithaa at 2007-7-13 21:02:52 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 12
Hi Sagitha,I'm having the same problem on SunOS 5.9 and was wondering if you were able to figure out the cause or fix for this problem?Thanks,MansoorMessage was edited by: Mansoor
Mansoora at 2007-7-13 21:02:52 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...