org.apache.http.client.params.ClientPNames Java Examples

The following examples show how to use org.apache.http.client.params.ClientPNames. 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: HttpHelper.java    From Huochexing12306 with Apache License 2.0 6 votes vote down vote up
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);
			}
		}
	}
}
 
Example #2
Source File: GetClient.java    From javabase with Apache License 2.0 6 votes vote down vote up
/**
 * 此处解释下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;
}
 
Example #3
Source File: ApiProxyServlet.java    From onboard with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: HttpClientUtil.java    From AndroidRobot with Apache License 2.0 6 votes vote down vote up
/** 
 * 获取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;  
}
 
Example #5
Source File: LocalFileModel.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Deprecated
public LocalFileModel( final String url,
                       final HttpClient client,
                       final String username,
                       final String password ) {
  if ( url == null ) {
    throw new NullPointerException();
  }
  this.url = url;
  this.username = username;
  this.password = password;
  this.client = client;
  this.client.getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY );
  this.client.getParams().setParameter( ClientPNames.MAX_REDIRECTS, Integer.valueOf( 10 ) );
  this.client.getParams().setParameter( ClientPNames.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE );
  this.client.getParams().setParameter( ClientPNames.REJECT_RELATIVE_REDIRECT, Boolean.FALSE );
  this.context = HttpClientContext.create();
}
 
Example #6
Source File: BuddycloudHTTPHelper.java    From buddycloud-android with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #7
Source File: JobEndNotifier.java    From hadoop with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #8
Source File: JobEndNotifier.java    From big-c with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #9
Source File: DepositFilesAccount.java    From neembuu-uploader with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set uprand cookie.
 * @throws IOException 
 */
public void setUprandCookie() throws IOException {
    httpGet = new NUHttpGet("http://www.dfiles.eu/");
    HttpParams params = new BasicHttpParams();
    params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
    httpGet.setParams(params);
    httpResponse = httpclient.execute(httpGet, httpContext);
    EntityUtils.toString(httpResponse.getEntity()); //Consume content
    
    uprandcookie = CookieUtils.getCookieValue(httpContext, "uprand");
    NULogger.getLogger().log(Level.INFO, "Uprand cookie from depositfiles.com : {0}", uprandcookie);
}
 
Example #10
Source File: cfHttpConnection.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public cfHttpConnection( cfSession _session, cfHttpData _httpData, File _clientCert, String _clientPassword ) throws cfmRunTimeException {
	init( _session, _httpData );
	if ( _clientCert != null ) {
		try {
			client = getHttpClient( _clientCert, _clientPassword );
		} catch ( Exception e ) {
			throw newRunTimeException( "Failed to instantiate http client due to certificate issue. " + e.getClass().getName() + " was thrown: " + e.getMessage() );
		}
	} else {
		client = new ContentEncodingHttpClient();
	}
	client.getParams().setBooleanParameter( ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true );
	resolveLinks = false;
}
 
Example #11
Source File: cfHttpConnection.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
private void addCookies() {

		Map<String, List<String>> cookies = httpData.getCookies();
		Iterator<String> keys = cookies.keySet().iterator();
		String domain = "";
		domain = message.getURI().getHost();
		CookieStore cookieStore = new BasicCookieStore();

		HttpContext localContext = new BasicHttpContext();

		// Bind custom cookie store to the local context
		localContext.setAttribute( ClientContext.COOKIE_STORE, cookieStore );
		client.getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY );

		while ( keys.hasNext() ) {
			String nextKey = keys.next();
			List<String> values = cookies.get( nextKey );
			Date date = new Date( 2038, 1, 1 );
			for ( int i = 0; i < values.size(); i++ ) {
				BasicClientCookie cookie = new BasicClientCookie( nextKey, values.get( i ) );
				cookieStore.addCookie( cookie );
				cookie.setVersion( 1 );
				cookie.setDomain( domain );
				cookie.setPath( "/" );
				cookie.setExpiryDate( date );
				cookie.setSecure( false );
			}
		}
		client.setCookieStore( cookieStore );
	}
 
Example #12
Source File: NFHttpClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
void init(IClientConfig config, boolean registerMonitor) {
	HttpParams params = getParams();

	HttpProtocolParams.setContentCharset(params, "UTF-8");  
	params.setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, 
			ThreadSafeClientConnManager.class.getName());
	HttpClientParams.setRedirecting(params, config.get(CommonClientConfigKey.FollowRedirects, true));
	// set up default headers
	List<Header> defaultHeaders = new ArrayList<Header>();
	defaultHeaders.add(new BasicHeader("Netflix.NFHttpClient.Version", "1.0"));
	defaultHeaders.add(new BasicHeader("X-netflix-httpclientname", name));
	params.setParameter(ClientPNames.DEFAULT_HEADERS, defaultHeaders);

	connPoolCleaner = new ConnectionPoolCleaner(name, this.getConnectionManager(), connectionPoolCleanUpScheduler);

	this.retriesProperty = config.getGlobalProperty(RETRIES.format(name));

	this.sleepTimeFactorMsProperty = config.getGlobalProperty(SLEEP_TIME_FACTOR_MS.format(name));
	setHttpRequestRetryHandler(
			new NFHttpMethodRetryHandler(this.name, this.retriesProperty.getOrDefault(), false,
					this.sleepTimeFactorMsProperty.getOrDefault()));
    tracer = Monitors.newTimer(EXECUTE_TRACER + "-" + name, TimeUnit.MILLISECONDS);
    if (registerMonitor) {
           Monitors.registerObject(name, this);
    }
    maxTotalConnectionProperty = config.getDynamicProperty(CommonClientConfigKey.MaxTotalHttpConnections);
    maxTotalConnectionProperty.onChange(newValue ->
    	((ThreadSafeClientConnManager) getConnectionManager()).setMaxTotal(newValue)
    );

    maxConnectionPerHostProperty = config.getDynamicProperty(CommonClientConfigKey.MaxHttpConnectionsPerHost);
    maxConnectionPerHostProperty.onChange(newValue ->
		((ThreadSafeClientConnManager) getConnectionManager()).setDefaultMaxPerRoute(newValue)
       );

	connIdleEvictTimeMilliSeconds = config.getGlobalProperty(CONN_IDLE_EVICT_TIME_MILLIS.format(name));
}
 
Example #13
Source File: LocalFileModel.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @deprecated use {@link LocalFileModel#LocalFileModel(java.lang.String,
 * org.pentaho.reporting.engine.classic.core.util.HttpClientManager.HttpClientBuilderFacade,
 * java.lang.String, java.lang.String, java.lang.String, int) }.
 */
@Deprecated()
public LocalFileModel( final String url,
                       final HttpClient client,
                       final String username,
                       final String password,
                       final String hostName,
                       int port ) {
  if ( url == null ) {
    throw new NullPointerException();
  }
  this.url = url;
  this.username = username;
  this.password = password;
  this.client = client;

  this.context = HttpClientContext.create();
  if ( !StringUtil.isEmpty( hostName ) ) {
    // Preemptive Basic Authentication
    HttpHost target = new HttpHost( hostName, port, "http" );
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local
    // auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put( target, basicAuth );
    // Add AuthCache to the execution context
    this.context.setAuthCache( authCache );
  }
  this.client.getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY );
  this.client.getParams().setParameter( ClientPNames.MAX_REDIRECTS, Integer.valueOf( 10 ) );
  this.client.getParams().setParameter( ClientPNames.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE );
  this.client.getParams().setParameter( ClientPNames.REJECT_RELATIVE_REDIRECT, Boolean.FALSE );
}
 
Example #14
Source File: HttpClientTimeoutLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void givenUsingOldApi_whenSettingTimeoutViaParameter_thenCorrect() throws IOException {
    
    DefaultHttpClient httpClient = new DefaultHttpClient();
    int timeout = 5; // seconds
    HttpParams httpParams = httpClient.getParams();
    httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
    httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
    httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000));
    
    final HttpGet request = new HttpGet("http://www.github.com");
    HttpResponse execute = httpClient.execute(request);
    assertThat(execute.getStatusLine().getStatusCode(), equalTo(200));
}
 
Example #15
Source File: HttpClientVendor.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public HttpClientVendor() {
    client = new DefaultHttpClient(buildConnectionManager(20));
    client.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
    client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
    client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);

    // Wait this long for an available connection. Setting this correctly is important in order to avoid
    // connectionpool timeouts.
    client.getParams().setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, 5000);
}
 
Example #16
Source File: HttpUtil.java    From Leo with Apache License 2.0 5 votes vote down vote up
/**
 * 默认构造函数
 */
public HttpUtil() {
	this.charset = "UTF-8"; 
	PoolingClientConnectionManager pccm = new PoolingClientConnectionManager();
       pccm.setMaxTotal(100); //设置整个连接池最大链接数
	httpClient = new DefaultHttpClient(pccm);
	httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.BROWSER_COMPATIBILITY);
	httpClient.getConnectionManager().closeIdleConnections(30,TimeUnit.SECONDS);
}
 
Example #17
Source File: HttpsUtil.java    From Leo with Apache License 2.0 5 votes vote down vote up
/**
 * 默认构造函数
 */
public HttpsUtil() {
	this.charset = "UTF-8";
	httpClient = this.setHttpClient(httpClient);
	httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.BROWSER_COMPATIBILITY);
	httpClient.getConnectionManager().closeIdleConnections(30,TimeUnit.SECONDS);
}
 
Example #18
Source File: WebAppProxyServlet.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Download link and have it be the response.
 * @param req the http request
 * @param resp the http response
 * @param link the link to download
 * @param c the cookie to set if any
 * @throws IOException on any error.
 */
private static void proxyLink(HttpServletRequest req, 
    HttpServletResponse resp, URI link, Cookie c, String proxyHost)
    throws IOException {
  DefaultHttpClient client = new DefaultHttpClient();
  client
      .getParams()
      .setParameter(ClientPNames.COOKIE_POLICY,
          CookiePolicy.BROWSER_COMPATIBILITY)
      .setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
  // Make sure we send the request from the proxy address in the config
  // since that is what the AM filter checks against. IP aliasing or
  // similar could cause issues otherwise.
  InetAddress localAddress = InetAddress.getByName(proxyHost);
  if (LOG.isDebugEnabled()) {
    LOG.debug("local InetAddress for proxy host: {}", localAddress);
  }
  client.getParams()
      .setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress);
  HttpGet httpGet = new HttpGet(link);
  @SuppressWarnings("unchecked")
  Enumeration<String> names = req.getHeaderNames();
  while(names.hasMoreElements()) {
    String name = names.nextElement();
    if(passThroughHeaders.contains(name)) {
      String value = req.getHeader(name);
      if (LOG.isDebugEnabled()) {
        LOG.debug("REQ HEADER: {} : {}", name, value);
      }
      httpGet.setHeader(name, value);
    }
  }

  String user = req.getRemoteUser();
  if (user != null && !user.isEmpty()) {
    httpGet.setHeader("Cookie",
        PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII"));
  }
  OutputStream out = resp.getOutputStream();
  try {
    HttpResponse httpResp = client.execute(httpGet);
    resp.setStatus(httpResp.getStatusLine().getStatusCode());
    for (Header header : httpResp.getAllHeaders()) {
      resp.setHeader(header.getName(), header.getValue());
    }
    if (c != null) {
      resp.addCookie(c);
    }
    InputStream in = httpResp.getEntity().getContent();
    if (in != null) {
      IOUtils.copyBytes(in, out, 4096, true);
    }
  } finally {
    httpGet.releaseConnection();
  }
}
 
Example #19
Source File: WebAppProxyServlet.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Download link and have it be the response.
 * @param req the http request
 * @param resp the http response
 * @param link the link to download
 * @param c the cookie to set if any
 * @throws IOException on any error.
 */
private static void proxyLink(HttpServletRequest req, 
    HttpServletResponse resp, URI link, Cookie c, String proxyHost)
    throws IOException {
  DefaultHttpClient client = new DefaultHttpClient();
  client
      .getParams()
      .setParameter(ClientPNames.COOKIE_POLICY,
          CookiePolicy.BROWSER_COMPATIBILITY)
      .setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
  // Make sure we send the request from the proxy address in the config
  // since that is what the AM filter checks against. IP aliasing or
  // similar could cause issues otherwise.
  InetAddress localAddress = InetAddress.getByName(proxyHost);
  if (LOG.isDebugEnabled()) {
    LOG.debug("local InetAddress for proxy host: {}", localAddress);
  }
  client.getParams()
      .setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress);
  HttpGet httpGet = new HttpGet(link);
  @SuppressWarnings("unchecked")
  Enumeration<String> names = req.getHeaderNames();
  while(names.hasMoreElements()) {
    String name = names.nextElement();
    if(passThroughHeaders.contains(name)) {
      String value = req.getHeader(name);
      if (LOG.isDebugEnabled()) {
        LOG.debug("REQ HEADER: {} : {}", name, value);
      }
      httpGet.setHeader(name, value);
    }
  }

  String user = req.getRemoteUser();
  if (user != null && !user.isEmpty()) {
    httpGet.setHeader("Cookie",
        PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII"));
  }
  OutputStream out = resp.getOutputStream();
  try {
    HttpResponse httpResp = client.execute(httpGet);
    resp.setStatus(httpResp.getStatusLine().getStatusCode());
    for (Header header : httpResp.getAllHeaders()) {
      resp.setHeader(header.getName(), header.getValue());
    }
    if (c != null) {
      resp.addCookie(c);
    }
    InputStream in = httpResp.getEntity().getContent();
    if (in != null) {
      IOUtils.copyBytes(in, out, 4096, true);
    }
  } finally {
    httpGet.releaseConnection();
  }
}
 
Example #20
Source File: SdcKrb5HttpClientConfigurer.java    From datacollector with Apache License 2.0 4 votes vote down vote up
public void configure(DefaultHttpClient httpClient, SolrParams config) {
  super.configure(httpClient, config);

  // Begin change for SDC-2962
  // Instead of checking existence of JAAS file, do the following if solr kerberos is enabled
  //if (System.getProperty(LOGIN_CONFIG_PROP) != null) {
    //String configValue = System.getProperty(LOGIN_CONFIG_PROP);

    //if (configValue != null) {
     // logger.info("Setting up SPNego auth with config: " + configValue);
      final String useSubjectCredsProp = "javax.security.auth.useSubjectCredsOnly";
      String useSubjectCredsVal = System.getProperty(useSubjectCredsProp);

      // "javax.security.auth.useSubjectCredsOnly" should be false so that the underlying
      // authentication mechanism can load the credentials from the JAAS configuration.
      if (useSubjectCredsVal == null) {
        System.setProperty(useSubjectCredsProp, "false");
      }
      else if (!useSubjectCredsVal.toLowerCase(Locale.ROOT).equals("false")) {
        // Don't overwrite the prop value if it's already been written to something else,
        // but log because it is likely the Credentials won't be loaded correctly.
        logger.warn("System Property: " + useSubjectCredsProp + " set to: " + useSubjectCredsVal
            + " not false.  SPNego authentication may not be successful.");
      }

      // Change for SDC-2962
      //javax.security.auth.login.Configuration.setConfiguration(jaasConfig);
      //Enable only SPNEGO authentication scheme.
      AuthSchemeRegistry registry = new AuthSchemeRegistry();
      registry.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, false));
      httpClient.setAuthSchemes(registry);
      // Get the credentials from the JAAS configuration rather than here
      Credentials useJaasCreds = new Credentials() {
        public String getPassword() {
          return null;
        }
        public Principal getUserPrincipal() {
          return null;
        }
      };

      SolrPortAwareCookieSpecFactory cookieFactory = new SolrPortAwareCookieSpecFactory();
      httpClient.getCookieSpecs().register(cookieFactory.POLICY_NAME, cookieFactory);
      httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, cookieFactory.POLICY_NAME);

      httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, useJaasCreds);

      httpClient.addRequestInterceptor(bufferedEntityInterceptor);
    //} else {
      //httpClient.getCredentialsProvider().clear();
    //}
 // }
}
 
Example #21
Source File: SockShare.java    From neembuu-uploader with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Upload with normal uploader.
 */
public void normalUpload() throws IOException, Exception{
    String uploadPostUrl;
    NUHttpPost httppost;
    HttpResponse response;
    String reqResponse;
    String doneURL;
    String sessionID;
    String authHash;
    
    //Set the cookie store
    cookieStore = new BasicCookieStore();
    httpContext = new BasicHttpContext();
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH); //CookiePolicy
    
    //Get the url for upload
    reqResponse = NUHttpClientUtils.getData(uploadGetURL, httpContext);
    
    //Read various strings
    uploadPostUrl = StringUtils.stringBetweenTwoStrings(reqResponse, "'script' : '", "'");
    authHash = StringUtils.stringBetweenTwoStrings(reqResponse, "auth_hash':'", "'");
    doneURL = "http://www.sockshare.com/cp.php?uploaded=upload_form.php?done="+StringUtils.stringBetweenTwoStrings(reqResponse, "upload_form.php?done=", "'"); //Now find the done URL
    NULogger.getLogger().log(Level.INFO, "Upload post URL: {0}", uploadPostUrl);
    NULogger.getLogger().log(Level.INFO, "AuthHash: {0}", authHash);
    NULogger.getLogger().log(Level.INFO, "Done URL: {0}", doneURL);
    sessionID = CookieUtils.getCookieValue(httpContext, "PHPSESSID");

    //Start the upload
    uploading();

    httppost = new NUHttpPost(uploadPostUrl);
    ContentBody cbFile = createMonitoredFileBody();
    MultipartEntity mpEntity = new MultipartEntity();
    mpEntity.addPart("Filename", new StringBody(file.getName()));
    mpEntity.addPart("fileext", new StringBody("*"));
    mpEntity.addPart("do_convert", new StringBody("1"));
    mpEntity.addPart("session", new StringBody(sessionID));
    mpEntity.addPart("folder", new StringBody("/"));
    mpEntity.addPart("auth_hash", new StringBody(authHash));
    mpEntity.addPart("Filedata", cbFile);
    mpEntity.addPart("Upload", new StringBody("Submit Query"));
    httppost.setEntity(mpEntity);
    response = httpclient.execute(httppost, httpContext);
    reqResponse = EntityUtils.toString(response.getEntity());
    
    if("cool story bro".equals(reqResponse)){
        //Now we can read the link
        gettingLink();
        reqResponse = NUHttpClientUtils.getData(doneURL, httpContext);
        downURL = "http://www.sockshare.com/file/"+StringUtils.stringBetweenTwoStrings(reqResponse, "<a href=\"http://www.sockshare.com/file/", "\"");
        NULogger.getLogger().log(Level.INFO, "Download URL: {0}", downURL);
    }
    else{
        //Handle errors
        NULogger.getLogger().info(reqResponse);
        throw new Exception("Error in sockshare.com. Take a look to last reqResponse!");
    }
}
 
Example #22
Source File: UploadingDotCom.java    From neembuu-uploader with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void run() {
    try {

        if (uploadingDotComAccount.loginsuccessful) {
            host = uploadingDotComAccount.username + " | Uploading.com";
            httpContext = uploadingDotComAccount.getHttpContext();
            httpClient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); 
        } else {
            host = "Uploading.com";

            uploadInvalid();
            return;
        }

        if (file.length() > maxFileSizeLimit) {
            throw new NUMaxFileSizeException(maxFileSizeLimit, file.getName(), uploadingDotComAccount.getHOSTNAME());
        }

        uploadInitialising();
        
        getPreDownloadLink();
        

        afterloginpage = getData();
        //NULogger.getLogger().log(Level.INFO, "after : {0}", afterloginpage);
        uploadinglink = StringUtils.stringBetweenTwoStrings(afterloginpage, "upload_url: '", "'");
        uploadinglink = uploadinglink.replaceAll("\\\\", "");
        NULogger.getLogger().log(Level.INFO, "New Upload link : {0}", uploadinglink);
        postURL = uploadinglink;
        sid = StringUtils.stringBetweenTwoStrings(afterloginpage, "SID': '", "'");
        NULogger.getLogger().log(Level.INFO, "New sid from site : {0}", sid);
        fileUpload();

    } catch(NUException ex){
        ex.printError();
        uploadInvalid();
    } catch (Exception e) {
        Logger.getLogger(UploadingDotCom.class.getName()).log(Level.SEVERE, null, e);
        uploadFailed();
    }

}
 
Example #23
Source File: LibRequestDirector.java    From YiBo with Apache License 2.0 4 votes vote down vote up
public LibRequestDirector(
        final HttpRequestExecutor requestExec,
        final ClientConnectionManager conman,
        final ConnectionReuseStrategy reustrat,
        final ConnectionKeepAliveStrategy kastrat,
        final HttpRoutePlanner rouplan,
        final HttpProcessor httpProcessor,
        final HttpRequestRetryHandler retryHandler,
        final RedirectHandler redirectHandler,
        final AuthenticationHandler targetAuthHandler,
        final AuthenticationHandler proxyAuthHandler,
        final UserTokenHandler userTokenHandler,
        final HttpParams params) {

    if (requestExec == null) {
        throw new IllegalArgumentException("Request executor may not be null.");
    }
    if (conman == null) {
        throw new IllegalArgumentException("Client connection manager may not be null.");
    }
    if (reustrat == null) {
        throw new IllegalArgumentException("Connection reuse strategy may not be null.");
    }
    if (kastrat == null) {
        throw new IllegalArgumentException("Connection keep alive strategy may not be null.");
    }
    if (rouplan == null) {
        throw new IllegalArgumentException("Route planner may not be null.");
    }
    if (httpProcessor == null) {
        throw new IllegalArgumentException("HTTP protocol processor may not be null.");
    }
    if (retryHandler == null) {
        throw new IllegalArgumentException("HTTP request retry handler may not be null.");
    }
    if (redirectHandler == null) {
        throw new IllegalArgumentException("Redirect handler may not be null.");
    }
    if (targetAuthHandler == null) {
        throw new IllegalArgumentException("Target authentication handler may not be null.");
    }
    if (proxyAuthHandler == null) {
        throw new IllegalArgumentException("Proxy authentication handler may not be null.");
    }
    if (userTokenHandler == null) {
        throw new IllegalArgumentException("User token handler may not be null.");
    }
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    this.requestExec       = requestExec;
    this.connManager       = conman;
    this.reuseStrategy     = reustrat;
    this.keepAliveStrategy = kastrat;
    this.routePlanner      = rouplan;
    this.httpProcessor     = httpProcessor;
    this.retryHandler      = retryHandler;
    this.redirectHandler   = redirectHandler;
    this.targetAuthHandler = targetAuthHandler;
    this.proxyAuthHandler  = proxyAuthHandler;
    this.userTokenHandler  = userTokenHandler;
    this.params            = params;

    this.managedConn       = null;

    this.execCount = 0;
    this.redirectCount = 0;
    this.maxRedirects = this.params.getIntParameter(ClientPNames.MAX_REDIRECTS, 100);
    this.targetAuthState = new AuthState();
    this.proxyAuthState = new AuthState();
}
 
Example #24
Source File: HttpRequestHelper.java    From YiBo with Apache License 2.0 4 votes vote down vote up
private static synchronized HttpClient createHttpClient(HttpConfig config) {
	if (config == null) {
		return null;
	}

	if (connectionManager == null) {
		connectionManager = createConnectionManager();
	}

	HttpParams httpParams = new BasicHttpParams();

	if (config.getHttpConnectionTimeout() > 0) {
		httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
			config.getHttpConnectionTimeout());
	}
	if (config.getHttpReadTimeout() > 0) {
		httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getHttpReadTimeout());
	}
	// 设置cookie策略
    httpParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
			
	// 设置http.protocol.expect-continue参数为false,即不使用Expect:100-Continue握手,
	// 因为如果服务器不支持HTTP 1.1,则会导致HTTP 417错误。
	HttpProtocolParams.setUseExpectContinue(httpParams, false);
	// 设置User-Agent
	HttpProtocolParams.setUserAgent(httpParams, config.getUserAgent());
	// 设置HTTP版本为 HTTP 1.1
	HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);

	DefaultHttpClient httpClient = new LibHttpClient(connectionManager, httpParams);

	updateProxySetting(config, httpClient);

	if (config.isUseGzip()) {
		httpClient.addRequestInterceptor(new GzipRequestInterceptor());
		httpClient.addResponseInterceptor(new GzipResponseInterceptor());
	}
	if (config.getHttpRetryCount() > 0) {
		HttpRequestRetryHandler retryHandler =
			new DefaultHttpRequestRetryHandler(config.getHttpRetryCount(), true);
		httpClient.setHttpRequestRetryHandler(retryHandler);
	}

	return httpClient;
}
 
Example #25
Source File: MainActivity.java    From Android-ImageManager with MIT License 4 votes vote down vote up
public MainActivity() {
    httpClient = new AsyncHttpClient();
    httpClient.getHttpClient().getParams().setParameter("http.protocol.single-cookie-header", true);
    httpClient.getHttpClient().getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
}
 
Example #26
Source File: SessionEventsTest.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void initHttpClient()
{
    client = new DefaultHttpClient();
    client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
}
 
Example #27
Source File: AsyncHttpClient.java    From AndroidWear-OpenWear with MIT License 3 votes vote down vote up
/**
 * Simple interface method, to enable or disable redirects. If you set
 * manually RedirectHandler on underlying HttpClient, effects of this method
 * will be canceled.
 * <p>
 * &nbsp;
 * </p>
 * Default setting is to disallow redirects.
 *
 * @param enableRedirects         boolean
 * @param enableRelativeRedirects boolean
 * @param enableCircularRedirects boolean
 */
public void setEnableRedirects(final boolean enableRedirects, final boolean enableRelativeRedirects,
                               final boolean enableCircularRedirects) {
    httpClient.getParams().setBooleanParameter(ClientPNames.REJECT_RELATIVE_REDIRECT, !enableRelativeRedirects);
    httpClient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, enableCircularRedirects);
    httpClient.setRedirectHandler(new MyRedirectHandler(enableRedirects));
}
 
Example #28
Source File: ApacheHttpTransport.java    From google-http-java-client with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor that allows an alternative Apache HTTP client to be used.
 *
 * <p>Note that a few settings are overridden:
 *
 * <ul>
 *   <li>HTTP version is set to 1.1 using {@link HttpProtocolParams#setVersion} with {@link
 *       HttpVersion#HTTP_1_1}.
 *   <li>Redirects are disabled using {@link ClientPNames#HANDLE_REDIRECTS}.
 *   <li>{@link ConnManagerParams#setTimeout} and {@link
 *       HttpConnectionParams#setConnectionTimeout} are set on each request based on {@link
 *       HttpRequest#getConnectTimeout()}.
 *   <li>{@link HttpConnectionParams#setSoTimeout} is set on each request based on {@link
 *       HttpRequest#getReadTimeout()}.
 * </ul>
 *
 * <p>Use {@link Builder} for a more user-friendly way to modify the HTTP client options.
 *
 * @param httpClient Apache HTTP client to use
 * @since 1.6
 */
public ApacheHttpTransport(HttpClient httpClient) {
  this.httpClient = httpClient;
  HttpParams params = httpClient.getParams();
  if (params == null) {
    params = newDefaultHttpClient().getParams();
  }
  HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
}
 
Example #29
Source File: AsyncHttpClient.java    From Mobike with Apache License 2.0 2 votes vote down vote up
/**
 * Simple interface method, to enable or disable redirects. If you set manually RedirectHandler
 * on underlying HttpClient, effects of this method will be canceled. <p>&nbsp;</p> Default
 * setting is to disallow redirects.
 *
 * @param enableRedirects         boolean
 * @param enableRelativeRedirects boolean
 * @param enableCircularRedirects boolean
 */
public void setEnableRedirects(final boolean enableRedirects, final boolean enableRelativeRedirects, final boolean enableCircularRedirects) {
    httpClient.getParams().setBooleanParameter(ClientPNames.REJECT_RELATIVE_REDIRECT, !enableRelativeRedirects);
    httpClient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, enableCircularRedirects);
    httpClient.setRedirectHandler(new MyRedirectHandler(enableRedirects));
}
 
Example #30
Source File: AsyncHttpClient.java    From android-project-wo2b with Apache License 2.0 2 votes vote down vote up
/**
 * Simple interface method, to enable or disable redirects. If you set manually RedirectHandler
 * on underlying HttpClient, effects of this method will be canceled. <p>&nbsp;</p> Default
 * setting is to disallow redirects.
 *
 * @param enableRedirects         boolean
 * @param enableRelativeRedirects boolean
 * @param enableCircularRedirects boolean
 */
public void setEnableRedirects(final boolean enableRedirects, final boolean enableRelativeRedirects, final boolean enableCircularRedirects) {
    httpClient.getParams().setBooleanParameter(ClientPNames.REJECT_RELATIVE_REDIRECT, !enableRelativeRedirects);
    httpClient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, enableCircularRedirects);
    httpClient.setRedirectHandler(new MyRedirectHandler(enableRedirects));
}