Question about posting to https

Hey guys,

I'm trying to do a program that'll post to an https site with some parameters such as username and password using a post action. I have read a bunch of code out there and sort of hacked together something simple I thought would work but I'm definitely doing something wrong. Could anybody tell me if there is something obvious I'm missing with how to get a succesful post action to an https link with parameters on the end via using java code? Right now it compiles perfectly, will run with no errors but I can tell it isn't posting properly to the website so I'm probably missing many somethings that make this not work.

Thanks!

Josh

import java.io.*;

import java.net.*;

import java.util.*;

import javax.net.ssl.*;

import java.net.URL.*;

import java.security.Security;

import java.util.Properties;

import java.io.OutputStreamWriter;

publicclass MyTest2

{

publicstaticvoid main(String[] args)

{

try{

File startFile=new File("test");

String[] theFiles=startFile.list();

// Construct data

String data = URLEncoder.encode("email","UTF-8") +"=" + URLEncoder.encode("myUserEmail","UTF-8");

data +="&" + URLEncoder.encode("password","UTF-8") +"=" + URLEncoder.encode("myPassword","UTF-8");

// Send data

System.out.println(data);

URL url =new URL("https://siteImtrying.com");

URLConnection conn = url.openConnection();

conn.setDoOutput(true);

OutputStreamWriter wr =new OutputStreamWriter(conn.getOutputStream());

wr.write(data);

wr.flush();

// Get the response

BufferedReader rd =new BufferedReader(new InputStreamReader(conn.getInputStream()));

String line;

while ((line = rd.readLine()) !=null)

{

System.out.println(line);

}

wr.close();

rd.close();

}catch (Exception e){

}

}

}

[3417 byte] By [JoshRa] at [2007-11-26 13:56:35]
# 1
Look here: http://forum.java.sun.com/thread.jspa?forumID=31&threadID=430139And search more for "https post".You might also have trouble with the certificate if it's not trusted look here: http://www.exampledepot.com/egs/javax.net.ssl/TrustAll.html
Rodney_McKaya at 2007-7-8 1:36:10 > top of Java-index,Java Essentials,Java Programming...
# 2

At the very mimimum you should change your code from

} catch (Exception e) {

}

to

} catch (Exception e) {

e.printStackTrace();

}

It is very hard to fix a problem for which you don't know the exception you may or may not be getting. In most cases (not all) you should at least do the printStackTrace() for just such occasions.

And in this example I wouldn't catch all exceptions. Just catch the "catchable" exceptions, let the RuntimeException be caught by the vm.

Caffeine0001a at 2007-7-8 1:36:10 > top of Java-index,Java Essentials,Java Programming...
# 3

> You might also have trouble with the certificate if it's not trusted

That's one possibility. There are many others, including much more obvious ones, that aren't eliminated yet because the OP hasn't given us any description of the actual problem. Here's one problem that I can see:} catch (Exception e) {

}

If the program throws an exception for any reason, the program appears to end normally.

DrClapa at 2007-7-8 1:36:10 > top of Java-index,Java Essentials,Java Programming...
# 4
You are right.I was thinking this couldn't be the code the OP is using.He's not event setting the request method to POST.
Rodney_McKaya at 2007-7-8 1:36:10 > top of Java-index,Java Essentials,Java Programming...
# 5

> He's not even setting the request method to POST.

Another possibility. When I had to do this I used Jakarta HttpClient to take care of those details rather than trying to deal with them on my own. It worked very well.

(And I did run into the untrusted certificate problem, too, but if Windows had been my platform that wouldn't have been a problem either.)

DrClapa at 2007-7-8 1:36:10 > top of Java-index,Java Essentials,Java Programming...
# 6
Wow that's embarassing, can't believe I missed that. I'm getting a problem now once i added the print for the exception. Sorry for being a newb, i'm going to dig into the trace a little to see if anything in the trace points me to something and will post again.
JoshRa at 2007-7-8 1:36:11 > top of Java-index,Java Essentials,Java Programming...
# 7
Thanks much you guys,The exception not being caught was definitely part of the problem and http://www.exampledepot.com/egs/javax.net.ssl/TrustAll.html helped me fix it completely. You rock dukes awarded to caff and rod for being the first to catch the probs. Thanks!Josh
JoshRa at 2007-7-8 1:36:11 > top of Java-index,Java Essentials,Java Programming...
# 8
I found http://www.java-samples.com/java/POST-toHTTPS-url-free-java-sample-program.htm this java sample for posting to https url useful.
vilyamsa at 2007-7-8 1:36:11 > top of Java-index,Java Essentials,Java Programming...