org.apache.http.conn.params.ConnRoutePNames Java Examples

The following examples show how to use org.apache.http.conn.params.ConnRoutePNames. 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: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param request
 * @param payload
 * @return
 */
protected HttpResponse sendRequest(HttpUriRequest request) {
  HttpResponse response = null;
  try {
    HttpClient httpclient = new DefaultHttpClient();
    log(request);
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, timeout);
    HttpConnectionParams.setSoTimeout(params, timeout);
    if(proxy != null) {
      httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
    response = httpclient.execute(request);
  } catch(IOException ioe) {
    if (ClientUtils.debugging ) {
      ioe.printStackTrace();
    }
    throw new EFhirClientException("Error sending Http Request: "+ioe.getMessage(), ioe);
  }
  return response;
}
 
Example #2
Source File: ApacheHttpTransaction.java    From OCR-Equation-Solver with MIT License 6 votes vote down vote up
/**
 * If this returns without throwing, then you can (and must) proceed to reading the
 * content using getResponseAsString() or getResponseAsStream(). If it throws, then
 * you do not have to read. You must always call release().
 *
 * @throws HttpHandlerException
 */
public void execute() throws WAHttpException {

    httpGet = new HttpGet(url.toString());
    HttpHost proxy = proxySettings.getProxyForHttpClient(url.toString());
    httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    
    try {
        response = httpClient.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK)
            throw new WAHttpException(statusCode);
        entity = response.getEntity();
    } catch (Exception e) {
        // This also releases all resources.
        httpGet.abort();
        if (e instanceof WAHttpException)
            throw (WAHttpException) e;
        else
            throw new WAHttpException(e);
    }
}
 
Example #3
Source File: PlayManager.java    From raccoon4 with Apache License 2.0 6 votes vote down vote up
/**
 * create a proxy client
 * 
 * @return either a client or null if none is configured
 * @throws KeyManagementException
 * @throws NumberFormatException
 *           if that port could not be parsed.
 * @throws NoSuchAlgorithmException
 */
private static HttpClient createProxyClient(PlayProfile profile)
		throws KeyManagementException, NoSuchAlgorithmException {
	if (profile.getProxyAddress() == null) {
		return null;
	}

	PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(
			SchemeRegistryFactory.createDefault());
	connManager.setMaxTotal(100);
	connManager.setDefaultMaxPerRoute(30);

	DefaultHttpClient client = new DefaultHttpClient(connManager);
	client.getConnectionManager().getSchemeRegistry()
			.register(Utils.getMockedScheme());
	HttpHost proxy = new HttpHost(profile.getProxyAddress(),
			profile.getProxyPort());
	client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
	if (profile.getProxyUser() != null && profile.getProxyPassword() != null) {
		client.getCredentialsProvider().setCredentials(
				new AuthScope(proxy),
				new UsernamePasswordCredentials(profile.getProxyUser(), profile
						.getProxyPassword()));
	}
	return client;
}
 
Example #4
Source File: CheckinWorker.java    From dummydroid with Apache License 2.0 6 votes vote down vote up
protected void setProxy(HttpClient client) throws IOException {
	File cfgfile = new File(NETCFG);
	if (cfgfile.exists()) {
		Properties cfg = new Properties();
		cfg.load(new FileInputStream(cfgfile));
		String ph = cfg.getProperty(PROXYHOST, null);
		String pp = cfg.getProperty(PROXYPORT, null);
		String pu = cfg.getProperty(PROXYUSER, null);
		String pw = cfg.getProperty(PROXYPASS, null);
		if (ph == null || pp == null) {
			return;
		}
		final HttpHost proxy = new HttpHost(ph, Integer.parseInt(pp));
		client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		if (pu != null && pw != null) {
			((DefaultHttpClient) client).getCredentialsProvider().setCredentials(
					new AuthScope(proxy), new UsernamePasswordCredentials(pu, pw));
		}
	}
}
 
Example #5
Source File: HttpClientFactoryImpl.java    From swift-explorer with Apache License 2.0 6 votes vote down vote up
private void setProxySettings (org.apache.http.client.HttpClient client, HasProxySettings proxySettings, String prot)
{
	if (client == null)
		return ;
       if (proxySettings == null || !proxySettings.isActive())
       	return ;
       if (prot == null || prot.isEmpty())
       	return ;
                   
   	org.apache.http.HttpHost proxy = new org.apache.http.HttpHost(proxySettings.getHost(), proxySettings.getPort(), prot) ;
   	client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy) ;
   	
   	CredentialsProvider credProvider = ((AbstractHttpClient) client).getCredentialsProvider();
   	credProvider.setCredentials(
   			new AuthScope(proxySettings.getHost(), proxySettings.getPort()), 
   			new UsernamePasswordCredentials(proxySettings.getUsername(), proxySettings.getPassword()));  
}
 
Example #6
Source File: RestAdapter.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * This is the parameterized constructor to initialize various fields.
 * @param as Accessor
 */
public RestAdapter(Accessor as) {
    this.accessor = as;
    DaoConfig config = accessor.getDaoConfig();
    httpClient = config.getHttpClient();
    if (httpClient == null) {
        httpClient = HttpClientFactory.create(DcContext.getPlatform(), config.getConnectionTimeout());
    }
    String proxyHost = config.getProxyHostname();
    int proxyPort = config.getProxyPort();
    if (proxyHost != null) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        // ID/Passが共にnullでなければ認証Proxyをセット
        String proxyUsername = config.getProxyUsername();
        String proxyPassword = config.getProxyPassword();
        if (httpClient instanceof AbstractHttpClient && proxyUsername != null && proxyPassword != null) {
            ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(
                    new AuthScope(proxyHost, proxyPort),
                    new UsernamePasswordCredentials(proxyUsername, proxyPassword));
        }
    }
}
 
Example #7
Source File: RestUtil.java    From XPagesExtensionLibrary with Apache License 2.0 6 votes vote down vote up
public ClientHttpRequestFactory createRequestFactory(HttpProxyConfiguration httpProxyConfiguration, boolean trustSelfSignedCerts) {
	HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
	HttpClient httpClient = requestFactory.getHttpClient();

	if (trustSelfSignedCerts) {
		registerSslSocketFactory(httpClient);
	} else {
	    // We don't use self signed certs in Designer so this code will always be executed
	    // TLSv1.0 has been disabled on Bluemix/Datapower 
	    // We need to use TLSv1.2 or TLSv1.1 instead
	    registerSslSocketFactoryTLS(httpClient);
	}
	    
	if (httpProxyConfiguration != null) {
		HttpHost proxy = new HttpHost(httpProxyConfiguration.getProxyHost(), httpProxyConfiguration.getProxyPort());
		httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
	}

	return requestFactory;
}
 
Example #8
Source File: ProxyWrappingHttpClientFactory.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public HttpClient create(final HttpMethod method, final URI uri) {
  // Use wrapped factory to obtain an httpclient instance for given method and uri
  final DefaultHttpClient httpclient = wrapped.create(method, uri);

  final HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort());

  // Sets usage of HTTP proxy
  httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost);

  // Sets proxy authentication, if credentials were provided
  if (proxyUsername != null && proxyPassword != null) {
    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(proxyHost),
            new UsernamePasswordCredentials(proxyUsername, proxyPassword));
  }

  return httpclient;
}
 
Example #9
Source File: WebServiceNtlmSender.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Override
public void configure() throws ConfigurationException {
	super.configure();

	HttpParams httpParameters = new BasicHttpParams();
	HttpConnectionParams.setConnectionTimeout(httpParameters, getTimeout());
	HttpConnectionParams.setSoTimeout(httpParameters, getTimeout());
	httpClient = new DefaultHttpClient(connectionManager, httpParameters);
	httpClient.getAuthSchemes().register("NTLM", new NTLMSchemeFactory());
	CredentialFactory cf = new CredentialFactory(getAuthAlias(), getUserName(), getPassword());
	httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new NTCredentials(cf.getUsername(), cf.getPassword(), Misc.getHostname(), getAuthDomain()));
	if (StringUtils.isNotEmpty(getProxyHost())) {
		HttpHost proxy = new HttpHost(getProxyHost(), getProxyPort());
		httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
	}
}
 
Example #10
Source File: GoogleAnalytics.java    From h2o-2 with Apache License 2.0 6 votes vote down vote up
protected HttpClient createHttpClient(GoogleAnalyticsConfig config) {
  ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager();
  connManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute(config));

  BasicHttpParams params = new BasicHttpParams();

  if (isNotEmpty(config.getUserAgent())) {
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgent());
  }

  if (isNotEmpty(config.getProxyHost())) {
    params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(config.getProxyHost(), config.getProxyPort()));
  }

  DefaultHttpClient client = new DefaultHttpClient(connManager, params);

  if (isNotEmpty(config.getProxyUserName())) {
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
            new UsernamePasswordCredentials(config.getProxyUserName(), config.getProxyPassword()));
    client.setCredentialsProvider(credentialsProvider);
  }

  return client;
}
 
Example #11
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * Method posts request payload
 * 
 * @param request
 * @param payload
 * @return
 */
protected HttpResponse sendPayload(HttpEntityEnclosingRequestBase request, byte[] payload, HttpHost proxy) {
  HttpResponse response = null;
boolean ok = false;
int tryCount = 0;
while (!ok) {
  try {
    tryCount++;
    HttpClient httpclient = new DefaultHttpClient();
    if(proxy != null) {
      httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
    request.setEntity(new ByteArrayEntity(payload));
    log(request);
    response = httpclient.execute(request);
    ok = true;
  } catch(IOException ioe) {
    if (tryCount <= retryCount) {
      ok = false;
    } else {
    throw new EFhirClientException("Error sending HTTP Post/Put Payload: "+ioe.getMessage(), ioe);
  }
  }
}
  return response;
}
 
Example #12
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param request
 * @param payload
 * @return
 */
 protected HttpResponse sendRequest(HttpUriRequest request) {
	HttpResponse response = null;
	try {
		HttpClient httpclient = new DefaultHttpClient();
     log(request);
		HttpParams params = httpclient.getParams();
     HttpConnectionParams.setConnectionTimeout(params, timeout);
     HttpConnectionParams.setSoTimeout(params, timeout);
		if(proxy != null) {
			httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		}
		response = httpclient.execute(request);
	} catch(IOException ioe) {
     throw new EFhirClientException("Error sending Http Request: "+ioe.getMessage(), ioe);
	}
	return response;
}
 
Example #13
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * Method posts request payload
 * 
 * @param request
 * @param payload
 * @return
 */
 protected HttpResponse sendPayload(HttpEntityEnclosingRequestBase request, byte[] payload, HttpHost proxy) {
	HttpResponse response = null;
	boolean ok = false;
	int tryCount = 0;
	while (!ok) {
	try {
	    tryCount++;
		HttpClient httpclient = new DefaultHttpClient();
		if(proxy != null) {
			httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		}
		request.setEntity(new ByteArrayEntity(payload));
     log(request);
		response = httpclient.execute(request);
	    ok = true;
	} catch(IOException ioe) {
	    if (tryCount <= retryCount) {
	      ok = false;
	    } else {
		throw new EFhirClientException("Error sending HTTP Post/Put Payload: "+ioe.getMessage(), ioe);
	}
	  }
	}
	return response;
}
 
Example #14
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * Method posts request payload
 * 
 * @param request
 * @param payload
 * @return
 */
protected HttpResponse sendPayload(HttpEntityEnclosingRequestBase request, byte[] payload, HttpHost proxy) {
  HttpResponse response = null;
  boolean ok = false;
  int tryCount = 0;
  while (!ok) {
    try {
      tryCount++;
      HttpClient httpclient = new DefaultHttpClient();
      if(proxy != null) {
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
      }
      request.setEntity(new ByteArrayEntity(payload));
      log(request);
      response = httpclient.execute(request);
      ok = true;
    } catch(IOException ioe) {
      if (tryCount <= retryCount) {
        ok = false;
      } else {
        throw new EFhirClientException("Error sending HTTP Post/Put Payload: "+ioe.getMessage(), ioe);
      }
    }
  }
  return response;
}
 
Example #15
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param request
 * @param payload
 * @return
 */
protected HttpResponse sendRequest(HttpUriRequest request) {
  HttpResponse response = null;
  try {
    HttpClient httpclient = new DefaultHttpClient();
    log(request);
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, timeout);
    HttpConnectionParams.setSoTimeout(params, timeout);
    if(proxy != null) {
      httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
    response = httpclient.execute(request);
  } catch(IOException ioe) {
    throw new EFhirClientException("Error sending Http Request: "+ioe.getMessage(), ioe);
  }
  return response;
}
 
Example #16
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * Method posts request payload
 * 
 * @param request
 * @param payload
 * @return
 */
protected HttpResponse sendPayload(HttpEntityEnclosingRequestBase request, byte[] payload, HttpHost proxy) {
  HttpResponse response = null;
  boolean ok = false;
  int tryCount = 0;
  while (!ok) {
    try {
      tryCount++;
      HttpClient httpclient = new DefaultHttpClient();
      if(proxy != null) {
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
      }
      request.setEntity(new ByteArrayEntity(payload));
      log(request);
      response = httpclient.execute(request);
      ok = true;
    } catch(IOException ioe) {
      if (tryCount <= retryCount) {
        ok = false;
      } else {
        throw new EFhirClientException("Error sending HTTP Post/Put Payload: "+ioe.getMessage(), ioe);
      }
    }
  }
  return response;
}
 
Example #17
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param request
 * @param payload
 * @return
 */
protected HttpResponse sendRequest(HttpUriRequest request) {
  HttpResponse response = null;
  try {
    HttpClient httpclient = new DefaultHttpClient();
    log(request);
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, timeout);
    HttpConnectionParams.setSoTimeout(params, timeout);
    if(proxy != null) {
      httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
    response = httpclient.execute(request);
  } catch(IOException ioe) {
    throw new EFhirClientException("Error sending Http Request: "+ioe.getMessage(), ioe);
  }
  return response;
}
 
Example #18
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param request
 * @param payload
 * @return
 */
protected HttpResponse sendRequest(HttpUriRequest request) {
  HttpResponse response = null;
  try {
    HttpClient httpclient = new DefaultHttpClient();
    log(request);
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, timeout);
    HttpConnectionParams.setSoTimeout(params, timeout);
    if(proxy != null) {
      httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
    response = httpclient.execute(request);
  } catch(IOException ioe) {
    throw new EFhirClientException("Error sending Http Request: "+ioe.getMessage(), ioe);
  }
  return response;
}
 
Example #19
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * Method posts request payload
 * 
 * @param request
 * @param payload
 * @return
 */
protected HttpResponse sendPayload(HttpEntityEnclosingRequestBase request, byte[] payload, HttpHost proxy) {
  HttpResponse response = null;
  boolean ok = false;
  int tryCount = 0;
  while (!ok) {
    try {
      tryCount++;
      HttpClient httpclient = new DefaultHttpClient();
      if(proxy != null) {
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
      }
      request.setEntity(new ByteArrayEntity(payload));
      log(request);
      response = httpclient.execute(request);
      ok = true;
    } catch(IOException ioe) {
      if (tryCount <= retryCount) {
        ok = false;
      } else {
        throw new EFhirClientException("Error sending HTTP Post/Put Payload: "+ioe.getMessage(), ioe);
      }
    }
  }
  return response;
}
 
Example #20
Source File: NUHttpClient.java    From neembuu-uploader with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set the proxy.
 * @param hostname the hostname (IP or DNS name)
 * @param port the port number. -1 indicates the scheme default port.
 * @param scheme the name of the scheme. null indicates the default scheme
 */
public static void setProxy(String hostname, String port, String scheme) throws NUProxyException, NUProxyPortException, NUProxyHostException{
    //Control the hostname
    checkProxyHost(hostname);
    
    //Control the port
    checkProxyPort(port);
    
    checkProxy(hostname, port);
    
    HttpHost proxy = new HttpHost(hostname, Integer.parseInt(port), scheme);
    httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
 
Example #21
Source File: HttpUtils.java    From jmeter-bzm-plugins with Apache License 2.0 5 votes vote down vote up
private static AbstractHttpClient createHTTPClient() {
    AbstractHttpClient client = new DefaultHttpClient();
    String proxyHost = System.getProperty("https.proxyHost", "");
    if (!proxyHost.isEmpty()) {
        int proxyPort = Integer.parseInt(System.getProperty("https.proxyPort", "-1"));
        log.info("Using proxy " + proxyHost + ":" + proxyPort);
        HttpParams params = client.getParams();
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        String proxyUser = System.getProperty(JMeter.HTTP_PROXY_USER, JMeterUtils.getProperty(JMeter.HTTP_PROXY_USER));
        if (proxyUser != null) {
            log.info("Using authenticated proxy with username: " + proxyUser);
            String proxyPass = System.getProperty(JMeter.HTTP_PROXY_PASS, JMeterUtils.getProperty(JMeter.HTTP_PROXY_PASS));

            String localHost;
            try {
                localHost = InetAddress.getLocalHost().getCanonicalHostName();
            } catch (Throwable e) {
                log.error("Failed to get local host name, defaulting to 'localhost'", e);
                localHost = "localhost";
            }

            AuthScope authscope = new AuthScope(proxyHost, proxyPort);
            String proxyDomain = JMeterUtils.getPropDefault("http.proxyDomain", "");
            NTCredentials credentials = new NTCredentials(proxyUser, proxyPass, localHost, proxyDomain);
            client.getCredentialsProvider().setCredentials(authscope, credentials);
        }
    }
    return client;
}
 
Example #22
Source File: NUHttpClient.java    From neembuu-uploader with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set the proxy.
 * @param hostname the hostname (IP or DNS name)
 * @param port the port number. -1 indicates the scheme default port.
 */
public static void setProxy(String hostname, String port) throws NUProxyException, NUProxyPortException, NUProxyHostException{
    //Control the hostname
    checkProxyHost(hostname);
    
    //Control the port
    checkProxyPort(port);
    
    checkProxy(hostname, port);
    
    HttpHost proxy = new HttpHost(hostname, Integer.parseInt(port));
    httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
 
Example #23
Source File: AsyncHttpClient.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * Sets the Proxy by it's hostname,port,username and password
 *
 * @param hostname the hostname (IP or DNS name)
 * @param port     the port number. -1 indicates the scheme default port.
 * @param username the username
 * @param password the password
 */
public void setProxy(String hostname, int port, String username, String password) {
    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope(hostname, port),
            new UsernamePasswordCredentials(username, password));
    final HttpHost proxy = new HttpHost(hostname, port);
    final HttpParams httpParams = this.httpClient.getParams();
    httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
 
Example #24
Source File: AsyncHttpClient.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * Sets the Proxy by it's hostname,port,username and password
 *
 * @param hostname the hostname (IP or DNS name)
 * @param port     the port number. -1 indicates the scheme default port.
 * @param username the username
 * @param password the password
 */
public void setProxy(String hostname, int port, String username, String password) {
    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope(hostname, port),
            new UsernamePasswordCredentials(username, password));
    final HttpHost proxy = new HttpHost(hostname, port);
    final HttpParams httpParams = this.httpClient.getParams();
    httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
 
Example #25
Source File: cfHttpConnection.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setProxyServer( String _proxyServer, int _proxyPort ) {
	proxyServer = _proxyServer;
	proxyPort = _proxyPort;
	HttpHost proxy = new HttpHost( proxyServer, proxyPort );
	client.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy );
}
 
Example #26
Source File: AsyncHttpClient.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * Sets the Proxy by it's hostname,port,username and password
 *
 * @param hostname the hostname (IP or DNS name)
 * @param port     the port number. -1 indicates the scheme default port.
 * @param username the username
 * @param password the password
 */
public void setProxy(String hostname, int port, String username, String password) {
    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope(hostname, port),
            new UsernamePasswordCredentials(username, password));
    final HttpHost proxy = new HttpHost(hostname, port);
    final HttpParams httpParams = this.httpClient.getParams();
    httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
 
Example #27
Source File: cfHttpConnection.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
private void setDefaultProxy() {
	HttpHost currentProxy = ( HttpHost )client.getParams().getParameter( ConnRoutePNames.DEFAULT_PROXY );

	if ( currentProxy == null ) {
		ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner( client.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault() );
		client.setRoutePlanner( routePlanner );
	}
}
 
Example #28
Source File: AsyncHttpClient.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the Proxy by it's hostname,port,username and password
 *
 * @param hostname the hostname (IP or DNS name)
 * @param port     the port number. -1 indicates the scheme default port.
 * @param username the username
 * @param password the password
 */
public void setProxy(String hostname, int port, String username, String password) {
    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope(hostname, port),
            new UsernamePasswordCredentials(username, password));
    final HttpHost proxy = new HttpHost(hostname, port);
    final HttpParams httpParams = this.httpClient.getParams();
    httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
 
Example #29
Source File: AsyncHttpClient.java    From sealtalk-android with MIT License 5 votes vote down vote up
/**
 * Sets the Proxy by it's hostname,port,username and password
 *
 * @param hostname the hostname (IP or DNS name)
 * @param port     the port number. -1 indicates the scheme default port.
 * @param username the username
 * @param password the password
 */
public void setProxy(String hostname, int port, String username, String password) {
    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope(hostname, port),
            new UsernamePasswordCredentials(username, password));
    final HttpHost proxy = new HttpHost(hostname, port);
    final HttpParams httpParams = this.httpClient.getParams();
    httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
 
Example #30
Source File: Archive.java    From Raccoon with Apache License 2.0 5 votes vote down vote up
/**
 * Get a proxy client, if it is configured.
 * 
 * @return either a client or null
 * @throws IOException
 *           if reading the config file fails
 * @throws KeyManagementException
 * @throws NumberFormatException
 *           if that port could not be parsed.
 * @throws NoSuchAlgorithmException
 */
public HttpClient getProxyClient() throws IOException, KeyManagementException,
		NoSuchAlgorithmException, NumberFormatException {
	File cfgfile = new File(root, NETCFG);
	if (cfgfile.exists()) {
		Properties cfg = new Properties();
		cfg.load(new FileInputStream(cfgfile));
		String ph = cfg.getProperty(PROXYHOST, null);
		String pp = cfg.getProperty(PROXYPORT, null);
		String pu = cfg.getProperty(PROXYUSER, null);
		String pw = cfg.getProperty(PROXYPASS, null);
		if (ph == null || pp == null) {
			return null;
		}
		PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(
				SchemeRegistryFactory.createDefault());
		connManager.setMaxTotal(100);
		connManager.setDefaultMaxPerRoute(30);

		DefaultHttpClient client = new DefaultHttpClient(connManager);
		client.getConnectionManager().getSchemeRegistry().register(Utils.getMockedScheme());
		HttpHost proxy = new HttpHost(ph, Integer.parseInt(pp));
		client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		if (pu != null && pw != null) {
			client.getCredentialsProvider().setCredentials(new AuthScope(proxy),
					new UsernamePasswordCredentials(pu, pw));
		}
		return client;
	}
	return null;
}