List of usage examples for org.apache.http.client.params HttpClientParams setRedirecting
public static void setRedirecting(final HttpParams params, final boolean value)
From source file:at.bitfire.davdroid.webdav.DavHttpClient.java
public static DefaultHttpClient getDefault() { HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.USER_AGENT, "DAVdroid/" + Constants.APP_VERSION); // use defaults of AndroidHttpClient HttpConnectionParams.setConnectionTimeout(params, 20 * 1000); HttpConnectionParams.setSoTimeout(params, 20 * 1000); HttpConnectionParams.setSocketBufferSize(params, 8192); HttpConnectionParams.setStaleCheckingEnabled(params, false); // don't allow redirections HttpClientParams.setRedirecting(params, false); DavHttpClient httpClient = new DavHttpClient(params); // use our own, SNI-capable LayeredSocketFactory for https:// SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry(); schemeRegistry.register(new Scheme("https", new TlsSniSocketFactory(), 443)); // allow gzip compression GzipDecompressingEntity.enable(httpClient); return httpClient; }
From source file:org.zywx.wbpalmstar.engine.eservice.EServiceTest.java
public static void test() { String realyPath = "http://localhost:8000/other.QDV"; HttpRequestBase mHhttpRequest = new HttpGet(realyPath); mHhttpRequest.addHeader("range", "bytes=34199-"); BasicHttpParams bparams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(bparams, 20000); HttpConnectionParams.setSoTimeout(bparams, 20000); HttpConnectionParams.setSocketBufferSize(bparams, 8 * 1024); HttpClientParams.setRedirecting(bparams, true); DefaultHttpClient mDefaultHttpClient = new DefaultHttpClient(bparams); HttpResponse response = null;// w w w .j a v a 2s . c om try { response = mDefaultHttpClient.execute(mHhttpRequest); int responseCode = response.getStatusLine().getStatusCode(); byte[] arrayOfByte = null; HttpEntity httpEntity = response.getEntity(); if (responseCode == 200 || responseCode == 206) { arrayOfByte = toByteArray(httpEntity); String m = new String(arrayOfByte, "UTF-8"); Log.i("ldx", "" + m.length()); Log.i("ldx", m); return; } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.mobicage.rogerthat.util.http.HTTPUtil.java
public static HttpClient getHttpClient(int connectionTimeout, int socketTimeout, final int retryCount) { final HttpParams params = new BasicHttpParams(); HttpConnectionParams.setStaleCheckingEnabled(params, true); HttpConnectionParams.setConnectionTimeout(params, connectionTimeout); HttpConnectionParams.setSoTimeout(params, socketTimeout); HttpClientParams.setRedirecting(params, false); final DefaultHttpClient httpClient = new DefaultHttpClient(params); if (shouldUseTruststore()) { KeyStore trustStore = loadTrustStore(); SSLSocketFactory socketFactory; try {/*from w w w . j a va 2s .c o m*/ socketFactory = new SSLSocketFactory(null, null, trustStore); } catch (Exception e) { throw new RuntimeException(e); } socketFactory.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); Scheme sch = new Scheme("https", socketFactory, CloudConstants.HTTPS_PORT); httpClient.getConnectionManager().getSchemeRegistry().register(sch); } if (retryCount > 0) { httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { return executionCount < retryCount; } }); } return httpClient; }
From source file:it.restrung.rest.misc.HttpClientFactory.java
/** * Private helper to initialize an http client * * @return the initialize http client/*from w w w . j a v a 2 s . c o m*/ */ private static synchronized DefaultHttpClient initializeClient() { //prepare for the https connection //call this in the constructor of the class that does the connection if //it's used multiple times SchemeRegistry schemeRegistry = new SchemeRegistry(); // http scheme schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); // https scheme schemeRegistry.register(new Scheme("https", new FakeSocketFactory(), 443)); HttpParams params; params = new BasicHttpParams(); params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 1); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(1)); params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); HttpClientParams.setRedirecting(params, true); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "utf8"); // ignore that the ssl cert is self signed ThreadSafeClientConnManager clientConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry); instance = new DefaultHttpClient(clientConnectionManager, params); instance.setRedirectHandler(new DefaultRedirectHandler()); //If the link has a redirect return instance; }
From source file:com.moarub.util.PageTitleGetter.java
private StringBuilder doGetTitle(String... params) { Log.d("Resolving title", "URL " + params[0]); fURLTo = params[0];/* www. j ava2 s.co m*/ fBackupTitle = params[1]; AndroidHttpClient httpClient = AndroidHttpClient.newInstance("Android ShareMore"); HttpClientParams.setRedirecting(httpClient.getParams(), true); HttpGet headReq = new HttpGet(fURLTo); try { HttpResponse result = httpClient.execute(headReq); return ShareMoreUtils.getResponseString(result); } catch (IOException e) { Log.d("Resolving title Exception", e.getMessage()); return null; } finally { httpClient.close(); } }
From source file:org.pixmob.appengine.client.SSLEnabledHttpClient.java
public static SSLEnabledHttpClient newInstance(String userAgent) { // the following code comes from AndroidHttpClient (API level 10) final HttpParams params = new BasicHttpParams(); // Turn off stale checking. Our connections break all the time anyway, // and it's not worth it to pay the penalty of checking every time. HttpConnectionParams.setStaleCheckingEnabled(params, false); final int timeout = 60 * 1000; HttpConnectionParams.setConnectionTimeout(params, timeout); HttpConnectionParams.setSoTimeout(params, timeout); HttpConnectionParams.setSocketBufferSize(params, 8192); // Don't handle redirects -- return them to the caller. Our code // often wants to re-POST after a redirect, which we must do ourselves. HttpClientParams.setRedirecting(params, false); // Set the specified user agent and register standard protocols. HttpProtocolParams.setUserAgent(params, userAgent); // Prevent UnknownHostException error with 3G connections: // http://stackoverflow.com/questions/2052299/httpclient-on-android-nohttpresponseexception-through-umts-3g HttpProtocolParams.setUseExpectContinue(params, false); final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", sslSocketFactory, 443)); final ClientConnectionManager manager = new SingleClientConnManager(params, schemeRegistry); final SSLEnabledHttpClient client = new SSLEnabledHttpClient(manager, params); client.addRequestInterceptor(new GzipRequestInterceptor()); client.addResponseInterceptor(new GzipResponseInterceptor()); return client; }
From source file:com.dajodi.scandic.ScandicSessionHelper.java
public static InputStream get(URI uri) { DefaultHttpClient client = Singleton.INSTANCE.getHttpClient(); try {/* w ww .j a va 2 s . c om*/ HttpGet get = new HttpGet(uri); Util.gzipify(get); final HttpParams params = new BasicHttpParams(); HttpClientParams.setRedirecting(params, false); get.setParams(params); Log.d("Executing get"); // should give us a 302 HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() != 200) { throw new ScandicHtmlException("Expected a 200, got " + response.getStatusLine()); } InputStream instream = Util.ungzip(response); return instream; } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } else { throw new RuntimeException(e); } } }
From source file:ch.lipsch.deshortener.Deshortener.java
/** * Deshortens the provided uri.//from w w w.j a v a 2 s . co m * * @param uriToDeshorten * The uri to deshorten. * @return Returns the result of the deshorten process. The result is only * successful when the given uri returns an 30x. Then value of the * Location header is returned within the result. * @throws IllegalArgumentException * If the provided uri is invalid. */ public static Result deshorten(Uri uriToDeshorten) { // Checks if the url shortener shows a preview if (checkForPreview(uriToDeshorten)) { return new Result(ResultType.SHOWS_PREVIEW); } // Open the network connetion HttpGet get = new HttpGet(uriToDeshorten.toString()); DefaultHttpClient client = new DefaultHttpClient(); HttpParams clientParams = client.getParams(); HttpClientParams.setRedirecting(clientParams, false); HttpResponse response = null; try { response = client.execute(get); } catch (ClientProtocolException e) { Log.e(LOG_TAG, "Unable to communicate to shortened url: " + uriToDeshorten.toString(), e); return new Result(ResultType.NETWORK_ERROR); } catch (IOException e) { Log.e(LOG_TAG, "Unable to communicate to shortened url: " + uriToDeshorten.toString(), e); return new Result(ResultType.NETWORK_ERROR); } // Check for redirection boolean isRedirection = ((response.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY) || (response.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) || (response.getStatusLine().getStatusCode() == HttpStatus.SC_SEE_OTHER)); if (response != null && isRedirection) { Header header = response.getFirstHeader("Location"); String redirectValue = header.getValue(); Uri deshortenedUri = Uri.parse(redirectValue); return new Result(deshortenedUri); } else { return new Result(ResultType.CANNOT_DESHORTEN); } }
From source file:outfox.ynote.open.client.YNoteHttpUtils.java
/** * Do a http get for the given url./*from w w w .j a v a 2 s. co m*/ * * @param url requested url * @param parameters request parameters * @param accessor oauth accessor * @return the http response * @throws IOException * @throws {@link YNoteException} */ public static HttpResponse doGet(String url, Map<String, String> parameters, OAuthAccessor accessor) throws IOException, YNoteException { // add ynote parameters to the url OAuth.addParameters(url, parameters == null ? null : parameters.entrySet()); HttpGet get = new HttpGet(url); // sign all parameters, including oauth parameters and ynote parameters // and add the oauth related information into the header Header oauthHeader = getAuthorizationHeader(url, OAuthMessage.GET, parameters, accessor); get.addHeader(oauthHeader); HttpParams params = new BasicHttpParams(); HttpClientParams.setRedirecting(params, false); get.setParams(params); HttpResponse response = client.execute(get); if ((response.getStatusLine().getStatusCode() / 100) != 2) { YNoteException e = wrapYNoteException(response); throw e; } return response; }
From source file:com.dubsar_dictionary.SecureClient.SecureAndroidHttpClient.java
/** * Create a new HttpClient with reasonable defaults (which you can update). * (Lifted and modified from AndroidHttpClient.) * * @param userAgent to report in your HTTP requests * @param context to use for caching SSL sessions (may be null for no caching) * @return AndroidHttpClient for you to use for all your requests. *///w ww . j ava 2 s . co m public static HttpClient newInstance(String userAgent) { Log.d(TAG, "Creating new client instance"); HttpParams params = new BasicHttpParams(); // Turn off stale checking. Our connections break all the time anyway, // and it's not worth it to pay the penalty of checking every time. HttpConnectionParams.setStaleCheckingEnabled(params, false); HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT); HttpConnectionParams.setSocketBufferSize(params, 8192); // Default to following redirects HttpClientParams.setRedirecting(params, true); // Set the specified user agent and register standard protocols. HttpProtocolParams.setUserAgent(params, userAgent); SchemeRegistry schemeRegistry = new SchemeRegistry(); SSLSocketFactory sf = SecureSocketFactory.getHttpSocketFactory(SOCKET_OPERATION_TIMEOUT); schemeRegistry.register(new Scheme("https", sf, 443)); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry); // We use a factory method to modify superclass initialization // parameters without the funny call-a-static-method dance. return new SecureAndroidHttpClient(manager, params); }