POST through HTTPs

Discussion in 'Java' started by yancheng.cheok@gmail.com, Apr 15, 2007.

  1. Guest

    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
    , Apr 15, 2007
    #1
    1. Advertising

  2. Guest

    /*
    * 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
    , Apr 15, 2007
    #2
    1. Advertising

  3. Guest

    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
    , Apr 16, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. agusayala

    Create pdf file through https problem

    agusayala, Dec 9, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    503
    Joerg Jooss
    Dec 9, 2005
  2. Gustavo Rahal
    Replies:
    0
    Views:
    720
    Gustavo Rahal
    Aug 12, 2008
  3. Axel
    Replies:
    8
    Views:
    1,060
    Adrienne Boswell
    Apr 27, 2009
  4. jotto
    Replies:
    4
    Views:
    373
    jotto
    Oct 2, 2006
  5. Naveen Dhanuka
    Replies:
    1
    Views:
    263
Loading...

Share This Page