URLConnection & SSL

Discussion in 'Java' started by Chris, Oct 14, 2004.

  1. Chris

    Chris Guest

    I'm looking for a fast and simple way to fetch an https page using
    URLConnection. This is for an app that will be distributed, so we can't have
    any complicated installation steps. I'm not real clear on how certificates
    and SSL work. Can anyone point me to a tutorial or cookbook-style
    instructions on how to get this to work?
     
    Chris, Oct 14, 2004
    #1
    1. Advertising

  2. Chris

    Mark Riordan Guest

    "Chris" <anon> wrote in message news:416eec0e$...
    > I'm looking for a fast and simple way to fetch an https page using
    > URLConnection.


    Try something like:

    try {
    String sURL = "https://www.whatever.com";
    URL url = new URL(sURL);
    URLConnection httpConn = url.openConnection();
    httpConn.setDoInput(true);
    httpConn.connect();
    InputStream in = httpConn.getInputStream();
    BufferedInputStream bufIn = new BufferedInputStream(in);
    int nbytes;
    do {
    // Echo the response on the Java Console.
    // This is crude, and just for demo purposes.
    byte buf[] = new byte[8192];
    nbytes = bufIn.read(buf, 0, 8192);
    System.out.println(new String(buf,"US-ASCII"));
    } while(nbytes > 0);

    } catch (Exception e) {
    System.out.println("Exception: " + e.getMessage());
    }

    This ignores proxies (unless you are running
    in a browser), and issues with funny certificates (mismatched
    hostname on certificate, etc.).

    Mark R
     
    Mark Riordan, Oct 14, 2004
    #2
    1. Advertising

  3. Chris

    F Saunders Guest

    Re: Mark can you help with;

    Have you been able to post to a https.asp with any success?

    I'd like to logon via httpConn.connect() to www.comsec.com.au but haven't
    been able
    to get past step 1 so far. Have tried the obvious .setPost(True), .setInput
    (True) etc..

    The parameters to the .asp are LoginName, Password. Have tried the ?
    loginName=33333&password=98763.

    Hope you can help,

    regards,
    Michael.




    "Mark Riordan" <> wrote in message
    news:...
    >
    > "Chris" <anon> wrote in message news:416eec0e$...
    > > I'm looking for a fast and simple way to fetch an https page using
    > > URLConnection.

    >
    > Try something like:
    >
    > try {
    > String sURL = "https://www.whatever.com";
    > URL url = new URL(sURL);
    > URLConnection httpConn = url.openConnection();
    > httpConn.setDoInput(true);
    > httpConn.connect();
    > InputStream in = httpConn.getInputStream();
    > BufferedInputStream bufIn = new BufferedInputStream(in);
    > int nbytes;
    > do {
    > // Echo the response on the Java Console.
    > // This is crude, and just for demo purposes.
    > byte buf[] = new byte[8192];
    > nbytes = bufIn.read(buf, 0, 8192);
    > System.out.println(new String(buf,"US-ASCII"));
    > } while(nbytes > 0);
    >
    > } catch (Exception e) {
    > System.out.println("Exception: " + e.getMessage());
    > }
    >
    > This ignores proxies (unless you are running
    > in a browser), and issues with funny certificates (mismatched
    > hostname on certificate, etc.).
    >
    > Mark R
    >
    >
     
    F Saunders, Oct 15, 2004
    #3
  4. Re: Mark can you help with;

    F Saunders wrote:

    > Have you been able to post to a https.asp with any success?
    >
    > I'd like to logon via httpConn.connect() to www.comsec.com.au but haven't
    > been able
    > to get past step 1 so far. Have tried the obvious .setPost(True),
    > .setInput (True) etc..
    >
    > The parameters to the .asp are LoginName, Password. Have tried the ?
    > loginName=33333&password=98763.
    >
    > Hope you can help,
    >
    > regards,
    > Michael.
    >
    >
    >
    >
    > "Mark Riordan" <> wrote in message
    > news:...
    >>
    >> "Chris" <anon> wrote in message news:416eec0e$...
    >> > I'm looking for a fast and simple way to fetch an https page using
    >> > URLConnection.

    >>
    >> Try something like:
    >>
    >> try {
    >> String sURL = "https://www.whatever.com";
    >> URL url = new URL(sURL);
    >> URLConnection httpConn = url.openConnection();
    >> httpConn.setDoInput(true);
    >> httpConn.connect();
    >> InputStream in = httpConn.getInputStream();
    >> BufferedInputStream bufIn = new BufferedInputStream(in);
    >> int nbytes;
    >> do {
    >> // Echo the response on the Java Console.
    >> // This is crude, and just for demo purposes.
    >> byte buf[] = new byte[8192];
    >> nbytes = bufIn.read(buf, 0, 8192);
    >> System.out.println(new String(buf,"US-ASCII"));
    >> } while(nbytes > 0);
    >>
    >> } catch (Exception e) {
    >> System.out.println("Exception: " + e.getMessage());
    >> }
    >>
    >> This ignores proxies (unless you are running
    >> in a browser), and issues with funny certificates (mismatched
    >> hostname on certificate, etc.).
    >>
    >> Mark R
    >>
    >>


    maybe this helps..

    public String posti(String u) throws IOException {
    System.setProperty("java.protocol.handler.pkgs",
    "com.sun.net.ssl.internal.www.protocol");
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()) ;

    StringBuffer sb = new StringBuffer();
    Enumeration e=params.keys();
    while(e.hasMoreElements()){
    String key=(String)e.nextElement();
    String val=(String)params.get(key);
    sb.append(key+"="+URLEncoder.encode(val)+"&");
    }

    String outputXml = "";
    HttpURLConnection conn = null;
    URL url = new URL(u);
    conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type","application
    x-www-form-urlencoded");
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    conn.connect();
    out.writeBytes(sb.toString());
    out.flush();
    out.close();
    DataInputStream in = new DataInputStream(conn.getInputStream());
    String line = "";
    while ((line = in.readLine()) != null) {
    outputXml += line;
    }
    in.close();
    conn.disconnect();
    return outputXml;
    }
    fabian
     
    Fabian Rossbacher, Oct 15, 2004
    #4
  5. Chris

    Mark Riordan Guest

    POST fields vs. query string parameters

    "F Saunders" <> wrote in message
    news:cknist$8pl$...
    > Have you been able to post to a https.asp with any success?
    >
    > I'd like to logon via httpConn.connect() to www.comsec.com.au but haven't
    > been able
    > to get past step 1 so far. Have tried the obvious .setPost(True),

    ..setInput
    > (True) etc..
    >
    > The parameters to the .asp are LoginName, Password. Have tried the ?
    > loginName=33333&password=98763.


    My simple example didn't POST any form fields.
    In your case, apparently the application does not accept those
    fields as query strings.
    So, as Fabian Rossbacher has pointed out, you need to transmit
    them as POSTed data. The data need to be encoded in a special format
    for the webserver to recognize them as POSTed form fields.
    Fabian's posting shows how to do that.

    Mark R
     
    Mark Riordan, Oct 15, 2004
    #5
    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. Kristoffel

    URLConnection and proxy

    Kristoffel, Jul 27, 2003, in forum: Java
    Replies:
    0
    Views:
    1,594
    Kristoffel
    Jul 27, 2003
  2. Brock Jones
    Replies:
    0
    Views:
    2,660
    Brock Jones
    Sep 4, 2003
  3. Bronwyn Howard
    Replies:
    0
    Views:
    6,145
    Bronwyn Howard
    Oct 1, 2003
  4. dave
    Replies:
    0
    Views:
    867
  5. Replies:
    2
    Views:
    3,723
Loading...

Share This Page