Example usage for org.apache.commons.httpclient.methods PostMethod addParameter

List of usage examples for org.apache.commons.httpclient.methods PostMethod addParameter

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PostMethod addParameter.

Prototype

public void addParameter(String paramString1, String paramString2) throws IllegalArgumentException 

Source Link

Usage

From source file:org.apache.wookie.tests.functional.ProxyTest.java

@Test
public void postWithMixedQueryAndParameters() throws Exception {
    HttpClient client = new HttpClient();
    List<String> authPrefs = new ArrayList<String>(2);
    authPrefs.add(AuthPolicy.DIGEST);/*from w ww  .ja  v  a 2 s. c  o m*/
    authPrefs.add(AuthPolicy.BASIC);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    // send the basic authentication response even before the server gives an unauthorized response
    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials("java", "java"));
    PostMethod req;
    req = new PostMethod(PROXY_URL + "?instanceid_key=" + instance_id_key);
    req.addParameter("url", PROTECTED_SITE_URL);
    client.executeMethod(req);
    int code = req.getStatusCode();
    req.releaseConnection();
    assertEquals(200, code);
}

From source file:org.apache.wookie.tests.functional.ProxyTest.java

/**
 * This tests accessing a site passing through POST parameters
 * @throws Exception//w w w  .j  a  va2 s.  c om
 */
@Test
public void postWithPassingParameters() throws Exception {
    try {
        String url = PROXY_URL;
        //String url = TEST_PROPERTIES_SERVICE_URL_VALID;
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        post.addParameter("instanceid_key", instance_id_key);
        post.addParameter("url", TEST_PROPERTIES_SERVICE_URL_VALID);
        post.addParameter("api_key", API_KEY_VALID);
        post.addParameter("widgetid", WIDGET_ID_VALID);
        post.addParameter("userid", "test");
        post.addParameter("is_public", "false");
        post.addParameter("shareddatakey", "proxytest");
        post.addParameter("propertyname", "pass");
        post.addParameter("propertyvalue", "pass");
        client.executeMethod(post);
        int code = post.getStatusCode();
        assertEquals(200, code);
        post.releaseConnection();
    } catch (Exception e) {
        fail("POST via proxy failed");
    }
    try {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
        get.setQueryString("api_key=" + API_KEY_VALID + "&widgetid=" + WIDGET_ID_VALID
                + "&userid=test&shareddatakey=proxytest&propertyname=pass");
        client.executeMethod(get);
        int code = get.getStatusCode();
        assertEquals(200, code);
        String resp = get.getResponseBodyAsString();
        assertEquals("pass", resp);
        get.releaseConnection();
    } catch (Exception e) {
        fail("POST via proxy failed to set info correctly");
    }
}

From source file:org.apache.wookie.tests.functional.ProxyTest.java

@Test
public void postWithOnlyParameters() throws Exception {
    HttpClient client = new HttpClient();
    List<String> authPrefs = new ArrayList<String>(2);
    authPrefs.add(AuthPolicy.DIGEST);/*  w  w w . j  av  a2 s.  co  m*/
    authPrefs.add(AuthPolicy.BASIC);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    // send the basic authentication response even before the server gives an unauthorized response
    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials("java", "java"));
    PostMethod req;
    req = new PostMethod(PROXY_URL);
    req.addParameter("url", PROTECTED_SITE_URL);
    req.addParameter("instanceid_key", instance_id_key);
    client.executeMethod(req);
    int code = req.getStatusCode();
    req.releaseConnection();
    assertEquals(200, code);
}

From source file:org.apache.wookie.tests.functional.WidgetAccessRequestPolicyControllerTest.java

@BeforeClass
public static void setup() {
    try {/*from   w  ww.j  a va  2 s  . c o  m*/
        HttpClient client = new HttpClient();
        setAuthenticationCredentials(client);
        PostMethod post = new PostMethod(TEST_WARP_SERVICE_URL_VALID);
        post.addParameter("widgetId", "1");
        post.addParameter("subdomains", "true");
        post.addParameter("origin", "http://www.9128.org");
        post.setDoAuthentication(true);
        client.executeMethod(post);
        int code = post.getStatusCode();
        assertEquals(201, code);
        post.releaseConnection();
    } catch (Exception e) {
        e.printStackTrace();
        fail("post failed");
    }
    // Now lets GET it to make sure it was added OK
    Element[] policies = getPolicies();
    for (Element policy : policies) {
        if (policy.getAttribute("origin").getValue().equals("http://www.9128.org")) {
            id = policy.getAttributeValue("id");
        }
    }
}

From source file:org.attribyte.api.http.impl.commons.Commons3Client.java

@Override
public Response send(Request request, RequestOptions options) throws IOException {

    HttpMethod method = null;//from   w  w  w.j ava  2s . c o m

    switch (request.getMethod()) {
    case GET:
        method = new GetMethod(request.getURI().toString());
        method.setFollowRedirects(options.followRedirects);
        break;
    case DELETE:
        method = new DeleteMethod(request.getURI().toString());
        break;
    case HEAD:
        method = new HeadMethod(request.getURI().toString());
        method.setFollowRedirects(options.followRedirects);
        break;
    case POST:
        method = new PostMethod(request.getURI().toString());
        if (request.getBody() != null) {
            ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(request.getBody().toByteArray());
            ((EntityEnclosingMethod) method).setRequestEntity(requestEntity);
        } else {
            PostMethod postMethod = (PostMethod) method;
            Collection<Parameter> parameters = request.getParameters();
            for (Parameter parameter : parameters) {
                String[] values = parameter.getValues();
                for (String value : values) {
                    postMethod.addParameter(parameter.getName(), value);
                }
            }
        }
        break;
    case PUT:
        method = new PutMethod(request.getURI().toString());
        if (request.getBody() != null) {
            ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(request.getBody().toByteArray());
            ((EntityEnclosingMethod) method).setRequestEntity(requestEntity);
        }
        break;
    }

    if (userAgent != null && Strings.isNullOrEmpty(request.getHeaderValue("User-Agent"))) {
        method.setRequestHeader("User-Agent", userAgent);
    }

    Collection<Header> headers = request.getHeaders();
    for (Header header : headers) {
        String[] values = header.getValues();
        for (String value : values) {
            method.setRequestHeader(header.getName(), value);
        }
    }

    int responseCode;
    InputStream is = null;

    try {
        responseCode = httpClient.executeMethod(method);
        is = method.getResponseBodyAsStream();
        if (is != null) {
            byte[] body = Request.bodyFromInputStream(is, options.maxResponseBytes);
            ResponseBuilder builder = new ResponseBuilder();
            builder.setStatusCode(responseCode);
            builder.addHeaders(getMap(method.getResponseHeaders()));
            return builder.setBody(body.length != 0 ? body : null).create();
        } else {
            ResponseBuilder builder = new ResponseBuilder();
            builder.setStatusCode(responseCode);
            builder.addHeaders(getMap(method.getResponseHeaders()));
            return builder.create();
        }

    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ioe) {
                //Ignore
            }
        }
        method.releaseConnection();
    }
}

From source file:org.bsc.confluence.xmlrpc.ConfluenceExportDecorator.java

/**
 * //  w w  w  .  j a  v  a 2  s  .  c  om
 * @param client
 * @param redirectUrl
 * @throws Exception 
 */
private void login(HttpClient client, String redirectUrl, RedirectTask task) {

    PostMethod post = null;
    try {
        final String login = String.format("%s/%s", url, "login.action");

        post = new PostMethod(login);

        post.setDoAuthentication(true);
        post.addParameter("os_username", confluence.getCredentials().username);
        post.addParameter("os_password", confluence.getCredentials().password);
        post.addParameter("os_destination", redirectUrl);
        post.addParameter("login", "Log+In");

        int statusCode = client.executeMethod(post);
        if (statusCode != HttpStatus.SC_OK && statusCode != SC_MOVED_TEMPORARILY) {
            throw new HttpException(
                    String.format("Execute ethod failed: [%s]", String.valueOf(post.getStatusLine())));
        }

        String redirectLocation;
        Header locationHeader = post.getResponseHeader("Location");
        if (locationHeader != null) {

            redirectLocation = locationHeader.getValue();

            task.exec(redirectLocation);

        } else {

            throw new HttpException(
                    String.format("no redirect to url found\n[%s]", new String(post.getResponseBody())));
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {

        if (post != null) {
            post.releaseConnection();
        }
    }

}

From source file:org.cloudata.core.rest.CRestTable.java

public String createTable(String tableName, String description, String[] columnNames) throws IOException {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(selectRestServer() + "/table/" + tableName);

    method.addParameter("data",
            CloudataRestService.getTableSchemaXml(new TableSchema(tableName, description, columnNames)));

    int result = client.executeMethod(method);
    if (result != 201) {
        throw new IOException(method.getResponseBodyAsString());
    }//from   w w w  . ja  va 2  s  .co m
    return method.getResponseBodyAsString();
}

From source file:org.cloudata.core.rest.CRestTable.java

public String putRow(String tableName, Row row) throws IOException {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(selectRestServer() + "/" + tableName);

    method.addParameter("data", CloudataRestService.getRowXml(new Row[] { row }));

    int result = client.executeMethod(method);
    if (result != 201) {
        throw new IOException(method.getResponseBodyAsString());
    }/*from  w w  w .jav  a 2 s .  c  o  m*/
    return method.getResponseBodyAsString();
}

From source file:org.codehaus.swizzle.confluence.ConfluenceExportDecorator.java

/**
 * /*from w ww.  j  a va  2 s  .co  m*/
 * @param client
 * @param redirectUrl
 * @throws Exception 
 */
private void login(HttpClient client, String redirectUrl, RedirectTask task) throws Exception {

    PostMethod post = null;
    try {
        final String login = String.format("%s/%s", url, "login.action");

        post = new PostMethod(login);

        post.setDoAuthentication(true);
        post.addParameter("os_username", username);
        post.addParameter("os_password", password);
        post.addParameter("os_destination", redirectUrl);
        post.addParameter("login", "Log+In");

        int statusCode = client.executeMethod(post);
        if (statusCode != HttpStatus.SC_OK && statusCode != SC_MOVED_TEMPORARILY) {
            throw new HttpException(
                    String.format("Execute ethod failed: [%s]", String.valueOf(post.getStatusLine())));
        }

        String redirectLocation;
        Header locationHeader = post.getResponseHeader("Location");
        if (locationHeader != null) {

            redirectLocation = locationHeader.getValue();

            task.exec(redirectLocation);

        } else {

            throw new HttpException(
                    String.format("no redirect to url found\n[%s]", new String(post.getResponseBody())));
        }
    } finally {

        if (post != null) {
            post.releaseConnection();
        }
    }

}

From source file:org.colombbus.tangara.net.TConnection.java

/**
 * Create the HTTP method from the command
 *
 * @param cmd//ww w .j  a v  a 2  s .co m
 * @return HttpMethod
 */
private HttpMethod createMethod(Command cmd) {
    // url args
    String url = buildURL(cmd);

    // method choice
    HttpMethod method = null;
    if (cmd.getBodyArgs().isEmpty()) {
        method = new GetMethod(url);
    } else {
        PostMethod postMethod = new PostMethod(url);
        // body args
        for (String argName : cmd.getBodyArgs().keySet()) {
            String value = cmd.getBodyArgs().get(argName);
            postMethod.addParameter(argName, value);
        }
        method = postMethod;
    }

    return method;
}