Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

POST through HTTPs

Thread view: 
yancheng.cheok@gmail.com - 15 Apr 2007 11:58 GMT
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
yancheng.cheok@gmail.com - 15 Apr 2007 15:31 GMT
/*
* 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?txtUserName=username&txtPassword=password&contin
ue=%2Fquotes.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
yancheng.cheok@gmail.com - 16 Apr 2007 17:56 GMT
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
[quoted text clipped - 4 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.