Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > URLConnection & SSL

Reply
Thread Tools

URLConnection & SSL

 
 
Chris
Guest
Posts: n/a
 
      10-14-2004
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?


 
Reply With Quote
 
 
 
 
Mark Riordan
Guest
Posts: n/a
 
      10-14-2004

"Chris" <anon> wrote in message news:416eec0e$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
F Saunders
Guest
Posts: n/a
 
      10-15-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Chris" <anon> wrote in message news:416eec0e$(E-Mail Removed)...
> > 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
>
>



 
Reply With Quote
 
Fabian Rossbacher
Guest
Posts: n/a
 
      10-15-2004
F Saunders wrote:

> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>
>> "Chris" <anon> wrote in message news:416eec0e$(E-Mail Removed)...
>> > 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
 
Reply With Quote
 
Mark Riordan
Guest
Posts: n/a
 
      10-15-2004
"F Saunders" <(E-Mail Removed)> wrote in message
news:cknist$8pl$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OutputStream from a URLConnection produces an OutOfMemory OutputStream from a URLConnection produces an OutOfMemory WinstonSmith_101@hotmail.com Java 2 10-25-2006 04:45 PM
write a string from URLConnection to a servlet dave Java 0 12-03-2003 07:47 PM
Problems encoding in utf8 with urlconnection response Bronwyn Howard Java 0 10-01-2003 06:10 AM
URLConnection to WebService gets FileNotFoundException Brock Jones Java 0 09-04-2003 12:10 AM
URLConnection and proxy Kristoffel Java 0 07-27-2003 08:27 PM



Advertisments