Java Code Examples for org.apache.commons.httpclient.methods.PostMethod#getResponseBody()

The following examples show how to use org.apache.commons.httpclient.methods.PostMethod#getResponseBody() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OAuth2Client.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
private String sendHttpPost(PostMethod httppost) throws HttpException, IOException, JSONException {
	HttpClient httpClient = getHttpClient();
	int statusCode = httpClient.executeMethod(httppost);
	byte[] response = httppost.getResponseBody();
	if (statusCode != 200) {
		logger.error("Error while getting access token from OAuth2 provider: server returned statusCode = " + statusCode);
		LogMF.error(logger, "Server response is:\n{0}", new Object[] { new String(response) });
		throw new SpagoBIRuntimeException("Error while getting access token from OAuth2 provider: server returned statusCode = " + statusCode);
	}

	String responseStr = new String(response);
	LogMF.debug(logger, "Server response is:\n{0}", responseStr);
	JSONObject jsonObject = new JSONObject(responseStr);
	String accessToken = jsonObject.getString("access_token");

	return accessToken;
}
 
Example 2
Source File: TestRailClient.java    From testrail-jenkins-plugin with Apache License 2.0 6 votes vote down vote up
private TestRailResponse httpPostInt(String path, String payload)
        throws UnsupportedEncodingException, IOException, HTTPException {
    TestRailResponse result;
    PostMethod post = new PostMethod(host + "/" + path);
    HttpClient httpclient = setUpHttpClient(post);

    try {
        StringRequestEntity requestEntity = new StringRequestEntity(
                payload,
                "application/json",
                "UTF-8"
        );
        post.setRequestEntity(requestEntity);
        Integer status = httpclient.executeMethod(post);
        String body = new String(post.getResponseBody(), post.getResponseCharSet());
        result = new TestRailResponse(status, body);
    } finally {
        post.releaseConnection();
    }

    return result;
}
 
Example 3
Source File: OAuth2Client.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public String getToken(String id, String password) {
	logger.debug("IN");
	try {
		HttpClient client = getHttpClient();
		String url = config.getProperty("REST_BASE_URL") + config.getProperty("TOKEN_PATH");
		PostMethod httppost = new PostMethod(url);
		httppost.setRequestHeader("Content-Type", "application/json");

		String body = "{\r\n";
		body += "    \"auth\": {\r\n";
		body += "        \"identity\": {\r\n";
		body += "            \"methods\": [\r\n";
		body += "                \"password\"\r\n";
		body += "            ],\r\n";
		body += "            \"password\": {\r\n";
		body += "                \"user\": {\r\n";
		body += "                    \"id\": \"" + id + "\",\r\n";
		body += "                    \"password\": \"" + password + "\"\r\n";
		body += "                }\r\n";
		body += "            }\r\n";
		body += "        }\r\n";
		body += "    }\r\n";
		body += "}";

		httppost.setRequestBody(body);

		int statusCode = client.executeMethod(httppost);

		if (statusCode != HttpStatus.SC_CREATED) {
			logger.error("Error while getting access token from IdM REST API: server returned statusCode = " + statusCode);

			byte[] response = httppost.getResponseBody();
			LogMF.error(logger, "Server response is:\n{0}", new Object[] { new String(response) });

			throw new SpagoBIRuntimeException("Error while getting access token from IdM REST API: server returned statusCode = " + statusCode);
		}

		return httppost.getResponseHeader("X-Subject-Token").getValue();
	} catch (Exception e) {
		throw new SpagoBIRuntimeException("Error while trying to get token from IdM REST API", e);
	} finally {
		logger.debug("OUT");
	}
}
 
Example 4
Source File: HttpClientUtils.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
public static String httpPost(String serverUrl, String method,
                              Map paramMap, String code) {
    String url = serverUrl + "/" + method;
    ZogUtils.printLog(HttpClientUtils.class, "request url: " + url);
    String content = null;
    if (url == null || url.trim().length() == 0 || paramMap == null
            || paramMap.isEmpty())
        return null;
    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(url);

    postMethod.setRequestHeader("Content-Type", "text/json;charset=utf-8");

    Iterator it = paramMap.keySet().iterator();

    while (it.hasNext()) {
        String key = it.next() + "";
        Object o = paramMap.get(key);
        if (o != null && o instanceof String) {
            postMethod.addParameter(new NameValuePair(key, o.toString()));
        }

        System.out.println(key + ":" + o);
        postMethod.addParameter(new NameValuePair(key, o.toString()));

    }
    try {
        httpClient.executeMethod(postMethod);
        ZogUtils.printLog(HttpClientUtils.class, postMethod.getStatusLine()
                + "");
        content = new String(postMethod.getResponseBody(), code);

    } catch (Exception e) {
        ZogUtils.printLog(HttpClientUtils.class, "time out");
        e.printStackTrace();
    } finally {
        if (postMethod != null)
            postMethod.releaseConnection();
        postMethod = null;
        httpClient = null;
    }
    return content;

}
 
Example 5
Source File: HttpClientUtils.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
public static String httpPost(String url, Map paramMap, String code) {
    ZogUtils.printLog(HttpClientUtils.class, "request url: " + url);
    String content = null;
    if (url == null || url.trim().length() == 0 || paramMap == null
            || paramMap.isEmpty())
        return null;
    HttpClient httpClient = new HttpClient();
    PostMethod method = new PostMethod(url);

    method.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded;charset=utf-8");

    Iterator it = paramMap.keySet().iterator();

    while (it.hasNext()) {
        String key = it.next() + "";
        Object o = paramMap.get(key);
        if (o != null && o instanceof String) {
            method.addParameter(new NameValuePair(key, o.toString()));
        }

        System.out.println(key + ":" + o);
        method.addParameter(new NameValuePair(key, o.toString()));

    }
    try {
        httpClient.executeMethod(method);
        ZogUtils.printLog(HttpClientUtils.class, method.getStatusLine()
                + "");
        content = new String(method.getResponseBody(), code);

    } catch (Exception e) {
        ZogUtils.printLog(HttpClientUtils.class, "time out");
        e.printStackTrace();
    } finally {
        if (method != null)
            method.releaseConnection();
        method = null;
        httpClient = null;
    }
    return content;

}