private synchronized void setHeaders(Collection<BasicHeader> headers) { if (headers != null && headers.size() != 0){ @SuppressWarnings("unchecked") Collection<BasicHeader> preHeaders = (Collection<BasicHeader>) mHttpClient.getParams().getParameter(ClientPNames.DEFAULT_HEADERS); if (preHeaders == null){ preHeaders = new ArrayList<BasicHeader>(); } for(BasicHeader bh:headers){ for(BasicHeader bh1:preHeaders){ if(bh.getName().equals(bh1.getName())){ preHeaders.remove(bh1); break; } } if (bh.getValue() != null){ preHeaders.add(bh); } } } }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (request == null) { throw new IllegalArgumentException("HTTP request may not be null"); } String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT")) { return; } // Add default headers @SuppressWarnings("unchecked") Collection<Header> defHeaders = (Collection<Header>) request.getParams().getParameter( ClientPNames.DEFAULT_HEADERS); if (defHeaders != null) { for (Header defHeader : defHeaders) { request.addHeader(defHeader); } } }
private byte[] downloadHTTPfile_post(String formToDownloadLocation, List<NameValuePair> params) throws IOException, NullPointerException, URISyntaxException { BasicHttpContext localContext = new BasicHttpContext(); LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState); if (this.mimicWebDriverCookieState) { localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies())); } HttpPost httppost = new HttpPost(formToDownloadLocation); HttpParams httpRequestParameters = httppost.getParams(); httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects); httppost.setParams(httpRequestParameters); httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); LOG.info("Sending POST request for: " + httppost.getURI()); @SuppressWarnings("resource") HttpResponse response = new DefaultHttpClient().execute(httppost, localContext); this.httpStatusOfLastDownloadAttempt = response.getStatusLine().getStatusCode(); LOG.info("HTTP GET request status: " + this.httpStatusOfLastDownloadAttempt); byte[] file = IOUtils.toByteArray(response.getEntity().getContent()); response.getEntity().getContent().close(); return file; }
/** * Perform an HTTP Status check and return the response code * * @return * @throws IOException */ @SuppressWarnings("resource") public int getHTTPStatusCode() throws IOException { HttpClient client = new DefaultHttpClient(); BasicHttpContext localContext = new BasicHttpContext(); LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState); if (this.mimicWebDriverCookieState) { localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies())); } HttpRequestBase requestMethod = this.httpRequestMethod.getRequestMethod(); requestMethod.setURI(this.linkToCheck); HttpParams httpRequestParameters = requestMethod.getParams(); httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects); requestMethod.setParams(httpRequestParameters); LOG.info("Sending " + requestMethod.getMethod() + " request for: " + requestMethod.getURI()); HttpResponse response = client.execute(requestMethod, localContext); LOG.info("HTTP " + requestMethod.getMethod() + " request status: " + response.getStatusLine().getStatusCode()); return response.getStatusLine().getStatusCode(); }
/** * 此处解释下MaxtTotal和DefaultMaxPerRoute的区别: * 1、MaxtTotal是整个池子的大小; * 2、DefaultMaxPerRoute是根据连接到的主机对MaxTotal的一个细分;比如: * MaxtTotal=400 DefaultMaxPerRoute=200 * 而我只连接到http://sishuok.com时,到这个主机的并发最多只有200;而不是400; * 而我连接到http://sishuok.com 和 http://qq.com时,到每个主机的并发最多只有200;即加起来是400(但不能超过400);所以起作用的设置是DefaultMaxPerRoute。 */ public HttpParams getHttpParams() { HttpParams params = new BasicHttpParams(); // 设置连接超时时间 Integer CONNECTION_TIMEOUT = 2 * 1000; // 设置请求超时2秒钟 根据业务调整 Integer SO_TIMEOUT = 2 * 1000; // 设置等待数据超时时间2秒钟 根据业务调整 // 定义了当从ClientConnectionManager中检索ManagedClientConnection实例时使用的毫秒级的超时时间 // 这个参数期望得到一个java.lang.Long类型的值。如果这个参数没有被设置,默认等于CONNECTION_TIMEOUT,因此一定要设置 Long CONN_MANAGER_TIMEOUT = 500L; // 该值就是连接不够用的时候等待超时时间,一定要设置,而且不能太大 () params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT); // 在提交请求之前 测试连接是否可用 params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true); return params; }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { Args.notNull(request, "HTTP request"); final String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT")) { return; } // Add default headers @SuppressWarnings("unchecked") Collection<? extends Header> defHeaders = (Collection<? extends Header>) request.getParams().getParameter(ClientPNames.DEFAULT_HEADERS); if (defHeaders == null) { defHeaders = this.defaultHeaders; } if (defHeaders != null) { for (final Header defHeader : defHeaders) { request.addHeader(defHeader); } } }
private void createHttpClient(NaviPoolConfig poolConfig, ServerUrlUtil.ServerUrl server) { if (params == null) { // httpClient.setParams(params)HttpParams httpClient = new DefaultHttpClient(cm); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, poolConfig.getSocketTimeout()); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, poolConfig.getConnectTimeout()); } else { httpClient = new DefaultHttpClient(cm, params); if (poolConfig instanceof NaviHttpPoolConfig) { httpClient.setHttpRequestRetryHandler( new NaviHttpRequestRetryHandler(((NaviHttpPoolConfig) poolConfig).getRetryTimes(), false) ); } } // 配置数据源 httpClient.getParams().setParameter(ClientPNames.DEFAULT_HOST, new HttpHost(server.getHost(), server.getPort())); }
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { Args.notNull(request, "HTTP request"); final String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT")) { return; } // Add default headers @SuppressWarnings("unchecked") Collection<? extends Header> defHeaders = (Collection<? extends Header>) request.getParams().getParameter(ClientPNames.DEFAULT_HEADERS); if (defHeaders == null) { defHeaders = this.defaultHeaders; } if (defHeaders != null) { for (final Header defHeader : defHeaders) { request.addHeader(defHeader); } } }
@Test public void testSetParams() { ModifiableSolrParams params = new ModifiableSolrParams(); params.set(HttpClientUtil.PROP_ALLOW_COMPRESSION, true); params.set(HttpClientUtil.PROP_BASIC_AUTH_PASS, "pass"); params.set(HttpClientUtil.PROP_BASIC_AUTH_USER, "user"); params.set(HttpClientUtil.PROP_CONNECTION_TIMEOUT, 12345); params.set(HttpClientUtil.PROP_FOLLOW_REDIRECTS, true); params.set(HttpClientUtil.PROP_MAX_CONNECTIONS, 22345); params.set(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, 32345); params.set(HttpClientUtil.PROP_SO_TIMEOUT, 42345); params.set(HttpClientUtil.PROP_USE_RETRY, false); DefaultHttpClient client = (DefaultHttpClient) HttpClientUtil.createClient(params); assertEquals(12345, HttpConnectionParams.getConnectionTimeout(client.getParams())); assertEquals(PoolingClientConnectionManager.class, client.getConnectionManager().getClass()); assertEquals(22345, ((PoolingClientConnectionManager)client.getConnectionManager()).getMaxTotal()); assertEquals(32345, ((PoolingClientConnectionManager)client.getConnectionManager()).getDefaultMaxPerRoute()); assertEquals(42345, HttpConnectionParams.getSoTimeout(client.getParams())); assertEquals(HttpClientUtil.NO_RETRY, client.getHttpRequestRetryHandler()); assertEquals("pass", client.getCredentialsProvider().getCredentials(new AuthScope("127.0.0.1", 1234)).getPassword()); assertEquals("user", client.getCredentialsProvider().getCredentials(new AuthScope("127.0.0.1", 1234)).getUserPrincipal().getName()); assertEquals(true, client.getParams().getParameter(ClientPNames.HANDLE_REDIRECTS)); client.getConnectionManager().shutdown(); }
@Override public void init() throws ServletException { String doLogStr = getConfigParam(P_LOG); if (doLogStr != null) { this.doLog = Boolean.parseBoolean(doLogStr); } String doForwardIPString = getConfigParam(P_FORWARDEDFOR); if (doForwardIPString != null) { this.doForwardIP = Boolean.parseBoolean(doForwardIPString); } initTarget();//sets target* HttpParams hcParams = new BasicHttpParams(); hcParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES); readConfigParam(hcParams, ClientPNames.HANDLE_REDIRECTS, Boolean.class); proxyClient = createHttpClient(hcParams); }
public void prepareForBrowser() { // Clear cookies, let the browser handle them httpClient.setCookieStore(new BlankCookieStore()); httpClient.getCookieSpecs().register("easy", new CookieSpecFactory() { @Override public CookieSpec newInstance(final HttpParams params) { return new BrowserCompatSpec() { @Override public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException { // easy! } }; } }); httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, "easy"); decompress = false; setFollowRedirects(false); }
@Override public void init() throws ServletException { String doLogStr = getConfigParam(P_LOG); if (doLogStr != null) { this.doLog = Boolean.parseBoolean(doLogStr); } String doForwardIPString = getConfigParam(P_FORWARDEDFOR); if (doForwardIPString != null) { this.doForwardIP = Boolean.parseBoolean(doForwardIPString); } initTarget();// sets target* HttpParams hcParams = new BasicHttpParams(); hcParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES); readConfigParam(hcParams, ClientPNames.HANDLE_REDIRECTS, Boolean.class); proxyClient = createHttpClient(hcParams); }
/** * 获取DefaultHttpClient对象 * * @param charset * 字符编码 * @return DefaultHttpClient对象 */ private static DefaultHttpClient getDefaultHttpClient(final String charset) { DefaultHttpClient httpclient = new DefaultHttpClient(); // 模拟浏览器,解决一些服务器程序只允许浏览器访问的问题 httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT); httpclient.getParams().setParameter( CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE); httpclient.getParams().setParameter( CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset == null ? CHARSET_ENCODING : charset); // 浏览器兼容性 httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // 定义重试策略 httpclient.setHttpRequestRetryHandler(requestRetryHandler); return httpclient; }
public void init() { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80)); HttpParams params = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(params, 10); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); httpClient = new DefaultHttpClient(cm, params); // 重试 // httpClient.setHttpRequestRetryHandler(new // DefaultHttpRequestRetryHandler(3, false)); // 超时设置 // httpClient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, // 10000); // httpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, // 10000); httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, true); threadNum = 10; execService = Executors.newFixedThreadPool(threadNum); destroy = false; }
public String getRedirectUrl(String url) { httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false); HttpGet httpget = new HttpGet(url); httpget.setHeader("User-Agent", userAgent); httpget.addHeader("Accept-Encoding", "gzip, deflate"); String newUrl; try { HttpResponse response = httpClient.execute(httpget); Header locationHeader = response.getLastHeader("Location"); newUrl = locationHeader.getValue(); } catch (IOException e) { Log.d(TAG, "get url failed,", e); newUrl = null; } httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, true); return newUrl; }
/** * <h1>WordPress constructor. After initializing this constructor, you can * call several core methods: login(Bundle userData), register(Bundle * userData)</h1> * * @param context * Pass the application's context to get this initialized. */ public Wordpress(Context context, OnConnectionFailureListener listener) { this.context = context; API = context.getString(R.string.api); BASE_URL = context.getString(R.string.url) + "/" + API + "/"; httpClient.getHttpClient().getParams() .setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); postHandler = new WordpressResponseHandler<WPPost>(); commentHandler = new WordpressResponseHandler<WPComment>(); onConnectionFailureListener = listener; postHandler.setOnConnectionFailureListener(onConnectionFailureListener); commentHandler .setOnConnectionFailureListener(onConnectionFailureListener); }
@SuppressWarnings("deprecation") @Test public void testCookiePolicy() { SpringClientFactory factory = new SpringClientFactory(); AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); addEnvironment(parent, "ribbon.restclient.enabled=true"); parent.register(RibbonAutoConfiguration.class, ArchaiusAutoConfiguration.class); parent.refresh(); factory.setApplicationContext(parent); RestClient client = factory.getClient("foo", RestClient.class); ApacheHttpClient4 jerseyClient = (ApacheHttpClient4) client.getJerseyClient(); assertEquals(CookiePolicy.IGNORE_COOKIES, jerseyClient.getClientHandler() .getHttpClient().getParams().getParameter(ClientPNames.COOKIE_POLICY)); parent.close(); factory.destroy(); }
private void prepairCookieStore(DefaultHttpClient client) { client.setCookieStore(cookieStore); CookieSpecFactory csf = new CookieSpecFactory() { public CookieSpec newInstance(HttpParams params) { return new BrowserCompatSpec() { @Override public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException { // not filtering; accept all cookies Log.d(this.toString(),"cookies"); } }; } }; client.getCookieSpecs().register("all", csf); client.getParams().setParameter( ClientPNames.COOKIE_POLICY, "all"); }
private HttpResponse getHTTPResponse() throws IOException, NullPointerException { if (fileURI == null) throw new NullPointerException("No file URI specified"); HttpClient client = new DefaultHttpClient(); BasicHttpContext localContext = new BasicHttpContext(); //Clear down the local cookie store every time to make sure we don't have any left over cookies influencing the test localContext.setAttribute(ClientContext.COOKIE_STORE, null); System.out.println("Mimic WebDriver cookie state: " + mimicWebDriverCookieState); if (mimicWebDriverCookieState) { localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(driver.manage().getCookies())); } HttpRequestBase requestMethod = httpRequestMethod.getRequestMethod(); requestMethod.setURI(fileURI); HttpParams httpRequestParameters = requestMethod.getParams(); httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects); requestMethod.setParams(httpRequestParameters); //TODO if post send map of variables, also need to add a post map setter System.out.println("Sending " + httpRequestMethod.toString() + " request for: " + fileURI); return client.execute(requestMethod, localContext); }
public synchronized HttpClient getClient() { if (m_client == null) { m_client = new DefaultHttpClient(); HttpParams clientParams = m_client.getParams(); clientParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, m_timeout); clientParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, m_timeout); clientParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); m_client.setParams(clientParams); m_client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(m_retries, false)); } return m_client; }
private static HttpParams buildParams(final HttpCollectionSet collectionSet) { HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, computeVersion(collectionSet.getUriDef())); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Integer.parseInt(ParameterMap.getKeyedString(collectionSet.getParameters(), "timeout", DEFAULT_SO_TIMEOUT))); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, Integer.parseInt(ParameterMap.getKeyedString(collectionSet.getParameters(), "timeout", DEFAULT_SO_TIMEOUT))); params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); //review the httpclient code, looks like virtual host is checked for null //and if true, sets Host to the connection's host property String virtualHost = collectionSet.getUriDef().getUrl().getVirtualHost(); if (virtualHost != null) { params.setParameter( ClientPNames.VIRTUAL_HOST, new HttpHost(virtualHost, collectionSet.getPort()) ); } return params; }
private void loadCookie(String authToken) throws AuthenticationException, IOException { String url = AUTH_COOKIE_URL.buildUpon().appendQueryParameter("auth", authToken).build().toString(); HttpGet method = new HttpGet(url); httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); HttpResponse res = httpClient.execute(method, httpContext); Header[] headers = res.getHeaders("Set-Cookie"); if (res.getEntity() != null) { res.getEntity().consumeContent(); } if (res.getStatusLine().getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY || headers.length == 0) { throw new AuthenticationException("Failed to get cookie"); } if (!hasValidAuthCookie()) throw new AuthenticationException("Failed to get cookie"); }
private synchronized InputStream doRequest(String url) throws IOException { if (verbose) { System.out.println("Making HTTP request: " + url); } final HttpParams httpParams = new BasicHttpParams(); HttpClient client = new DefaultHttpClient(httpParams); if (proxyIP != null) { HttpHost proxy = new HttpHost(proxyIP, proxyPort); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } HttpConnectionParams.setConnectionTimeout(client.getParams(), (int) timeout); HttpConnectionParams.setSoTimeout(client.getParams(), (int) timeout); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, this.userAgent); client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); if (verbose) { System.out.println("Status code for request: " + response.getStatusLine().getStatusCode()); } if (response.getStatusLine().getStatusCode() == 429) { hardRateLimit = System.currentTimeMillis() + 900000L; //15 min throw new RateLimitReachedException(); } return response.getEntity().getContent(); }
private DefaultHttpClient getHttpClient() { DefaultHttpClient client = new DefaultHttpClient(); // I believe DefaultHttpClient handles redirects by default anyway client.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.TRUE); client.addRequestInterceptor(new HttpRequestInterceptor() { public void process( final HttpRequest request, final HttpContext context) throws HttpException, IOException { request.addHeader("Accept", "application/xml"); } }); SSLSocketFactory.getSocketFactory().setHostnameVerifier( SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER ); return client; }
@Test @DatabaseSetup("/database_seed.xml") public void the_approval_will_be_remembered() throws IOException { givenValidAuthCode("marissa", "koala", "internal"); givenAuthCode(); givenAccessTokenUsingAuthCode(); { HttpGet httpGet = new HttpGet(loginUri); httpGet.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.NETSCAPE); httpGet.getParams().setBooleanParameter("http.protocol.handle-redirects", false); authCodeResponse = httpClient.execute(httpGet); httpGet.releaseConnection(); } givenAuthCode(); givenAccessTokenUsingAuthCode(); assertTrue(accessToken != null); assertNotNull(accessToken.getRefreshToken()); }
@Test @DatabaseSetup("/database_seeds/LoginOAuth2IT/database_seed_zero_validity.xml") public void the_approval_will_not_be_remembered_if_validity_is_zero() throws IOException { givenValidAuthCode("marissa", "koala", "internal"); givenAuthCode(); givenAccessTokenUsingAuthCode(); HttpGet httpGet = new HttpGet(loginUri); httpGet.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.NETSCAPE); httpGet.getParams().setBooleanParameter("http.protocol.handle-redirects", false); authCodeResponse = httpClient.execute(httpGet); String response = IOUtils.toString(authCodeResponse.getEntity().getContent()); httpGet.releaseConnection(); assertThat(response, containsString("<title>Access confirmation</title>")); }
public static void reqArrayNoSSL(String url, Context parent, ModelCallback<JSONArray> callback) { RequestAsyncTask<JSONArray> task = new RequestAsyncTask<JSONArray>( "get", url, null, false, false, parent, callback) { @Override protected JSONArray toJSON(String responseStr) throws JSONException { return new JSONArray(responseStr); } }; task.client = new DefaultHttpClient(); task.client.getParams().setParameter( ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); task.headers = new HashMap<String, String>(); task.headers.put("User-Agent", BROWSER_LIKE_USER_AGENT); executeOnExecutor(task, HIGH_PRIORITY_EXECUTOR); }
/** * Returns a client with all our selected properties / params. * * @return client */ public static final DefaultHttpClient getClient() { // create a singular HttpClient object DefaultHttpClient client = new DefaultHttpClient(connectionManager); // dont retry client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); // get the params for the client HttpParams params = client.getParams(); // establish a connection within x seconds params.setParameter(CoreConnectionPNames.SO_TIMEOUT, connectionTimeout); // no redirects params.setParameter(ClientPNames.HANDLE_REDIRECTS, false); // set custom ua params.setParameter(CoreProtocolPNames.USER_AGENT, userAgent); // set the proxy if the user has one set if ((System.getProperty("http.proxyHost") != null) && (System.getProperty("http.proxyPort") != null)) { HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost").toString(), Integer.valueOf(System.getProperty("http.proxyPort"))); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } return client; }
/** * Start a HTTP Session with authorisation * * @param username * @param password */ public void startSession(String username, String password) { // Create your httpclient client = new DefaultHttpClient(); client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); // Then provide the right credentials client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password)); // Generate BASIC scheme object and stick it to the execution context basicAuth = new BasicScheme(); context = new BasicHttpContext(); context.setAttribute("preemptive-auth", basicAuth); // Add as the first (because of the zero) request interceptor // It will first intercept the request and preemptively initialize the authentication scheme if there is not client.addRequestInterceptor(new PreemptiveAuth(), 0); }
private DefaultHttpClient createClient(boolean https) { final DefaultHttpClient client = (https ? new DefaultHttpClient(conMan) : new DefaultHttpClient()); // Allows a slightly lenient cookie acceptance client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // Allows follow of redirects on POST client.setRedirectStrategy(new LaxRedirectStrategy()); return client; }
private static int httpNotification(String uri, int timeout) throws IOException, URISyntaxException { DefaultHttpClient client = new DefaultHttpClient(); client.getParams() .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout) .setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, (long) timeout); HttpGet httpGet = new HttpGet(new URI(uri)); httpGet.setHeader("Accept", "*/*"); return client.execute(httpGet).getStatusLine().getStatusCode(); }
/** * Setup some variables */ public void setup() { log.info("Setup..."); if (StringUtils.isEmpty(this.username)) { throw new IllegalArgumentException("Username is mandatory."); } if (StringUtils.isEmpty(this.password)) { throw new IllegalArgumentException("Password is mandatory."); } this.deviceId = InstagramHashUtil.generateDeviceId(this.username, this.password); if (StringUtils.isEmpty(this.uuid)) { this.uuid = InstagramGenericUtil.generateUuid(true); } if (this.cookieStore == null) { this.cookieStore = new BasicCookieStore(); } log.info("Device ID is: " + this.deviceId + ", random id: " + this.uuid); this.client = new DefaultHttpClient(); this.client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); this.client.setCookieStore(this.cookieStore); }