URLConnection

Discussion in 'Java' started by rplank@gmail.com, Oct 18, 2006.

  1. Guest

    I am new to URL Connections so please forgive me if this is a stupid
    question.

    I am writing an Frame app that I want to use as a Client interface to a
    web site that is writen in php, I am trying to figure out how to pass
    the user name and password to this web site. if someone could point me
    in the right driection that would be great.

    Rob
    , Oct 18, 2006
    #1
    1. Advertising

  2. Hi,

    You need to find out the login form fields for the site, as well as the
    URL to post this form to. You also need to check if the site requires
    cookie handling at the client. To start with, you can use the example
    provided here:

    http://martin.nobilitas.com/java/cookies.html

    It has an example of a form post and cookie-handling.

    -cheers,
    Manish
    Manish Pandit, Oct 18, 2006
    #2
    1. Advertising

  3. Guest

    This was helpfull, I have an idea how to do it now but I can't seem to
    figure out how to specify the web page with out getting "unreported
    exception java.net.MalformedURLException; must be caught or declared to
    be thrown" error

    if i remove the URL line the program complies with out error

    error -> URL url = new URL("http://www.kingsofchaos.com");
    String user="", email="", pass="",body1="";
    user = UserText.getText();
    email = EmailText.getText();
    pass = PasswordText.getText();

    body1 = ("field1="+user+"&field2="+email+"&field3="+pass+"");
    outputTA.setText(body1);
    ~~~~~~~going to call function~~~~~

    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
    }




    Rob

    Manish Pandit wrote:

    > Hi,
    >
    > You need to find out the login form fields for the site, as well as the
    > URL to post this form to. You also need to check if the site requires
    > cookie handling at the client. To start with, you can use the example
    > provided here:
    >
    > http://martin.nobilitas.com/java/cookies.html
    >
    > It has an example of a form post and cookie-handling.
    >
    > -cheers,
    > Manish
    , Oct 19, 2006
    #3
  4. wrote:
    > This was helpfull,


    Please refrain from top-posting. It makes threads very confusing.

    >..I have an idea how to do it now but I can't seem to
    > figure out how to specify the web page with out getting "unreported
    > exception java.net.MalformedURLException; must be caught or declared to
    > be thrown" error


    comp.lang.java.help is a good group for beginners.

    > if i remove the URL line the program complies with out error


    Or, if you accept either of the options specified.. (cont. *)

    2) 'declared'

    public URL getURL(String path) throws MalformedURLException {
    // the code that calls for a new URL
    return new URL(path);
    }

    ...but as soon as you go to call that method, you will end
    with the same error from the code that *calls* it, which leads
    to option ..1.
    1) 'caught'

    public URL getURL(String path) {
    URL url = null;
    try {
    // the code that calls for a new URL
    url = new URL(path);
    } catch(MalformedURLException murle) { //exception caught!
    murle.printStackTrace();
    }
    return url;
    }

    * ..the program will compile just fine (so long as
    nothing else is wrong with the code).

    Andrew T.
    Andrew Thompson, Oct 19, 2006
    #4
    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,562
    Kristoffel
    Jul 27, 2003
  2. Brock Jones
    Replies:
    0
    Views:
    2,622
    Brock Jones
    Sep 4, 2003
  3. Bronwyn Howard
    Replies:
    0
    Views:
    6,088
    Bronwyn Howard
    Oct 1, 2003
  4. dave
    Replies:
    0
    Views:
    829
  5. Replies:
    2
    Views:
    3,566
Loading...

Share This Page