POST through HTTPs

Y

yancheng.cheok

Hi, I try to perform http POST using
http://martin.nobilitas.com/java/cookies.html

it works.

However, when I try to perform POST using https, by using tips

// Dynamically register the JSSE provider.
java.security.Security.addProvider(new
com.sun.net.ssl.internal.ssl.Provider());

// Set this property to use Sun's reference implementation of the
HTTPS protocol.
System.setProperty("java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");

it won't work anyone. I get error

Exception java.io.IOException: Server returned HTTP response code: 500
for URL: https://www.xyz.com/dologin.jsp

any suggestion?

thanks
 
Y

yancheng.cheok

/*
* Main.java
*
* Created on April 15, 2007, 10:05 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package oskcookies;

import java.net.*;
import java.io.*;

/**
*
* @author yccheok
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/** Post a string to an URL and get the reply as a string. Returns an
empty
string if things didn't work out. */
static public String getURLPostString(URL url, String body) {
StringBuffer sb = new StringBuffer();

// find the newline character(s) on the current system
String newline = null;
try {
newline = System.getProperty("line.separator");
} catch (Exception e) {
newline = "\n";
}

try {
// URL must use the http protocol!
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setRequestMethod("POST");
conn.setAllowUserInteraction(false); // you may not ask
the user
conn.setDoOutput(true); // we want to send things
// the Content-type should be default, but we set it
anyway
conn.setRequestProperty( "Content-type", "application/x-
www-form-urlencoded" );
// the content-length should not be necessary, but we're
cautious
conn.setRequestProperty( "Content-length",
Integer.toString(body.length()));

// get the output stream to POST our form data
OutputStream rawOutStream = conn.getOutputStream();
PrintWriter pw = new PrintWriter(rawOutStream);

pw.print(body); // here we "send" our body!
pw.flush();
pw.close();

// get the input stream for reading the reply
// IMPORTANT! Your body will not get transmitted if you
get the
// InputStream before completely writing out your output
first!
InputStream rawInStream = conn.getInputStream();

// get response
BufferedReader rdr = new BufferedReader(new
InputStreamReader(rawInStream));
String line;

while ((line = rdr.readLine()) != null) {
sb.append(line);
sb.append(newline);
}
return sb.toString();
} catch (Exception e) {
System.out.println("Exception "+e.toString());
e.printStackTrace();
}
return ""; // an exception occurred
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
// Dynamically register the JSSE provider.
java.security.Security.addProvider(new
com.sun.net.ssl.internal.ssl.Provider());
// Set this property to use Sun's reference implementation
of the HTTPS protocol.
System.setProperty("java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");

URL url = new java.net.URL("https://www.abc.com/
dologin.jsp");

String txtUserName =
"txtUserName="+URLEncoder.encode("username");
String txtPassword =
"txtPassword="+URLEncoder.encode("name");
String _continue = "continue="+URLEncoder.encode("/
quotes.jsp");
String sessionError =
"sessionError="+URLEncoder.encode("0");
String body = txtUserName + "&" + txtPassword + "&" +
_continue + "&" + sessionError;

/* https://www.abc.com/dologin.jsp?txt...=password&continue=/quotes.jsp&sessionError=0
*/

System.out.println(getURLPostString(url, body));
}
catch(java.net.MalformedURLException exp) {
exp.printStackTrace();
}
}

}

please take note that, when i directly enter this URL in my web
browser (https://www.abc.com/dologin.jsp?
txtUserName=username&txtPassword=password&continue=
%2Fquotes.jsp&sessionError=0), the page can be login successful.

However, if I execute the above code, I get the following error:

Exception java.io.IOException: Server returned HTTP response code: 500
for URL: https://www.abc.com/dologin.jsp
 
Y

yancheng.cheok

I try to test using

if(conn instanceof javax.net.ssl.HttpsURLConnection) {
System.out.println("YES! is HttpsURLConnection");
}
else
{
System.out.println("is not HttpsURLConnection");
}

I realize that when I have the operation
// Dynamically register the JSSE provider.
java.security.Security.addProvider(new
com.sun.net.ssl.internal.ssl.Provider());

// Set this property to use Sun's reference
implementation of the HTTPS protocol.
System.setProperty("java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");

I get none-HttpsURLConnection. I remove the above two lines, I will
get javax.net.ssl.HttpsURLConnection. However, I still get 500 error
code.

I am trying to login into https://www.osk188.com/login.jsp?continue=/quotes.jsp

What else steps I had missed out?

Thanks
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top