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

The following examples show how to use org.apache.http.client.params.CookiePolicy. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
Source File: URLBencher.java    From wt1 with Apache License 2.0 4 votes vote down vote up
public void run() {
			long totalHTime = 0, maxHTime = 0;
			long totalBTime = 0, maxBTime = 0;
			for (int i = 0; i < nb; i++) {
				try {
					long before = System.nanoTime();
					/*
					HttpURLConnection conn = (HttpURLConnection) u.openConnection();
					int code = conn.getResponseCode();
					*/
					
					String url  = u.toExternalForm();
					if (r.nextInt(300) == 1) {
					    url += "?type=purchase&amount=" + r.nextInt(120);
					}

					GetMethod gm = new GetMethod(url);

					
					/* For some users, simulate the fact that they are the same, coming back (to have more visits than visitors) */
					if (r.nextInt(20 + r.nextInt(10)) == 1) {
					    gm.addRequestHeader("Cookie", "__wt1vic=888888888");
					}
					
//					System.out.println(url);
					gm.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
					int code = client.executeMethod(gm);

					long atCode = System.nanoTime();

					InputStream is = gm.getResponseBodyAsStream();
					IOUtils.toByteArray(is);
					is.close();
					long atEnd = System.nanoTime();
					if (i % 50 == 0) {
					    System.out.println("t=" + threadId + " i=" + i + " c=" + code + " head=" + (atCode-before)/1000 + " body=" + (atEnd-atCode)/1000);
					}

					totalHTime += (atCode-before)/1000;
					totalBTime += (atEnd-atCode)/1000;
					maxHTime = Math.max(maxHTime, (atCode-before)/1000);
					maxBTime = Math.max(maxBTime, (atEnd-atCode)/1000);
					
				} catch (IOException e) {
					System.out.println("t= "+ threadId + " i=" + i + " FAIL " + e.getMessage());
				}
			}
			System.out.println("t= "+ threadId + " avgHead=" + totalHTime/nb + " avgBody=" + totalBTime/nb + " maxHead=" + maxHTime + " maxBody=" + maxBTime);
		}
 
Example #14
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);
}