List of usage examples for org.apache.http.params CoreProtocolPNames USER_AGENT
String USER_AGENT
To view the source code for org.apache.http.params CoreProtocolPNames USER_AGENT.
Click Source Link
From source file:wh.contrib.RewardOptimizerHttpClient.java
public static void main(String[] args) throws Exception { HttpParams params = new BasicHttpParams(); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 30000) .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000) .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 64 * 1024) .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false) .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true) .setParameter(CoreProtocolPNames.USER_AGENT, "HttpComponents/1.1 (RewardOptimizer - karlthepagan@gmail.com)"); final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(2, params); BasicHttpProcessor httpproc = new BasicHttpProcessor(); httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTargetHost()); httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); // We are going to use this object to synchronize between the // I/O event and main threads RequestCount requestCount = new RequestCount(1); BufferingHttpClientHandler handler = new BufferingHttpClientHandler(httpproc, new MyHttpRequestExecutionHandler(requestCount), new DefaultConnectionReuseStrategy(), params); handler.setEventListener(new EventLogger()); final IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(handler, params); Thread t = new Thread(new Runnable() { public void run() { try { ioReactor.execute(ioEventDispatch); } catch (InterruptedIOException ex) { System.err.println("Interrupted"); } catch (IOException e) { System.err.println("I/O error: " + e.getMessage()); }//from ww w . j a v a 2 s . c o m System.out.println("Shutdown"); } }); t.start(); List<SessionRequest> reqs = new ArrayList<SessionRequest>(); // reqs.add(ioReactor.connect( // new InetSocketAddress("www.yahoo.com", 80), // null, // new HttpHost("www.yahoo.com"), // null)); // reqs.add(ioReactor.connect( // new InetSocketAddress("www.google.com", 80), // null, // new HttpHost("www.google.ch"), // null)); // reqs.add(ioReactor.connect( // new InetSocketAddress("www.apache.org", 80), // null, // new HttpHost("www.apache.org"), // null)); reqs.add(ioReactor.connect(new InetSocketAddress("www.wowhead.com", 80), null, new HttpHost("www.wowhead.com"), null)); // Block until all connections signal // completion of the request execution synchronized (requestCount) { while (requestCount.getValue() > 0) { requestCount.wait(); } } System.out.println("Shutting down I/O reactor"); ioReactor.shutdown(); System.out.println("Done"); }
From source file:org.eclipse.ecf.provider.filetransfer.httpcore.NHttpClient.java
public static void main(String[] args) throws Exception { HttpParams params = new BasicHttpParams(); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000) .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000) .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024) .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false) .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true) .setParameter(CoreProtocolPNames.USER_AGENT, "HttpComponents/1.1"); final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(2, params); BasicHttpProcessor httpproc = new BasicHttpProcessor(); httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTargetHost()); httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); // We are going to use this object to synchronize between the // I/O event and main threads //CountDownLatch requestCount = new CountDownLatch(3); CountDownLatch requestCount = new CountDownLatch(1); BufferingHttpClientHandler handler = new MyBufferingHttpClientHandler(httpproc, new MyHttpRequestExecutionHandler(requestCount), new DefaultConnectionReuseStrategy(), params); handler.setEventListener(new EventLogger()); final IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(handler, params); Thread t = new Thread(new Runnable() { public void run() { try { ioReactor.execute(ioEventDispatch); } catch (InterruptedIOException ex) { System.err.println("Interrupted"); } catch (IOException e) { System.err.println("I/O error: " + e.getMessage()); }/* w w w . j a v a 2s. c om*/ System.out.println("Shutdown"); } }); t.start(); SessionRequest[] reqs = new SessionRequest[1]; reqs[0] = ioReactor.connect(new InetSocketAddress("ftp.osuosl.org", 80), null, new HttpHost("ftp.osuosl.org"), new MySessionRequestCallback(requestCount)); // Block until all connections signal // completion of the request execution requestCount.await(); System.out.println("Shutting down I/O reactor"); ioReactor.shutdown(); System.out.println("Done"); }
From source file:NHttpClient.java
public static void main(String[] args) throws Exception { HttpParams params = new BasicHttpParams(); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000) .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000) .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024) .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false) .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true) .setParameter(CoreProtocolPNames.USER_AGENT, "HttpComponents/1.1"); final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(2, params); BasicHttpProcessor httpproc = new BasicHttpProcessor(); httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTargetHost()); httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); // We are going to use this object to synchronize between the // I/O event and main threads CountDownLatch requestCount = new CountDownLatch(3); BufferingHttpClientHandler handler = new BufferingHttpClientHandler(httpproc, new MyHttpRequestExecutionHandler(requestCount), new DefaultConnectionReuseStrategy(), params); handler.setEventListener(new EventLogger()); final IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(handler, params); Thread t = new Thread(new Runnable() { public void run() { try { ioReactor.execute(ioEventDispatch); } catch (InterruptedIOException ex) { System.err.println("Interrupted"); } catch (IOException e) { System.err.println("I/O error: " + e.getMessage()); }/*from w w w . j av a2 s. c om*/ System.out.println("Shutdown"); } }); t.start(); SessionRequest[] reqs = new SessionRequest[3]; reqs[0] = ioReactor.connect(new InetSocketAddress("www.yahoo.com", 80), null, new HttpHost("www.yahoo.com"), new MySessionRequestCallback(requestCount)); reqs[1] = ioReactor.connect(new InetSocketAddress("www.google.com", 80), null, new HttpHost("www.google.ch"), new MySessionRequestCallback(requestCount)); reqs[2] = ioReactor.connect(new InetSocketAddress("www.apache.org", 80), null, new HttpHost("www.apache.org"), new MySessionRequestCallback(requestCount)); // Block until all connections signal // completion of the request execution requestCount.await(); System.out.println("Shutting down I/O reactor"); ioReactor.shutdown(); System.out.println("Done"); }
From source file:com.threadswarm.imagefeedarchiver.driver.CommandLineDriver.java
public static void main(String[] args) throws InterruptedException, ExecutionException, ParseException { // define available command-line options Options options = new Options(); options.addOption("h", "help", false, "display usage information"); options.addOption("u", "url", true, "RSS feed URL"); options.addOption("a", "user-agent", true, "User-Agent header value to use when making HTTP requests"); options.addOption("o", "output-directory", true, "output directory for downloaded images"); options.addOption("t", "thread-count", true, "number of worker threads, defaults to cpu-count + 1"); options.addOption("d", "delay", true, "delay between image downloads (in milliseconds)"); options.addOption("p", "notrack", false, "tell websites that you don't wish to be tracked (DNT)"); options.addOption("s", "https", false, "Rewrite image URLs to leverage SSL/TLS"); CommandLineParser commandLineParser = new BasicParser(); CommandLine commandLine = commandLineParser.parse(options, args); // print usage information if 'h'/'help' or no-args were given if (args.length == 0 || commandLine.hasOption("h")) { HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.printHelp("java -jar ImageFeedArchiver.jar", options); return; //abort execution }//from w w w .j a v a 2 s . c om URI rssFeedUri = null; if (commandLine.hasOption("u")) { String rssFeedUrlString = commandLine.getOptionValue("u"); try { rssFeedUri = FeedUtils.getUriFromUrlString(rssFeedUrlString); } catch (MalformedURLException | URISyntaxException e) { LOGGER.error("The Feed URL you supplied was malformed or violated syntax rules.. exiting", e); System.exit(1); } LOGGER.info("Target RSS feed URL: {}", rssFeedUri); } else { throw new IllegalStateException("RSS feed URL was not specified!"); } File outputDirectory = null; if (commandLine.hasOption("o")) { outputDirectory = new File(commandLine.getOptionValue("o")); if (!outputDirectory.isDirectory()) throw new IllegalArgumentException("output directory must be a *directory*!"); LOGGER.info("Using output directory: '{}'", outputDirectory); } else { throw new IllegalStateException("output directory was not specified!"); } String userAgentString = null; if (commandLine.hasOption("a")) { userAgentString = commandLine.getOptionValue("a"); LOGGER.info("Setting 'User-Agent' header value to '{}'", userAgentString); } int threadCount; if (commandLine.hasOption("t")) { threadCount = Integer.parseInt(commandLine.getOptionValue("t")); } else { threadCount = Runtime.getRuntime().availableProcessors() + 1; } LOGGER.info("Using {} worker threads", threadCount); long downloadDelay = 0; if (commandLine.hasOption("d")) { String downloadDelayString = commandLine.getOptionValue("d"); downloadDelay = Long.parseLong(downloadDelayString); } LOGGER.info("Using a download-delay of {} milliseconds", downloadDelay); boolean doNotTrackRequested = commandLine.hasOption("p"); boolean forceHttps = commandLine.hasOption("s"); ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/applicationContext.xml"); ((ConfigurableApplicationContext) context).registerShutdownHook(); HttpClient httpClient = (HttpClient) context.getBean("httpClient"); if (userAgentString != null) httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgentString); ProcessedRssItemDAO processedRssItemDAO = (ProcessedRssItemDAO) context.getBean("processedRssItemDAO"); CommandLineDriver.Builder driverBuilder = new CommandLineDriver.Builder(rssFeedUri); driverBuilder.setDoNotTrackRequested(doNotTrackRequested).setOutputDirectory(outputDirectory) .setDownloadDelay(downloadDelay).setThreadCount(threadCount).setHttpClient(httpClient) .setForceHttps(forceHttps).setProcessedRssItemDAO(processedRssItemDAO); CommandLineDriver driver = driverBuilder.build(); driver.run(); }
From source file:Main.java
public static String httpStringGet(String url, String enc) throws Exception { // This method for HttpConnection String page = ""; BufferedReader bufferedReader = null; try {// w ww . j a v a 2 s . com HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android"); HttpParams httpParams = client.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 3000); HttpConnectionParams.setSoTimeout(httpParams, 5000); HttpGet request = new HttpGet(); request.setHeader("Content-Type", "text/plain; charset=utf-8"); request.setURI(new URI(url)); HttpResponse response = client.execute(request); bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), enc)); StringBuffer stringBuffer = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator");// "\n" while ((line = bufferedReader.readLine()) != null) { stringBuffer.append(line + NL); } bufferedReader.close(); page = stringBuffer.toString(); Log.i("page", page); System.out.println(page + "page"); return page; } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { Log.d("BBB", e.toString()); } } } }
From source file:org.eclipse.epp.internal.mpc.core.util.HttpUtil.java
public static HttpClient createHttpClient(String baseUri) { HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, MarketplaceClientCore.BUNDLE_ID); if (baseUri != null) { configureProxy(client, baseUri); }/* w w w . j a va 2 s . c o m*/ return client; }
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:com.bjorsond.android.timeline.sync.ServerDeleter.java
protected static void deleteUserFromGroupToGAE(Group groupToRemoveMember, User userToRemoveFromGroup) { final HttpDelete httpDelete = new HttpDelete("/rest/group/" + groupToRemoveMember.getId() + "/user/" + userToRemoveFromGroup.getUserName() + "/"); httpDelete.addHeader(CoreProtocolPNames.USER_AGENT, "TimelineAndroid"); sendDeleteRequestTOGAEServer("", Constants.targetHost, httpDelete); }
From source file:com.odoo.core.rpc.http.OdooSafeClient.java
public static DefaultHttpClient getSafeClient(boolean forceConnect) { createThreadSafeClient(forceConnect); httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "odoo-mobile-android"); return httpClient; }
From source file:org.apache.olingo.client.core.http.DefaultHttpClientFactory.java
@Override public DefaultHttpClient create(final HttpMethod method, final URI uri) { final DefaultHttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT); return client; }