You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Greg Dunn <gr...@nisc.cc> on 2003/03/20 19:14:06 UTC

RE: [HttpClient] sample code for form post

I need to post a set of form values to an authorization service.  I couldn't
locate any example code that does this, does anyone know where I could find
some?

My initial attempts have been thwarted by the server's returning an HTTP
100 - Continue response.

This is the relevant part of what I have via trial and much error:


try {
    HttpClient client = new HttpClient();
    client.setConnectionTimeout(5000);

    PostMethod post = new PostMethod(paymentServerURL);
    post.setFollowRedirects(false);
    post.setStrictMode(true);
    post.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");

    // Add the form values
    NameValuePair[] data = {
        new NameValuePair("x_First_Name", x_First_Name),
        new NameValuePair("x_Last_Name", x_Last_Name),
        new NameValuePair("x_Card_Num", x_Card_Num),
        new NameValuePair("x_Exp_Date", x_Exp_Date)
    };

    post.setRequestBody(data);
    client.executeMethod(post);

    try {
        responseContent = post.getResponseBodyAsString();
    } catch (NullPointerException e) {
        // do graceful stuff
    }

    responseCode = post.getStatusCode();
    responseMess = post.getStatusText();

    if (responseCode != 200) {
        // do graceful stuff
    }

    post.releaseConnection();

} catch (MoreExceptions me) {
    // do graceful stuff
}


Greg