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

The following examples show how to use org.apache.http.conn.params.ConnRouteParams. 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: TestProxy.java    From albert with MIT License 5 votes vote down vote up
public static HttpClient getHttpClient() {

	 DefaultHttpClient httpClient = new DefaultHttpClient();
	 String proxyHost = "127.0.0.1";
	 int proxyPort = 1080;
//	 String userName = "111";
//	 String password = "111";
//	 httpClient.getCredentialsProvider().setCredentials(
//	   new AuthScope(proxyHost, proxyPort),
//	   new UsernamePasswordCredentials(userName, password));
	 HttpHost proxy = new HttpHost(proxyHost,proxyPort);
	 httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
	 return httpClient;
	}
 
Example #2
Source File: ProxyAwareTransportFactory.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public HttpTransport create() {

    if (proxyConfig == null) {
        return DEFAULT_TRANSPORT;
    }

    final Proxy proxy = proxyConfig.createProxy();

    if (Proxy.Type.HTTP.equals(proxy.type()) && proxyConfig.hasCredential()) {
        // If it requires authentication via username and password, use ApacheHttpTransport
        final String host = proxyConfig.getProxyServerHost();
        final int port = proxyConfig.getProxyServerPort();
        final HttpHost proxyHost = new HttpHost(host, port);

        final DefaultHttpClient httpClient = new DefaultHttpClient();
        ConnRouteParams.setDefaultProxy(httpClient.getParams(), proxyHost);

        if (proxyConfig.hasCredential()) {
            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);
            httpClient.setCredentialsProvider(credentialsProvider);
        }

        return new ApacheHttpTransport(httpClient);

    }

    return new NetHttpTransport.Builder().setProxy(proxy).build();
}
 
Example #3
Source File: HttpTracker.java    From bt with Apache License 2.0 4 votes vote down vote up
private static HttpClient buildClient(InetAddress localAddress) {
    HttpClient client = HttpClients.createMinimal();
    client.getParams().setParameter(ConnRouteParams.LOCAL_ADDRESS, localAddress);
    return client;
}
 
Example #4
Source File: DownloadThread.java    From travelguide with Apache License 2.0 4 votes vote down vote up
/**
 * Executes the download in a separate thread
 */
public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    State state = new State(mInfo, mService);
    AndroidHttpClient client = null;
    PowerManager.WakeLock wakeLock = null;
    int finalStatus = DownloaderService.STATUS_UNKNOWN_ERROR;

    try {
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
        wakeLock.acquire();

        if (Constants.LOGV) {
            Log.v(Constants.TAG, "initiating download for " + mInfo.mFileName);
            Log.v(Constants.TAG, "  at " + mInfo.mUri);
        }

        client = AndroidHttpClient.newInstance(userAgent(), mContext);

        boolean finished = false;
        while (!finished) {
            if (Constants.LOGV) {
                Log.v(Constants.TAG, "initiating download for " + mInfo.mFileName);
                Log.v(Constants.TAG, "  at " + mInfo.mUri);
            }
            // Set or unset proxy, which may have changed since last GET
            // request.
            // setDefaultProxy() supports null as proxy parameter.
            ConnRouteParams.setDefaultProxy(client.getParams(),
                    getPreferredHttpHost(mContext, state.mRequestUri));
            HttpGet request = new HttpGet(state.mRequestUri);
            try {
                executeDownload(state, client, request);
                finished = true;
            } catch (RetryDownload exc) {
                // fall through
            } finally {
                request.abort();
                request = null;
            }
        }

        if (Constants.LOGV) {
            Log.v(Constants.TAG, "download completed for " + mInfo.mFileName);
            Log.v(Constants.TAG, "  at " + mInfo.mUri);
        }
        finalizeDestinationFile(state);
        finalStatus = DownloaderService.STATUS_SUCCESS;
    } catch (StopRequest error) {
        // remove the cause before printing, in case it contains PII
        Log.w(Constants.TAG,
                "Aborting request for download " + mInfo.mFileName + ": " + error.getMessage());
        error.printStackTrace();
        finalStatus = error.mFinalStatus;
        // fall through to finally block
    } catch (Throwable ex) { // sometimes the socket code throws unchecked
                             // exceptions
        Log.w(Constants.TAG, "Exception for " + mInfo.mFileName + ": " + ex);
        finalStatus = DownloaderService.STATUS_UNKNOWN_ERROR;
        // falls through to the code that reports an error
    } finally {
        if (wakeLock != null) {
            wakeLock.release();
            wakeLock = null;
        }
        if (client != null) {
            client.close();
            client = null;
        }
        cleanupDestination(state, finalStatus);
        notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter,
                state.mRedirectCount, state.mGotData, state.mFilename);
    }
}
 
Example #5
Source File: DownloadThread.java    From Alite with GNU General Public License v3.0 4 votes vote down vote up
/**
    * Executes the download in a separate thread
    */
   @SuppressLint("Wakelock")
public void run() {
       Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

       State state = new State(mInfo, mService);
       AndroidHttpClient client = null;
       PowerManager.WakeLock wakeLock = null;
       int finalStatus = DownloaderService.STATUS_UNKNOWN_ERROR;

       try {
           PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
           wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
           wakeLock.acquire();

           if (Constants.LOGV) {
               Log.v(Constants.TAG, "initiating download for " + mInfo.mFileName);
               Log.v(Constants.TAG, "  at " + mInfo.mUri);
           }

           client = AndroidHttpClient.newInstance(userAgent(), mContext);

           boolean finished = false;
           while (!finished) {
               if (Constants.LOGV) {
                   Log.v(Constants.TAG, "initiating download for " + mInfo.mFileName);
                   Log.v(Constants.TAG, "  at " + mInfo.mUri);
               }
               // Set or unset proxy, which may have changed since last GET
               // request.
               // setDefaultProxy() supports null as proxy parameter.
               ConnRouteParams.setDefaultProxy(client.getParams(),
                       getPreferredHttpHost(mContext, state.mRequestUri));
               HttpGet request = new HttpGet(state.mRequestUri);
               try {
                   executeDownload(state, client, request);
                   finished = true;
               } catch (RetryDownload exc) {
                   // fall through
               } finally {
                   request.abort();
                   request = null;
               }
           }

           if (Constants.LOGV) {
               Log.v(Constants.TAG, "download completed for " + mInfo.mFileName);
               Log.v(Constants.TAG, "  at " + mInfo.mUri);
           }
           finalizeDestinationFile(state);
           finalStatus = DownloaderService.STATUS_SUCCESS;
       } catch (StopRequest error) {
           // remove the cause before printing, in case it contains PII
           Log.w(Constants.TAG,
                   "Aborting request for download " + mInfo.mFileName + ": " + error.getMessage());
           error.printStackTrace();
           finalStatus = error.mFinalStatus;
           // fall through to finally block
       } catch (Throwable ex) { // sometimes the socket code throws unchecked
                                // exceptions
           Log.w(Constants.TAG, "Exception for " + mInfo.mFileName + ": " + ex);
           finalStatus = DownloaderService.STATUS_UNKNOWN_ERROR;
           // falls through to the code that reports an error
       } finally {
           if (wakeLock != null) {
               wakeLock.release();
               wakeLock = null;
           }
           if (client != null) {
               client.close();
               client = null;
           }
           cleanupDestination(state, finalStatus);
           notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter,
                   state.mRedirectCount, state.mGotData, state.mFilename);
       }
   }
 
Example #6
Source File: ApacheHttpTransport.java    From google-http-java-client with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the HTTP proxy to use {@link DefaultHttpRoutePlanner} or {@code null} to use {@link
 * #setProxySelector(ProxySelector)} with {@link ProxySelector#getDefault()}.
 *
 * <p>By default it is {@code null}, which uses the proxy settings from <a
 * href="http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
 * properties</a>.
 *
 * <p>For example:
 *
 * <pre>
 * setProxy(new HttpHost("127.0.0.1", 8080))
 * </pre>
 */
public Builder setProxy(HttpHost proxy) {
  ConnRouteParams.setDefaultProxy(params, proxy);
  if (proxy != null) {
    proxySelector = null;
  }
  return this;
}
 
Example #7
Source File: ApacheHttpTransport.java    From google-http-java-client with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the HTTP proxy selector to use {@link ProxySelectorRoutePlanner} or {@code null} for
 * {@link DefaultHttpRoutePlanner}.
 *
 * <p>By default it is {@link ProxySelector#getDefault()} which uses the proxy settings from <a
 * href="http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
 * properties</a>.
 */
public Builder setProxySelector(ProxySelector proxySelector) {
  this.proxySelector = proxySelector;
  if (proxySelector != null) {
    ConnRouteParams.setDefaultProxy(params, null);
  }
  return this;
}