URLConnection & SSL

C

Chris

I'm looking for a fast and simple way to fetch an https page using
URLConnection. This is for an app that will be distributed, so we can't have
any complicated installation steps. I'm not real clear on how certificates
and SSL work. Can anyone point me to a tutorial or cookbook-style
instructions on how to get this to work?
 
M

Mark Riordan

Chris said:
I'm looking for a fast and simple way to fetch an https page using
URLConnection.

Try something like:

try {
String sURL = "https://www.whatever.com";
URL url = new URL(sURL);
URLConnection httpConn = url.openConnection();
httpConn.setDoInput(true);
httpConn.connect();
InputStream in = httpConn.getInputStream();
BufferedInputStream bufIn = new BufferedInputStream(in);
int nbytes;
do {
// Echo the response on the Java Console.
// This is crude, and just for demo purposes.
byte buf[] = new byte[8192];
nbytes = bufIn.read(buf, 0, 8192);
System.out.println(new String(buf,"US-ASCII"));
} while(nbytes > 0);

} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}

This ignores proxies (unless you are running
in a browser), and issues with funny certificates (mismatched
hostname on certificate, etc.).

Mark R
 
F

F Saunders

Have you been able to post to a https.asp with any success?

I'd like to logon via httpConn.connect() to www.comsec.com.au but haven't
been able
to get past step 1 so far. Have tried the obvious .setPost(True), .setInput
(True) etc..

The parameters to the .asp are LoginName, Password. Have tried the ?
loginName=33333&password=98763.

Hope you can help,

regards,
Michael.




Mark Riordan said:
Chris said:
I'm looking for a fast and simple way to fetch an https page using
URLConnection.

Try something like:

try {
String sURL = "https://www.whatever.com";
URL url = new URL(sURL);
URLConnection httpConn = url.openConnection();
httpConn.setDoInput(true);
httpConn.connect();
InputStream in = httpConn.getInputStream();
BufferedInputStream bufIn = new BufferedInputStream(in);
int nbytes;
do {
// Echo the response on the Java Console.
// This is crude, and just for demo purposes.
byte buf[] = new byte[8192];
nbytes = bufIn.read(buf, 0, 8192);
System.out.println(new String(buf,"US-ASCII"));
} while(nbytes > 0);

} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}

This ignores proxies (unless you are running
in a browser), and issues with funny certificates (mismatched
hostname on certificate, etc.).

Mark R
 
F

Fabian Rossbacher

F said:
Have you been able to post to a https.asp with any success?

I'd like to logon via httpConn.connect() to www.comsec.com.au but haven't
been able
to get past step 1 so far. Have tried the obvious .setPost(True),
.setInput (True) etc..

The parameters to the .asp are LoginName, Password. Have tried the ?
loginName=33333&password=98763.

Hope you can help,

regards,
Michael.




Mark Riordan said:
Chris said:
I'm looking for a fast and simple way to fetch an https page using
URLConnection.

Try something like:

try {
String sURL = "https://www.whatever.com";
URL url = new URL(sURL);
URLConnection httpConn = url.openConnection();
httpConn.setDoInput(true);
httpConn.connect();
InputStream in = httpConn.getInputStream();
BufferedInputStream bufIn = new BufferedInputStream(in);
int nbytes;
do {
// Echo the response on the Java Console.
// This is crude, and just for demo purposes.
byte buf[] = new byte[8192];
nbytes = bufIn.read(buf, 0, 8192);
System.out.println(new String(buf,"US-ASCII"));
} while(nbytes > 0);

} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}

This ignores proxies (unless you are running
in a browser), and issues with funny certificates (mismatched
hostname on certificate, etc.).

Mark R

maybe this helps..

public String posti(String u) throws IOException {
System.setProperty("java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()) ;

StringBuffer sb = new StringBuffer();
Enumeration e=params.keys();
while(e.hasMoreElements()){
String key=(String)e.nextElement();
String val=(String)params.get(key);
sb.append(key+"="+URLEncoder.encode(val)+"&");
}

String outputXml = "";
HttpURLConnection conn = null;
URL url = new URL(u);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application
x-www-form-urlencoded");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
conn.connect();
out.writeBytes(sb.toString());
out.flush();
out.close();
DataInputStream in = new DataInputStream(conn.getInputStream());
String line = "";
while ((line = in.readLine()) != null) {
outputXml += line;
}
in.close();
conn.disconnect();
return outputXml;
}
fabian
 
M

Mark Riordan

F Saunders said:
Have you been able to post to a https.asp with any success?

I'd like to logon via httpConn.connect() to www.comsec.com.au but haven't
been able
to get past step 1 so far. Have tried the obvious .setPost(True), ..setInput
(True) etc..

The parameters to the .asp are LoginName, Password. Have tried the ?
loginName=33333&password=98763.

My simple example didn't POST any form fields.
In your case, apparently the application does not accept those
fields as query strings.
So, as Fabian Rossbacher has pointed out, you need to transmit
them as POSTed data. The data need to be encoded in a special format
for the webserver to recognize them as POSTed form fields.
Fabian's posting shows how to do that.

Mark R
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top