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

The following examples show how to use org.apache.commons.httpclient.methods.GetMethod#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: HttpCommonClient.java    From openrasp-testcases with MIT License 6 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
        String urlString = req.getParameter("url");
        if (urlString != null) {
            HttpClient httpClient = new HttpClient();
            GetMethod getMethod = new GetMethod(urlString);
            httpClient.executeMethod(getMethod);
            String charSet = getMethod.getResponseCharSet();
            byte[] responseBody = getMethod.getResponseBody();
            resp.getWriter().println("response:\r\n" + new String(responseBody, charSet));
        }
    } catch (IOException e) {
        resp.getWriter().println(e);
    }
}
 
Example 2
Source File: OldHttpClientApi.java    From javabase with Apache License 2.0 6 votes vote down vote up
public void  get() throws UnsupportedEncodingException {
    HttpClient client = new HttpClient();
    String APIKEY = "4b441cb500f431adc6cc0cb650b4a5d0";
    String INFO =new URLCodec().encode("who are you","utf-8");
    String requesturl = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;
    GetMethod getMethod = new GetMethod(requesturl);
    try {
        int stat = client.executeMethod(getMethod);
        if (stat != HttpStatus.SC_OK)
           log.error("get失败!");
        byte[] responseBody = getMethod.getResponseBody();
        String content=new String(responseBody ,"utf-8");
        log.info(content);
    }catch (Exception e){
       log.error(""+e.getLocalizedMessage());
    }finally {
        getMethod.releaseConnection();
    }
}
 
Example 3
Source File: PortFactory.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the content under the given URL with username and passwort
 * authentication.
 * 
 * @param url
 * @param username
 * @param password
 * @return
 * @throws IOException
 */
private static byte[] getUrlContent(URL url, String username,
        String password) throws IOException {
    final HttpClient client = new HttpClient();

    // Set credentials:
    client.getParams().setAuthenticationPreemptive(true);
    final Credentials credentials = new UsernamePasswordCredentials(
            username, password);
    client.getState()
            .setCredentials(
                    new AuthScope(url.getHost(), url.getPort(),
                            AuthScope.ANY_REALM), credentials);

    // Retrieve content:
    final GetMethod method = new GetMethod(url.toString());
    final int status = client.executeMethod(method);
    if (status != HttpStatus.SC_OK) {
        throw new IOException("Error " + status + " while retrieving "
                + url);
    }
    return method.getResponseBody();
}
 
Example 4
Source File: BasicAuthLoader.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the content under the given URL with username and passwort
 * authentication.
 * 
 * @param url
 *            the URL to read
 * @param username
 * @param password
 * @return the read content.
 * @throws IOException
 *             if an I/O exception occurs.
 */
private static byte[] getUrlContent(URL url, String username,
        String password) throws IOException {
    final HttpClient client = new HttpClient();

    // Set credentials:
    client.getParams().setAuthenticationPreemptive(true);
    final Credentials credentials = new UsernamePasswordCredentials(
            username, password);
    client.getState()
            .setCredentials(
                    new AuthScope(url.getHost(), url.getPort(),
                            AuthScope.ANY_REALM), credentials);

    // Retrieve content:
    final GetMethod method = new GetMethod(url.toString());
    final int status = client.executeMethod(method);
    if (status != HttpStatus.SC_OK) {
        throw new IOException("Error " + status + " while retrieving "
                + url);
    }
    return method.getResponseBody();
}
 
Example 5
Source File: YarnRestJobStatusProvider.java    From samza with Apache License 2.0 6 votes vote down vote up
/**
 * Issues a HTTP Get request to the provided url and returns the response
 * @param requestUrl the request url
 * @return the response
 * @throws IOException if there are problems with the http get request.
 */
byte[] httpGet(String requestUrl)
    throws IOException {
  GetMethod getMethod = new GetMethod(requestUrl);
  try {
    int responseCode = this.httpClient.executeMethod(getMethod);
    LOGGER.debug("Received response code: {} for the get request on the url: {}", responseCode, requestUrl);
    byte[] response = getMethod.getResponseBody();
    if (responseCode != HttpStatus.SC_OK) {
      throw new SamzaException(
          String.format("Received response code: %s for get request on: %s, with message: %s.", responseCode,
              requestUrl, StringUtils.newStringUtf8(response)));
    }
    return response;
  } finally {
    getMethod.releaseConnection();
  }
}
 
Example 6
Source File: JobsClient.java    From samza with Apache License 2.0 6 votes vote down vote up
/**
 * This method initiates http get request on the request url and returns the
 * response returned from the http get.
 * @param requestUrl url on which the http get request has to be performed.
 * @return the http get response.
 * @throws IOException if there are problems with the http get request.
 */
private byte[] httpGet(String requestUrl) throws IOException {
  GetMethod getMethod = new GetMethod(requestUrl);
  try {
    int responseCode = httpClient.executeMethod(getMethod);
    LOG.debug("Received response code: {} for the get request on the url: {}", responseCode, requestUrl);
    byte[] response = getMethod.getResponseBody();
    if (responseCode != HttpStatus.SC_OK) {
      throw new SamzaException(String.format("Received response code: %s for get request on: %s, with message: %s.",
                                             responseCode, requestUrl, StringUtils.newStringUtf8(response)));
    }
    return response;
  } finally {
    getMethod.releaseConnection();
  }
}
 
Example 7
Source File: ExtractRegular.java    From HtmlExtractor with Apache License 2.0 6 votes vote down vote up
/**
 * 从配置管理WEB服务器下载规则(json表示)
 *
 * @param url 配置管理WEB服务器下载规则的地址
 * @return json字符串
 */
private String downJson(String url) {
    // 构造HttpClient的实例
    HttpClient httpClient = new HttpClient();
    // 创建GET方法的实例
    GetMethod method = new GetMethod(url);
    try {
        // 执行GetMethod
        int statusCode = httpClient.executeMethod(method);
        LOGGER.info("响应代码:" + statusCode);
        if (statusCode != HttpStatus.SC_OK) {
            LOGGER.error("请求失败: " + method.getStatusLine());
        }
        // 读取内容
        String responseBody = new String(method.getResponseBody(), "utf-8");
        return responseBody;
    } catch (IOException e) {
        LOGGER.error("检查请求的路径:" + url, e);
    } finally {
        // 释放连接
        method.releaseConnection();
    }
    return "";
}
 
Example 8
Source File: OAuth2TenantInitializer.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private String getTenantName(String organizationId) {
	try {
		OAuth2Client oauth2Client = new OAuth2Client();

		String token = oauth2Client.getAdminToken();

		HttpClient httpClient = oauth2Client.getHttpClient();
		String url = config.getProperty("REST_BASE_URL") + config.getProperty("ORGANIZATION_INFO_PATH") + organizationId;
		GetMethod httpget = new GetMethod(url);
		httpget.addRequestHeader("X-Auth-Token", token);

		int statusCode = httpClient.executeMethod(httpget);
		byte[] response = httpget.getResponseBody();
		if (statusCode != HttpStatus.SC_OK) {
			logger.error("Error while getting the name of organization with id [" + organizationId + "]: server returned statusCode = " + statusCode);
			LogMF.error(logger, "Server response is:\n{0}", new Object[] { new String(response) });
			throw new SpagoBIRuntimeException(
					"Error while getting the name of organization with id [" + organizationId + "]: server returned statusCode = " + statusCode);
		}

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

		return jsonObject.getJSONObject("project").getString("name");
	} catch (Exception e) {
		throw new SpagoBIRuntimeException("Error while trying to obtain tenant' name from IdM REST API", e);
	} finally {
		logger.debug("OUT");
	}
}
 
Example 9
Source File: FileServiceDataSetCRUD.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * get CSV file from remote Url
 *
 * @param url
 * @return
 * @throws HttpException
 * @throws IOException
 */
byte[] getCSVFile(String url) throws HttpException, IOException {
	logger.debug("IN");
	HttpClient client = new HttpClient();
	HttpConnectionManager conManager = client.getHttpConnectionManager();

	// Cancel, proxy settings made on server
	// logger.debug("Setting proxy");
	// client.getHostConfiguration().setProxy("192.168.10.1", 3128);
	// HttpState state = new HttpState();
	// state.setProxyCredentials(null, null, new UsernamePasswordCredentials("", ""));
	// client.setState(state);
	// logger.debug("proxy set");
	// cancel

	GetMethod httpGet = new GetMethod(url);
	int statusCode = client.executeMethod(httpGet);
	logger.debug("Status code after request to remote URL is " + statusCode);
	byte[] response = httpGet.getResponseBody();

	if (response == null) {
		logger.warn("Response of remote URL is empty");
	}

	httpGet.releaseConnection();
	logger.debug("OUT");
	return response;
}
 
Example 10
Source File: OAuth2SecurityInfoProvider.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private static JSONObject getRolesAsJson() {
	try {

		OAuth2Client oauth2Client = new OAuth2Client();

		Properties config = OAuth2Config.getInstance().getConfig();

		// Retrieve the admin's token for REST services authentication
		String token = oauth2Client.getAdminToken();

		HttpClient httpClient = oauth2Client.getHttpClient();

		// Get roles of the application (specified in the
		// oauth2.config.properties)
		String url = config.getProperty("REST_BASE_URL") + config.getProperty("ROLES_PATH") + "?application_id=" + config.getProperty("APPLICATION_ID");
		GetMethod httpget = new GetMethod(url);
		httpget.addRequestHeader("X-Auth-Token", token);

		int statusCode = httpClient.executeMethod(httpget);
		byte[] response = httpget.getResponseBody();
		if (statusCode != HttpStatus.SC_OK) {
			logger.error("Error while getting application information from IdM REST API: server returned statusCode = " + statusCode);
			LogMF.error(logger, "Server response is:\n{0}", new Object[] { new String(response) });
			throw new SpagoBIRuntimeException("Error while getting application information from IdM REST API: server returned statusCode = " + statusCode);
		}

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

		return jsonObject;

	} catch (Exception e) {
		throw new SpagoBIRuntimeException("Error while getting roles list from OAuth2 provider", e);
	}

}
 
Example 11
Source File: TestRailClient.java    From testrail-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
private TestRailResponse httpGetInt(String path) throws IOException {
    TestRailResponse result;
    GetMethod get = new GetMethod(host + "/" + path);
    HttpClient httpclient = setUpHttpClient(get);

    try {
        Integer status = httpclient.executeMethod(get);
        String body = new String(get.getResponseBody(), get.getResponseCharSet());
        result = new TestRailResponse(status, body);
    } finally {
        get.releaseConnection();
    }

    return result;
}
 
Example 12
Source File: HttpClientUtil.java    From Gather-Platform with GNU General Public License v3.0 4 votes vote down vote up
public byte[] getAsByte(String url) throws IOException {
//        clearCookies();
        GetMethod g = new GetMethod(url);
        hc.executeMethod(g);
        return g.getResponseBody();
    }
 
Example 13
Source File: OAuth2TenantInitializer.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
private List<String> getTenants() {
	logger.debug("IN");
	List<String> tenants = new ArrayList<>();

	try {
		OAuth2Client oauth2Client = new OAuth2Client();

		Properties config = OAuth2Config.getInstance().getConfig();

		// Retrieve the admin's token for REST services authentication
		String token = oauth2Client.getAdminToken();

		HttpClient httpClient = oauth2Client.getHttpClient();

		// Get roles of the application (specified in the
		// oauth2.config.properties)
		String url = config.getProperty("REST_BASE_URL") + "users/" + config.getProperty("ADMIN_ID") + "/projects";
		GetMethod httpget = new GetMethod(url);
		httpget.addRequestHeader("X-Auth-Token", token);

		int statusCode = httpClient.executeMethod(httpget);
		byte[] response = httpget.getResponseBody();
		if (statusCode != HttpStatus.SC_OK) {
			logger.error("Error while getting organizations 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 organizations 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);
		JSONArray organizationsList = jsonObject.getJSONArray("projects");

		for (int i = 0; i < organizationsList.length(); i++) {
			if (!organizationsList.getJSONObject(i).has("is_default")) {
				String organizationId = organizationsList.getJSONObject(i).getString("id");
				String organizationName = getTenantName(organizationId);

				tenants.add(organizationName);

			}
		}

		return tenants;
	} catch (Exception e) {
		throw new SpagoBIRuntimeException("Error while trying to obtain tenants' informations from OAuth2 provider", e);
	} finally {
		logger.debug("OUT");
	}
}
 
Example 14
Source File: HttpClientUtil.java    From spider with GNU General Public License v3.0 4 votes vote down vote up
public byte[] getAsByte(String url) throws IOException {
//        clearCookies();
        GetMethod g = new GetMethod(url);
        hc.executeMethod(g);
        return g.getResponseBody();
    }