POST data with Java

Discussion in 'Java' started by adrian.bartholomew@gmail.com, Aug 1, 2008.

  1. Guest

    Hi. I'm trying to fill out online forms automatically with Java.
    Things like posting to classified ads that would include signing in
    and uploading pics etc. without having to go to the sites themselves.
    I understand that I may have to obtain the POST variables.
    Can anyone help me or point me in the right direction?

    Thanks.
     
    , Aug 1, 2008
    #1
    1. Advertising

  2. Roedy Green Guest

    On Fri, 1 Aug 2008 14:37:46 -0700 (PDT),
    wrote, quoted or indirectly quoted someone who said :

    >Hi. I'm trying to fill out online forms automatically with Java.
    >Things like posting to classified ads that would include signing in
    >and uploading pics etc. without having to go to the sites themselves.
    >I understand that I may have to obtain the POST variables.
    >Can anyone help me or point me in the right direction?


    See http://mindprod.com/jgloss/http.html for code listings. You can
    also download. For a app that posts see
    http://mindprod.com/applet/submitter.html
    with source.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Aug 1, 2008
    #2
    1. Advertising

  3. Arne Vajhøj Guest

    wrote:
    > Hi. I'm trying to fill out online forms automatically with Java.
    > Things like posting to classified ads that would include signing in
    > and uploading pics etc. without having to go to the sites themselves.
    > I understand that I may have to obtain the POST variables.
    > Can anyone help me or point me in the right direction?


    You can use (Http)URLConnection, but I will strongly
    recommend something a bit more high level like Jakarta
    HttpClient.

    See a code snippet below.

    Arne

    =============================================

    import java.io.IOException;

    import org.apache.commons.httpclient.Cookie;
    import org.apache.commons.httpclient.Header;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;

    public class Login {
    private HttpClient client;

    public Login() {
    client = new HttpClient();
    }

    public void login(String url,
    String userField, String userValue,
    String passField, String passValue) {
    NameValuePair[] nvp = new NameValuePair[2];
    nvp[0] = new NameValuePair(userField, userValue);
    nvp[1] = new NameValuePair(passField, passValue);
    post(url, nvp);
    }
    public String get(String url) {
    GetMethod met = new GetMethod(url);
    try {
    client.executeMethod(met);
    } catch (HttpException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return met.getResponseBodyAsString();
    }
    public String post(String url, NameValuePair[] nvp) {
    PostMethod met = new PostMethod(url);
    if(nvp != null) {
    met.setRequestBody(nvp);
    }
    try {
    client.executeMethod(met);
    } catch (HttpException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return met.getResponseBodyAsString();
    }
    public static void main(String[] args) {
    Login lgi = new Login();
    lgi.login("http://arne:8080/useradmin/Login",
    "username", args[0],
    "password", args[1]);

    System.out.println(lgi.get("http://arne:8080/useradmin/UserAdmin.jsp"));
    }
    }
     
    Arne Vajhøj, Aug 1, 2008
    #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. Vishal
    Replies:
    1
    Views:
    701
    Steve C. Orr [MVP, MCSD]
    Dec 21, 2004
  2. =?Utf-8?B?Z29yaWxsYQ==?=
    Replies:
    1
    Views:
    808
    Masudur
    May 25, 2007
  3. Hal Vaughan
    Replies:
    4
    Views:
    790
    Hal Vaughan
    Jan 14, 2008
  4. JT

    post data, then post again..

    JT, Sep 5, 2003, in forum: ASP General
    Replies:
    1
    Views:
    157
    Bite My Bubbles
    Sep 6, 2003
  5. J. Muenchbourg
    Replies:
    0
    Views:
    394
    J. Muenchbourg
    Aug 6, 2003
Loading...

Share This Page