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:hydrograph.server.service.HydrographServiceClient.java

public void calltoDeleteLocalDebugService() throws IOException {

    HttpClient httpClient = new HttpClient();

    PostMethod postMethod = new PostMethod("http://" + HOST_NAME + ":" + PORT + "/deleteLocalDebugFile");
    postMethod.addParameter("jobId", JOB_ID);
    postMethod.addParameter("componentId", COMPONENT_ID);
    postMethod.addParameter("socketId", SOCKET_ID);

    java.util.Date date = new java.util.Date();
    System.out.println("+++ Start: " + new Timestamp(date.getTime()));

    int response = httpClient.executeMethod(postMethod);
    System.out.println("response: " + response);
}

From source file:de.linsin.alterego.notification.AppNotificationService.java

PostMethod setUp(String argTitle, String argMessage) {
    PostMethod method = new PostMethod(URL);
    method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

    method.addParameter(USER_CREDENTIALS, credentials);
    method.addParameter(NOTIFICATION_LONG_MESSAGE, argMessage);
    method.addParameter(NOTIFICATION_TITLE, argTitle);
    method.addParameter(MESSAGE_LEVEL, "2");
    return method;
}

From source file:com.cloud.utils.rest.RESTValidationStrategy.java

/**
 * Logs against the REST server. The cookie is stored in the <code>_authcookie<code> variable.
 * <p>//from w  ww .  j  a v a2 s  . c o  m
 * The method returns false if the login failed or the connection could not be made.
 *
 */
protected void login(final String protocol, final HttpClient client) throws CloudstackRESTException {
    String url;

    if (host == null || host.isEmpty() || user == null || user.isEmpty() || password == null
            || password.isEmpty()) {
        throw new CloudstackRESTException("Hostname/credentials are null or empty");
    }

    try {
        url = new URL(protocol, host, loginUrl).toString();
    } catch (final MalformedURLException e) {
        s_logger.error("Unable to build Nicira API URL", e);
        throw new CloudstackRESTException("Unable to build Nicira API URL", e);
    }

    final PostMethod pm = new PostMethod(url);
    pm.addParameter("username", user);
    pm.addParameter("password", password);

    try {
        client.executeMethod(pm);
    } catch (final HttpException e) {
        throw new CloudstackRESTException("REST Service API login failed ", e);
    } catch (final IOException e) {
        throw new CloudstackRESTException("REST Service API login failed ", e);
    } finally {
        pm.releaseConnection();
    }

    if (pm.getStatusCode() != HttpStatus.SC_OK) {
        s_logger.error("REST Service API login failed : " + pm.getStatusText());
        throw new CloudstackRESTException("REST Service API login failed " + pm.getStatusText());
    }

    // Extract the version for later use
    if (pm.getResponseHeader("Server") != null) {
        serverVersion = pm.getResponseHeader("Server").getValue();
        s_logger.debug("Server reports version " + serverVersion);
    }

    // Success; the cookie required for login is kept in _client
}

From source file:net.navasoft.madcoin.backend.services.push.AndroidPushNotificationConfiguration.java

/**
 * Creates the service.//ww w  .ja  v  a  2 s.c om
 * 
 * @return the object
 * @throws PushNotificationException
 *             the push notification exception
 * @since 27/07/2014, 06:48:42 PM
 */
@Override
public Object createService() throws PushNotificationException {
    try {
        service = new HttpClient();
        PostMethod method = new PostMethod("https://www.google.com/accounts/ClientLogin");
        method.addParameter("Email", getUsername());
        method.addParameter("Passwd", getPassword());
        method.addParameter("accountType", "HOSTED_OR_GOOGLE");
        method.addParameter("source", "unit-test");
        method.addParameter("service", "ac2dm");
        service.executeMethod(method);
        byte[] responseBody = method.getResponseBody();
        String response = new String(responseBody);
        String auth = response.split("\n")[2];
        token = auth.split("=")[1];
        return service;
    } catch (HttpException e) {
        e.printStackTrace();
        throw new PushNotificationException("HTTP ", e);
    } catch (IOException e) {
        e.printStackTrace();
        throw new PushNotificationException("IO", e);
    }

}

From source file:de.mpg.escidoc.http.Login.java

private String passLogin(Cookie securityCookie) {
    String userHandle = null;//from ww w .j  a  v a 2s  .c o m
    PostMethod postMethod = new PostMethod(this.FRAMEWORK_URL + "/aa/login");
    postMethod.addParameter("target", this.FRAMEWORK_URL);
    client.getState().addCookie(securityCookie);
    try {
        client.executeMethod(postMethod);

    } catch (HttpException e) {
        System.out.println("HttpException in login POST request");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("IOException in login POST request");
        e.printStackTrace();
    }
    if (postMethod.getStatusCode() != 303) {
        System.out.println("StatusCode: " + postMethod.getStatusCode());
        return userHandle;
    }
    try {
        InputStream is = postMethod.getResponseBodyAsStream();
        System.out.println(Util.inputStreamToString(is));
    } catch (IOException e) {
        e.printStackTrace();
    }
    Header headers[] = postMethod.getResponseHeaders();
    for (int i = 0; i < headers.length; ++i) {
        System.out.println(headers[i]);
        if ("Location".equals(headers[i].getName())) {
            String location = headers[i].getValue();
            int index = location.indexOf('=');
            userHandle = new String(Base64.decode(location.substring(index + 1, location.length())));
            // System.out.println("location: " + location);
            System.out.println("UserHandle: " + userHandle);
        }
    }
    System.out.println(userHandle);
    return userHandle;
}

From source file:com.unicauca.braim.http.HttpBraimClient.java

public Session POST_Session(String user_id, String access_token) throws IOException {
    Session session = null;/*  ww  w. ja v  a  2s . co  m*/
    Gson gson = new Gson();

    String data = "?session[user_id]=" + user_id;
    String token_data = "&braim_token=" + access_token;

    PostMethod method = new PostMethod(api_url + "/api/v1/sessions");
    method.addParameter("session[user_id]", user_id);
    method.addParameter("braim_token", access_token);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    int statusCode = client.executeMethod(method);
    if (statusCode != HttpStatus.SC_CREATED) {
        System.err.println("Method failed: " + method.getStatusLine());
        throw new IOException("The session was not being created");
    }

    byte[] responseBody = method.getResponseBody();
    String response = new String(responseBody, "UTF-8");
    session = gson.fromJson(response, Session.class);
    System.out.println("new session of" + session.getUser_id() + "was created");

    return session;
}

From source file:de.mpg.escidoc.http.Login.java

private Cookie passSecurityCheck() {
    String loginName = Util.input("Enter Username: ");
    String loginPassword = Util.input("Enter Password: ");
    int delim1 = this.FRAMEWORK_URL.indexOf("//");
    int delim2 = this.FRAMEWORK_URL.indexOf(":", delim1);
    String host;/*from   w w  w  .  ja  va2  s  .c o m*/
    int port;
    if (delim2 > 0) {
        host = this.FRAMEWORK_URL.substring(delim1 + 2, delim2);
        port = Integer.parseInt(this.FRAMEWORK_URL.substring(delim2 + 1));
    } else {
        host = this.FRAMEWORK_URL.substring(delim1 + 2);
        port = 80;
    }

    PostMethod post = new PostMethod(this.FRAMEWORK_URL + "/aa/j_spring_security_check");
    post.addParameter("j_username", loginName);
    post.addParameter("j_password", loginPassword);
    try {
        this.client.executeMethod(post);
    } catch (HttpException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    post.releaseConnection();
    CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
    Cookie[] logoncookies = cookiespec.match(host, port, "/", false, this.client.getState().getCookies());
    Cookie sessionCookie = logoncookies[0];
    return sessionCookie;
}

From source file:com.liferay.mail.util.FuseMailHook.java

protected PostMethod getPostMethod() {
    PostMethod post = new PostMethod(_URL);

    post.addParameter("PlatformUser", _USERNAME);
    post.addParameter("PlatformPassword", _PASSWORD);

    return post;//from  w ww.  j  av a  2  s  .  c om
}

From source file:com.salas.bbservice.service.forum.ForumHandler.java

/**
 * Posts a new topic to the forum./*w ww .j a v a  2 s.c o m*/
 *
 * @param aFullName full name of the author.
 * @param aEmail    optional email address of the author.
 * @param aForumId  ID of the forum to post topic to.
 * @param aSubject  subject of the post.
 * @param aMessage  message of the post.
 */
private void post(String aFullName, String aEmail, int aForumId, String aSubject, String aMessage)
        throws IOException {
    PostMethod post = new PostMethod(forumBaseURL + "/post.php?action=post&fid=" + aForumId);
    post.addParameter("form_sent", "1");
    post.addParameter("form_user", "Guest");
    post.addParameter("req_username", "[BB] " + aFullName.trim());
    post.addParameter("email", StringUtils.isEmpty(aEmail) ? "" : aEmail.trim());
    post.addParameter("req_subject", aSubject.trim());
    post.addParameter("req_message", aMessage.trim());

    HttpClient client = new HttpClient();
    int status = client.executeMethod(post);

    if (status >= 400)
        throw new IOException("Failed to publish topic.");
}

From source file:fr.msch.wissl.server.TestLogin.java

@Test
public void test() throws IOException, JSONException {
    HttpClient client = new HttpClient();

    // good username, bad password: error 401
    PostMethod post = new PostMethod(TServer.URL + "login");
    post.addParameter("username", admin_username);
    post.addParameter("password", "bad_password");
    client.executeMethod(post);/*from  www  .j a v  a 2s  . c o  m*/
    post.getResponseBodyAsString();
    Assert.assertEquals(401, post.getStatusCode());

    // empty password and username: error 401
    post = new PostMethod(TServer.URL + "login");
    post.addParameter("username", "");
    post.addParameter("password", "");
    client.executeMethod(post);
    post.getResponseBodyAsString();
    Assert.assertEquals(401, post.getStatusCode());

    // log in as admin
    post = new PostMethod(TServer.URL + "login");
    post.addParameter("username", admin_username);
    post.addParameter("password", admin_password);
    client.executeMethod(post);

    String ret = post.getResponseBodyAsString();
    JSONObject obj = new JSONObject(ret);
    int uid_admin = obj.getInt("userId");
    String sid_admin = obj.getString("sessionId");
    int auth = obj.getInt("auth");

    Assert.assertEquals(200, post.getStatusCode());
    Assert.assertEquals(uid_admin, this.admin_userId);
    Assert.assertNotNull(UUID.fromString(sid_admin));
    Assert.assertEquals(auth, 1);

    // call 'stats' with this session, should succeed
    GetMethod get = new GetMethod(TServer.URL + "stats");
    get.addRequestHeader("sessionId", sid_admin);
    client.executeMethod(get);
    Assert.assertEquals(200, get.getStatusCode());

    // the previous session that was setup by the test case 
    // for this user should NOT have been destroyed
    // both sessions are kept for the same user
    get = new GetMethod(TServer.URL + "stats");
    get.addRequestHeader("sessionId", this.admin_sessionId);
    client.executeMethod(get);
    Assert.assertEquals(200, get.getStatusCode());

    // the other user set up by the test case should still be logged in
    get = new GetMethod(TServer.URL + "stats");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    Assert.assertEquals(200, get.getStatusCode());

    // logout all users
    post = new PostMethod(TServer.URL + "logout");
    post.addRequestHeader("sessionId", sid_admin);
    client.executeMethod(post);
    Assert.assertEquals(204, post.getStatusCode());

    post = new PostMethod(TServer.URL + "logout");
    post.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(post);
    Assert.assertEquals(204, post.getStatusCode());

    post = new PostMethod(TServer.URL + "logout");
    post.addRequestHeader("sessionId", this.admin_sessionId);
    client.executeMethod(post);
    Assert.assertEquals(204, post.getStatusCode());

    // check that neither client can call 'stats'
    get = new GetMethod(TServer.URL + "stats");
    get.addRequestHeader("sessionId", sid_admin);
    client.executeMethod(get);
    Assert.assertEquals(401, get.getStatusCode());

    get = new GetMethod(TServer.URL + "stats");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    Assert.assertEquals(401, get.getStatusCode());
}