DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials( new AuthScope("PROXY HOST", 8080), new UsernamePasswordCredentials("username", "password")); HttpHost targetHost = new HttpHost("TARGET HOST", 443, "https"); HttpHost proxy = new HttpHost("PROXY HOST", 8080); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
public HttpClient createHttpClient( String user, String password, String proxyHost, int proxyPort, AuthScope authScope ) { HttpHost httpHost = new HttpHost( proxyHost, proxyPort ); RequestConfig requestConfig = RequestConfig.custom() .setProxy( httpHost ) .build(); CredentialsProvider provider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials( user, password ); provider.setCredentials( authScope, credentials ); return HttpClientBuilder .create() .setDefaultCredentialsProvider( provider ) .setDefaultRequestConfig( requestConfig ) .setConnectionManager( manager ) .build(); }
private void initHttpRestClient() { httpClient = new DefaultHttpClient(); restHost = new HttpHost(kaaRestHost, kaaRestPort, "http"); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(restHost, basicAuth); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(kaaRestHost, kaaRestPort, AuthScope.ANY_REALM), new UsernamePasswordCredentials(kaaRestUser, kaaRestPassword)); httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache); httpContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider); }
HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpclient.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy); && config.getProxyPassword() != null) { httpclient.getCredentialsProvider().setCredentials( new AuthScope(proxyHost, proxyPort), new NTCredentials(config.getProxyUsername(), config.getProxyPassword(),
/** * @param host the hostname (IP or DNS name) * @param port the port number. {@code -1} indicates the scheme default port. * @param scheme the name of the scheme. {@code null} indicates the {@link HttpHost#DEFAULT_SCHEME_NAME default scheme} * @param login login the user name * @param password password the password * @return this for fluent addAuthentication */ public CommonDataLoader addAuthentication(final String host, final int port, final String scheme, final String login, final String password) { final HttpHost httpHost = new HttpHost(host, port, scheme); final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(login, password); authenticationMap.put(httpHost, credentials); httpClient = null; return this; }
provider = new BasicCredentialsProvider(); provider.setCredentials(AuthScope.ANY, credentials); httpClientBuilder.setDefaultCredentialsProvider(provider); HttpHost targetHost = new HttpHost(httpPost.getURI().getHost(), httpPost.getURI().getPort());
AndroidHttpClient httpClient = AndroidHttpClient.newInstance("User Agent"); URL urlObj = new URL(url); HttpHost host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol()); AuthScope scope = new AuthScope(urlObj.getHost(), urlObj.getPort()); HttpContext credContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost (url); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairsArrayList)); // Execute post request and get http response HttpResponse httpResponse = httpClient.execute(host, httpPost, credContext); httpClient.close();
Integer proxyPort = Integer.parseInt(proxyPortStr); requestConfigBuilder.setProxy(new HttpHost(proxyHost, proxyPort)); logger.log(Level.INFO, "Using proxy: " + proxyHost + ":" + proxyPort); credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword)); httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
HttpHost targetHost = new HttpHost("localhost", 80, "http"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("username", "password"));
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; }
final HttpHost httpHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getScheme()); builder.setRoutePlanner(new NonProxyListProxyRoutePlanner(httpHost, proxy.getNonProxyHosts())); AuthScope authScope = new AuthScope(httpHost, auth.getRealm(), auth.getAuthScheme()); credentialsProvider.setCredentials(authScope, credentials);
/** * @param host * host * @param port * port * @param scheme * scheme * @param login * login * @param password * password * @return this for fluent addAuthentication */ public CommonsDataLoader addAuthentication(final String host, final int port, final String scheme, final String login, final String password) { final HttpHost httpHost = new HttpHost(host, port, scheme); final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(login, password); authenticationMap.put(httpHost, credentials); return this; }
HttpHost targetHost = new HttpHost("localhost", 8080, "http"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS)); AuthCache authCache = new BasicAuthCache(); authCache.put(targetHost, new BasicScheme()); // Add AuthCache to the execution context final HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); context.setAuthCache(authCache);
HttpClientContext localContext = null; try { HttpHost target = new HttpHost( host, port, schema ); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope( target.getHostName(), target.getPort() ), new UsernamePasswordCredentials( user, password ) );
private HttpContext createContext() { final IpCamAuth auth = getAuth(); if (auth == null) { return null; } final URI uri = toURI(url); final HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); final Credentials credentials = new UsernamePasswordCredentials(auth.getUserName(), auth.getPassword()); final CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(AuthScope.ANY, credentials); final AuthCache cache = new BasicAuthCache(); cache.put(host, new BasicScheme()); final HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(provider); context.setAuthCache(cache); return context; }
final HttpHost proxyHost = new HttpHost(host, port); final AuthScope proxyAuthScope = new AuthScope(host, port); final UsernamePasswordCredentials proxyCredential = new UsernamePasswordCredentials(proxyConfig.getProxyUserName(), proxyConfig.getProxyUserPassword()); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(proxyAuthScope, proxyCredential);
HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpclient.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy); && config.getProxyPassword() != null) { httpclient.getCredentialsProvider().setCredentials( new AuthScope(proxyHost, proxyPort), new NTCredentials(config.getProxyUsername(), config.getProxyPassword(),
DefaultHttpClient httpclient = new DefaultHttpClient(); HttpHost proxy = new HttpHost("myproxy.com", 9191); httpclient.getCredentialsProvider().setCredentials( new AuthScope(PROXY, PROXY_PORT), new UsernamePasswordCredentials("username", "password")); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
private HttpContext gethttpContext(Credentials credentials) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, credentials); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); URI stashUri = URI.create(this.apiBaseUrl); authCache.put(new HttpHost(stashUri.getHost(), stashUri.getPort(), stashUri.getScheme()), basicAuth); HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); context.setAuthCache(authCache); RequestConfig config = RequestConfig.copy(context.getRequestConfig()). setConnectTimeout(StashApiClient.HTTP_CONNECTION_TIMEOUT_SECONDS * 1000). setSocketTimeout(StashApiClient.HTTP_SOCKET_TIMEOUT_SECONDS * 1000).build(); context.setRequestConfig(config); return context; }
public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) { this.wxCpConfigStorage = wxConfigProvider; String http_proxy_host = wxCpConfigStorage.getHttp_proxy_host(); int http_proxy_port = wxCpConfigStorage.getHttp_proxy_port(); String http_proxy_username = wxCpConfigStorage.getHttp_proxy_username(); String http_proxy_password = wxCpConfigStorage.getHttp_proxy_password(); if(StringUtils.isNotBlank(http_proxy_host)) { // 使用代理服务器 if(StringUtils.isNotBlank(http_proxy_username)) { // 需要用户认证的代理服务器 CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(http_proxy_host, http_proxy_port), new UsernamePasswordCredentials(http_proxy_username, http_proxy_password)); httpClient = HttpClients .custom() .setDefaultCredentialsProvider(credsProvider) .build(); } else { // 无需用户认证的代理服务器 httpClient = HttpClients.createDefault(); } httpProxy = new HttpHost(http_proxy_host, http_proxy_port); } else { httpClient = HttpClients.createDefault(); } }