Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > POST through HTTPs

Reply
Thread Tools

POST through HTTPs

 
 
yancheng.cheok@gmail.com
Guest
Posts: n/a
 
      04-15-2007
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

 
Reply With Quote
 
 
 
 
yancheng.cheok@gmail.com
Guest
Posts: n/a
 
      04-15-2007
/*
* 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?txtU...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

 
Reply With Quote
 
 
 
 
yancheng.cheok@gmail.com
Guest
Posts: n/a
 
      04-16-2007
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=%2Fquotes.jsp

What else steps I had missed out?

Thanks

 
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
server side redirect https => https NOT working Axel ASP General 8 04-27-2009 02:02 AM
https authentication & storing https page in string Naveen Dhanuka Ruby 1 09-19-2007 02:05 PM
open-uri and HTTPS, or net/https with a redirect jotto Ruby 4 10-02-2006 07:26 AM
Beginner needs help. *SOME* https sites inaccesible through router ??? big chris Cisco 4 01-18-2004 11:30 PM
Post post post. Shel-hed Computer Support 2 11-08-2003 07:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57