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(NameValuePair paramNameValuePair) throws IllegalArgumentException 

Source Link

Usage

From source file:com.francetelecom.admindm.changedustate.callviaacs.CallChangeDUStateUninstallCapabilityOnEdgeAcs.java

/**
 * @param args/* w w  w  .  j a  v a 2  s.  c om*/
 */
public static void main(final String[] args) {
    HttpClient client = new HttpClient();
    // client.getHostConfiguration().setProxy("", );

    String host = null;
    String port = null;
    String address = "http://" + host + ":" + port + "/edge/api/";

    String acsUsername = null;
    String acsPassword = null;

    if (host == null || port == null || acsUsername == null || acsPassword == null) {
        throw new InvalidParameterException("Fill host: " + host + ", port: " + port + ", acsUsername: "
                + acsUsername + ", and acsPassword: " + acsPassword + ".");
    }

    String realm = "NBBS_API_Realm";

    AuthScope authscope = new AuthScope(host, Integer.parseInt(port), realm);

    // client.getState().setCredentials(realm, host,
    // new UsernamePasswordCredentials(acsUsername, acsPassword));

    client.getState().setCredentials(authscope, new UsernamePasswordCredentials(acsUsername, acsPassword));

    PostMethod post = null;

    // -----
    // ----- Execution de la capability : changeDUStateUninstall
    // -----

    post = new PostMethod(address + "capability/execute");

    post.addParameter(new NameValuePair("deviceId", "10003"));
    post.addParameter(new NameValuePair("timeoutMs", "60000"));
    post.addParameter(new NameValuePair("capability", "\"changeDUStateUninstall\""));

    // UUID: string
    // Version: string
    // ExecutionEnvRef: String

    JSONObject object = new JSONObject();
    object.put("UUID", "45");
    object.put("Version", "2.2.0");
    object.put("ExecutionEnvRef", "ExecutionEnvRef_value");
    post.addParameter(new NameValuePair("input", object.toString()));

    post.setDoAuthentication(true);

    // post.addParameter(new NameValuePair("deviceId", "60001"));

    // -----
    // ----- Partie commune : Execution du post
    // -----

    try {
        int status = client.executeMethod(post);
        System.out.println("status: " + status);
        String resp = post.getResponseBodyAsString();
        System.out.println("resp: " + resp);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // release any connection resources used by the method
        post.releaseConnection();
    }
}

From source file:com.francetelecom.admindm.changedustate.callviaacs.CallChangeDUStateUpdateCapabilityOnEdgeAcs.java

/**
 * @param args/*from w w  w .  j  a  va  2s  . c om*/
 */
public static void main(final String[] args) {
    HttpClient client = new HttpClient();
    // client.getHostConfiguration().setProxy("", );

    String host = null;
    String port = null;
    String address = "http://" + host + ":" + port + "/edge/api/";

    String acsUsername = null;
    String acsPassword = null;

    if (host == null || port == null || acsUsername == null || acsPassword == null) {
        throw new InvalidParameterException("Fill host: " + host + ", port: " + port + ", acsUsername: "
                + acsUsername + ", and acsPassword: " + acsPassword + ".");
    }

    String realm = "NBBS_API_Realm";

    AuthScope authscope = new AuthScope(host, Integer.parseInt(port), realm);

    // client.getState().setCredentials(realm, host,
    // new UsernamePasswordCredentials(acsUsername, acsPassword));

    client.getState().setCredentials(authscope, new UsernamePasswordCredentials(acsUsername, acsPassword));

    PostMethod post = null;

    // -----
    // ----- Execution de la capability : changeDUStateUpdate
    // -----

    post = new PostMethod(address + "capability/execute");

    post.addParameter(new NameValuePair("deviceId", "10003"));
    post.addParameter(new NameValuePair("timeoutMs", "60000"));
    post.addParameter(new NameValuePair("capability", "\"changeDUStateUpdate\""));

    // UUID: string
    // Version: string
    // URL: string
    // Username: string
    // Password: string

    JSONObject object = new JSONObject();
    object.put("UUID", "45");
    object.put("Version", "1.0.0");
    object.put("URL", "http://127.0.0.1:8085/a/org.apache.felix.http.jetty-2.2.0.jar");
    object.put("Username", "Username_value");
    object.put("Password", "Password_value");
    post.addParameter(new NameValuePair("input", object.toString()));

    post.setDoAuthentication(true);

    // post.addParameter(new NameValuePair("deviceId", "60001"));

    // -----
    // ----- Partie commune : Execution du post
    // -----

    try {
        int status = client.executeMethod(post);
        System.out.println("status: " + status);
        String resp = post.getResponseBodyAsString();
        System.out.println("resp: " + resp);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // release any connection resources used by the method
        post.releaseConnection();
    }
}

From source file:com.francetelecom.admindm.changedustate.callviaacs.CallChangeDUStateInstallCapabilityOnEdgeAcs.java

/**
 * @param args/*from ww  w  . j a  v  a  2  s  .c o m*/
 */
public static void main(final String[] args) {
    HttpClient client = new HttpClient();
    // client.getHostConfiguration().setProxy("", );

    String host = null;
    String port = null;
    String address = "http://" + host + ":" + port + "/edge/api/";

    String acsUsername = null;
    String acsPassword = null;

    if (host == null || port == null || acsUsername == null || acsPassword == null) {
        throw new InvalidParameterException("Fill host: " + host + ", port: " + port + ", acsUsername: "
                + acsUsername + ", and acsPassword: " + acsPassword + ".");
    }

    String realm = "NBBS_API_Realm";

    AuthScope authscope = new AuthScope(host, Integer.parseInt(port), realm);

    // client.getState().setCredentials(realm, host,
    // new UsernamePasswordCredentials(acsUsername, acsPassword));

    client.getState().setCredentials(authscope, new UsernamePasswordCredentials(acsUsername, acsPassword));

    PostMethod post = null;

    // -----
    // ----- Execution de la capability : changeDUStateInstall
    // -----

    post = new PostMethod(address + "capability/execute");

    post.addParameter(new NameValuePair("deviceId", "10003"));
    post.addParameter(new NameValuePair("timeoutMs", "60000"));
    post.addParameter(new NameValuePair("capability", "\"changeDUStateInstall\""));

    // URL: string
    // UUID: string
    // Username: string
    // Password: string
    // ExecutionEnvRef: string

    JSONObject object = new JSONObject();
    object.put("URL", "http://127.0.0.1:8085/a/org.apache.felix.http.jetty-1.0.0.jar");
    // object.put("UUID", "UUID_value");
    object.put("Username", "Username_value");
    object.put("Password", "Password_value");
    object.put("ExecutionEnvRef", "ExecutionEnvRef_value");
    post.addParameter(new NameValuePair("input", object.toString()));

    post.setDoAuthentication(true);

    // post.addParameter(new NameValuePair("deviceId", "60001"));

    // -----
    // ----- Partie commune : Execution du post
    // -----

    try {
        int status = client.executeMethod(post);
        System.out.println("status: " + status);
        String resp = post.getResponseBodyAsString();
        System.out.println("resp: " + resp);

        // 10 avr. 2013 10:09:23
        // org.apache.commons.httpclient.auth.AuthChallengeProcessor
        // selectAuthScheme
        // INFO: basic authentication scheme selected
        // status: 200
        // resp: "executed: changeDUStateInstall: {Password=Password_value,
        // Username=Username_value, ExecutionEnvRef=ExecutionEnvRef_value,
        // URL=http:\/\/archive.apache.org\/dist\/felix\/org.apache.felix.http.jetty-1.0.0.jar},
        // resp: com.netopia.nbbs.tr69.msg.ChangeDUStateResponse@2db81edf"

    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // release any connection resources used by the method
        post.releaseConnection();
    }
}

From source file:cn.newtouch.util.HttpClientUtil.java

/**
 * /* ww w  .jav  a 2s  . c om*/
 * List ?
 * 
 * @since 2011-11-25
 * @param url
 * @param params
 *            ?
 * @return
 */
public static String post(String url, List<LabelValue> params) {
    HttpClient client = new HttpClient();
    PostMethod postMethod = new PostMethod(url);

    try {
        client.getParams().setContentCharset(ENCODING_UTF);// ?

        for (LabelValue temp : params) {
            postMethod.addParameter(new NameValuePair(temp.getValue(), temp.getValue()));// username?values
        }

        int tmpStatusCode = client.executeMethod(postMethod);

        // ?
        if (tmpStatusCode == HttpStatus.SC_OK) {
            return postMethod.getResponseBodyAsString();

        } else {
            return null;
        }
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        postMethod.releaseConnection();
    }
    return null;
}

From source file:lucee.commons.net.http.httpclient3.HTTPEngine3Impl.java

private static void setParams(HttpMethod httpMethod, Map<String, String> params) {
    if (params == null || !(httpMethod instanceof PostMethod))
        return;//  w w  w . ja  v a  2s.co m
    PostMethod post = (PostMethod) httpMethod;
    Iterator<Entry<String, String>> it = params.entrySet().iterator();
    Entry<String, String> e;
    while (it.hasNext()) {
        e = it.next();
        post.addParameter(new NameValuePair(e.getKey(), e.getValue()));
    }
}

From source file:it.drwolf.ridire.utility.test.SSLConnectionTest.java

public SSLConnectionTest() {
    Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 8443));
    this.httpClient = new HttpClient();
    // this.httpClient.getParams().setAuthenticationPreemptive(true);
    Credentials defaultcreds = new UsernamePasswordCredentials("admin", "admin");
    this.httpClient.getState().setCredentials(
            new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), defaultcreds);
    PostMethod method = new PostMethod("https://localhost:8443/engine");
    method.addParameter(new NameValuePair("action", "rescan"));
    try {/*w  w  w  . j  a va 2  s .co m*/
        int status = this.httpClient.executeMethod(method);
        Header redirectLocation = method.getResponseHeader("location");
        String loc = redirectLocation.getValue();
        GetMethod getmethod = new GetMethod("https://localhost:8443/engine");
        status = this.httpClient.executeMethod(getmethod);
        System.out.println(status);
    } catch (HttpException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        method.releaseConnection();
    }
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.externalServices.PhoneValidationUtils.java

public boolean sendSMS(String number, String token) {
    number = number.replace(" ", "");
    final String message = "Bem-vindo ao sistema Fenix. Introduza o codigo " + token + " . Obrigado!";
    if (canRun()) {
        PostMethod method = new PostMethod(CIIST_SMS_GATEWAY_URL);
        method.addParameter(new NameValuePair("number", number));
        method.addParameter(new NameValuePair("msg", message));
        try {/*from   w w  w . jav a2  s  .  c om*/
            CIIST_CLIENT.executeMethod(method);
            if (method.getStatusCode() != 200) {
                return false;
            }
        } catch (HttpException e) {
            // TODO Auto-generated catch block
            logger.error(e.getMessage(), e);
            return false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            logger.error(e.getMessage(), e);
            return false;
        }
    } else {
        logger.info("SMS to >" + number + "<: " + message);
    }
    return true;
}

From source file:com.ebay.jetstream.application.dataflows.VisualDataFlow.java

private URL getYumlPic(String yumlData) {
    try {/*  ww w  . j  a  v a2 s. co  m*/
        if (!graphYuml.containsKey(yumlData)) {
            HttpClient httpclient = new HttpClient();
            PostMethod post = new PostMethod(yumlActivityUrl);
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            post.addParameter(new NameValuePair("dsl_text", yumlData));
            httpclient.executeMethod(post);
            URL url = new URL(yumlUrl + post.getResponseBodyAsString());
            graphYuml.put(yumlData, url);
            return url;
        } else {
            return graphYuml.get(yumlData);
        }
    } catch (Exception e) {
        logger.error(" Exception while rendering pipeline ", e);
        return null;
    }
}

From source file:com.zb.app.biz.service.WeixinTest.java

public void login() {
    httpClient = new HttpClient();
    PostMethod post = new PostMethod(loginUrl);
    post.addParameter(new NameValuePair("username", account));
    post.addParameter(new NameValuePair("pwd", DigestUtils.md5Hex(password)));
    post.addParameter(new NameValuePair("imgcode", ""));
    post.addParameter(new NameValuePair("f", "json"));
    post.setRequestHeader("Host", "mp.weixin.qq.com");
    post.setRequestHeader("Referer", "https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN");

    try {//w  w w  .j  av  a2s .  co  m
        int code = httpClient.executeMethod(post);
        if (HttpStatus.SC_OK == code) {
            String res = post.getResponseBodyAsString();
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject) parser.parse(res);
            JSONObject _obj = (JSONObject) obj.get("base_resp");
            @SuppressWarnings("unused")
            String msg = (String) _obj.get("err_msg");
            String redirect_url = (String) obj.get("redirect_url");
            Long errCode = (Long) _obj.get("ret");
            if (0 == errCode) {
                isLogin = true;
                token = StringUtils.substringAfter(redirect_url, "token=");
                if (null == token) {
                    token = StringUtils.substringBetween(redirect_url, "token=", "&");
                }
                StringBuffer cookie = new StringBuffer();
                for (Cookie c : httpClient.getState().getCookies()) {
                    cookie.append(c.getName()).append("=").append(c.getValue()).append(";");
                }
                this.cookiestr = cookie.toString();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.pureinfo.force.net.impl.HttpUtilImpl.java

/**
 * @see com.pureinfo.force.net.IHttpUtil#getContent(String, NameValuePair[],
 *      String, String)//from w w w  .  ja va 2 s  .c  o m
 */
public String getContent(String _sUrl, NameValuePair[] _args, String _sRequestCharset, String _sResponseCharset)
        throws IOTransferException {
    if (_sRequestCharset == null) {
        _sRequestCharset = "utf-8";
    }

    //1. to create http client and set proxy
    HttpClient client = new HttpClient();
    if (m_proxyHost != null) {
        client.getHostConfiguration().setProxyHost(m_proxyHost);
        if (m_sProxyUser != null & m_sProxyPassword != null) {
            client.getState().setProxyCredentials(//
                    new AuthScope(m_proxyHost.getHostName(), m_proxyHost.getPort()),
                    new UsernamePasswordCredentials(m_sProxyUser, m_sProxyPassword));
        }
    }

    //2. to create post
    PostMethod method = new PostMethod(_sUrl);
    if (m_nRetryCount > 0) {
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, //
                new DefaultHttpMethodRetryHandler(m_nRetryCount, false));
    }
    method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=" + _sRequestCharset);
    for (int i = 0; _args != null && i < _args.length; i++) {
        method.addParameter(_args[i]);
    }

    //3. to send request and read response
    String sResponse;
    try {
        client.executeMethod(method);
        sResponse = method.getResponseBodyAsString();
        if (!method.getRequestCharSet().equals(_sRequestCharset)) {
            sResponse = new String(sResponse.getBytes(), _sResponseCharset);
        }
        return sResponse;
    } catch (Exception ex) {
        throw new IOTransferException(_sUrl, ex);
    } finally {
        method.releaseConnection();
    }
}