Java Code Examples for org.apache.commons.httpclient.HttpClient#setState()

The following examples show how to use org.apache.commons.httpclient.HttpClient#setState() . 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
public HttpClient getHttpClient() {

		String proxyUrl = System.getProperty("http.proxyHost");
		String proxyPort = System.getProperty("http.proxyPort");
		String proxyUser = System.getProperty("http.proxyUsername");
		String proxyPassword = System.getProperty("http.proxyPassword");

		HttpClient client = new HttpClient();
		if (proxyUrl != null && proxyPort != null) {
			logger.debug("Setting proxy configuration ...");
			client.getHostConfiguration().setProxy(proxyUrl, Integer.parseInt(proxyPort));
			if (proxyUser != null) {
				logger.debug("Setting proxy authentication configuration ...");
				HttpState state = new HttpState();
				state.setProxyCredentials(null, null, new UsernamePasswordCredentials(proxyUser, proxyPassword));
				client.setState(state);
			}
		} else {
			logger.debug("No proxy configuration found");
		}

		return client;
	}
 
Example 2
Source File: HttpClientUtil.java    From boubei-tss with Apache License 2.0 5 votes vote down vote up
/**
  * <p>
  * 初始化HttpClient对象,同时设置转发应用的Cookie信息。
  * 将当前请求中的cookie信息(除去sessionId cookie 和 token cookie)设置到新的请求中来
  * </p>
  * @param targetAS 转发的目标应用
  * @return
  */
 public static HttpClient getHttpClient(AppServer targetAS) {
 	HttpState initialState = new HttpState();
     HttpServletRequest request = Context.getRequestContext().getRequest();
     javax.servlet.http.Cookie[] cookies = request.getCookies();
     cookies = (javax.servlet.http.Cookie[]) EasyUtils.checkNull(cookies, new javax.servlet.http.Cookie[] {});
     
     // 设置转发Cookies信息
     AppServer currentAS = Context.getApplicationContext().getCurrentAppServer();
     for (javax.servlet.http.Cookie cookie : request.getCookies()) {
         String cookieName = cookie.getName();
         if (cookieName.equals(currentAS.getSessionIdName()) 
         		|| cookieName.equals(RequestContext.USER_TOKEN)) {
             continue;
         }
         
         // 保存当前应用以外的sessionId信息的cookie一般是以其应用Code命名的,当前应用的则以JSESSIONID命名
         if (cookieName.equals(targetAS.getCode())) { 
             cookieName = targetAS.getSessionIdName();
         }
         String domain = targetAS.getDomain();
String path   = targetAS.getPath();
Cookie apacheCookie = new Cookie(domain, cookieName, cookie.getValue(), path, null, request.isSecure());
         initialState.addCookie(apacheCookie);
     }
     
     HttpClient client = getHttpClient();
     client.setState(initialState);
     
     return client;
 }
 
Example 3
Source File: RORClient.java    From development with Apache License 2.0 5 votes vote down vote up
public HttpClient createHttpClient() {
	HttpClient client = new HttpClient();

	String proxyHost = System.getProperty(HTTPS_PROXY_HOST);
	String proxyPort = System.getProperty(HTTPS_PROXY_PORT);
	String proxyUser = System.getProperty(HTTPS_PROXY_USER);
	String proxyPassword = System.getProperty(HTTPS_PROXY_PASSWORD);
	int proxyPortInt = 0;

	try {
		proxyPortInt = Integer.parseInt(proxyPort);
	} catch (NumberFormatException e) {
		// ignore
	}
	if (!useProxyByPass(this.apiUrl)) {
		if (proxyHost != null && proxyPortInt > 0) {
			client.getHostConfiguration().setProxy(proxyHost, proxyPortInt);

			if (proxyUser != null && proxyUser.length() > 0
					&& proxyPassword != null && proxyPassword.length() > 0) {
				HttpState state = new HttpState();
				Credentials proxyCredentials = new UsernamePasswordCredentials(
						proxyUser, proxyPassword);
				state.setProxyCredentials(new AuthScope(proxyHost,
						proxyPortInt), proxyCredentials);
				client.setState(state);
			}
		}
	}
	return client;
}
 
Example 4
Source File: CredentialsUtils.java    From httpclientAuthHelper with Apache License 2.0 5 votes vote down vote up
public static void setProxyHost(HttpClient httpClient, UsernamePasswordCredentials proxyCredentials,
                                String proxyHost, int proxyPort) {

    if (proxyHost != null && !proxyHost.isEmpty()) {
        httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
        if (proxyCredentials != null) {
            HttpState state = new HttpState();
            state.setProxyCredentials(new AuthScope(proxyHost, proxyPort), proxyCredentials);
            httpClient.setState(state);
        }
    }
}