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:com.seajas.search.contender.http.HttpClientFeedFetcher.java
/** * NOTE: If a user agent is given here, it overrides the one given to the HttpClient. * //w w w . j a v a 2 s. co m * @param userAgent * @param feedUrl * @param headers * @return SyndFeed * @throws IllegalArgumentException * @throws IOException * @throws FeedException * @throws FetcherException */ public SyndFeed retrieveFeed(final String userAgent, final URL feedUrl, final Map<String, String> headers) throws IllegalArgumentException, IOException, FeedException, FetcherException { if (feedUrl == null) throw new IllegalArgumentException("The given URL is invalid"); HttpGet method = new HttpGet(feedUrl.toString()); method.setHeader(new BasicHeader("Accept-Encoding", "gzip")); if (headers != null) for (Entry<String, String> header : headers.entrySet()) method.setHeader(new BasicHeader(header.getKey(), header.getValue())); if (userAgent != null) method.setHeader(new BasicHeader(CoreProtocolPNames.USER_AGENT, userAgent)); // Retrieve the feed if (isUsingDeltaEncoding()) method.setHeader(new BasicHeader("A-IM", "feed")); SyndFeedInfo syndFeedInfo = feedCache.getFeedInfo(feedUrl); if (syndFeedInfo != null) { method.setHeader(new BasicHeader("If-None-Match", syndFeedInfo.getETag())); if (syndFeedInfo.getLastModified() instanceof String) method.setHeader(new BasicHeader("If-Modified-Since", (String) syndFeedInfo.getLastModified())); } HttpResponse response = httpClient.execute(method); fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, feedUrl.toString()); try { handleErrorCodes(response.getStatusLine().getStatusCode()); SyndFeed feed = handleResponse(syndFeedInfo, feedUrl.toString(), response); syndFeedInfo = buildSyndFeedInfo(feedUrl, feedUrl.toString(), response, feed); feedCache.setFeedInfo(feedUrl, syndFeedInfo); // The feed may have been modified to pick up cached values (e.g. for delta encoding) return syndFeedInfo.getSyndFeed(); } catch (RuntimeException e) { method.abort(); throw e; } catch (FetcherException e) { if (logger.isInfoEnabled()) logger.info("Consuming all entity content so that the connection is properly released."); EntityUtils.consume(response.getEntity()); throw e; } }
From source file:net.peterkuterna.android.apps.devoxxsched.c2dm.AppEngineRequestTransport.java
public void send(String payload, TransportReceiver receiver, boolean newToken) { Throwable ex;//ww w . ja va2 s. c om try { final SharedPreferences prefs = Prefs.get(context); final String accountName = prefs.getString(DevoxxPrefs.ACCOUNT_NAME, "unknown"); Account account = new Account(accountName, "com.google"); String authToken = getAuthToken(context, account); if (newToken) { // invalidate the cached token AccountManager accountManager = AccountManager.get(context); accountManager.invalidateAuthToken(account.type, authToken); authToken = getAuthToken(context, account); } HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, HttpUtils.buildUserAgent(context)); String continueURL = BASE_URL; URI uri = new URI( AUTH_URL + "?continue=" + URLEncoder.encode(continueURL, "UTF-8") + "&auth=" + authToken); HttpGet method = new HttpGet(uri); final HttpParams getParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(getParams, 20 * SECOND_IN_MILLIS); HttpConnectionParams.setSoTimeout(getParams, 20 * SECOND_IN_MILLIS); HttpClientParams.setRedirecting(getParams, false); method.setParams(getParams); HttpResponse res = client.execute(method); Header[] headers = res.getHeaders("Set-Cookie"); if (!newToken && (res.getStatusLine().getStatusCode() != 302 || headers.length == 0)) { send(payload, receiver, true); } String ascidCookie = null; for (Header header : headers) { if (header.getValue().indexOf("ACSID=") >= 0) { // let's parse it String value = header.getValue(); String[] pairs = value.split(";"); ascidCookie = pairs[0]; } } // Make POST request uri = new URI(BASE_URL + urlPath); HttpPost post = new HttpPost(); post.setHeader("Content-Type", "application/json;charset=UTF-8"); post.setHeader("Cookie", ascidCookie); post.setURI(uri); post.setEntity(new StringEntity(payload, "UTF-8")); HttpResponse response = client.execute(post); if (200 == response.getStatusLine().getStatusCode()) { String contents = readStreamAsString(response.getEntity().getContent()); receiver.onTransportSuccess(contents); } else { receiver.onTransportFailure(new ServerFailure(response.getStatusLine().getReasonPhrase())); } return; } catch (UnsupportedEncodingException e) { ex = e; } catch (ClientProtocolException e) { ex = e; } catch (IOException e) { ex = e; } catch (URISyntaxException e) { ex = e; } catch (PendingAuthException e) { final Intent intent = new Intent(SettingsActivity.AUTH_PERMISSION_ACTION); intent.putExtra("AccountManagerBundle", e.getAccountManagerBundle()); context.sendBroadcast(intent); return; } catch (Exception e) { ex = e; } receiver.onTransportFailure(new ServerFailure(ex.getMessage())); }
From source file:net.datacrow.onlinesearch.discogs.task.DiscogsMusicSearch.java
private HttpClient getHttpClient() { HttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "DataCrow/4.0 +http://www.datacrow.net"); return httpClient; }
From source file:net.spy.memcached.CouchbaseConnection.java
private List<CouchbaseNode> createConnections(List<InetSocketAddress> addrs) throws IOException { List<CouchbaseNode> nodeList = new LinkedList<CouchbaseNode>(); for (InetSocketAddress a : addrs) { HttpParams params = new SyncBasicHttpParams(); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000) .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000) .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024) .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false) .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true) .setParameter(CoreProtocolPNames.USER_AGENT, "Spymemcached Client/1.1"); HttpProcessor httpproc = new ImmutableHttpProcessor( new HttpRequestInterceptor[] { new RequestContent(), new RequestTargetHost(), new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue(), }); AsyncNHttpClientHandler protocolHandler = new AsyncNHttpClientHandler(httpproc, new MyHttpRequestExecutionHandler(), new DefaultConnectionReuseStrategy(), new DirectByteBufferAllocator(), params); protocolHandler.setEventListener(new EventLogger()); AsyncConnectionManager connMgr = new AsyncConnectionManager(new HttpHost(a.getHostName(), a.getPort()), NUM_CONNS, protocolHandler, params); getLogger().info("Added %s to connect queue", a); CouchbaseNode node = connFactory.createCouchDBNode(a, connMgr); node.init();//from w w w. java2 s. com nodeList.add(node); } return nodeList; }
From source file:com.ryan.ryanreader.cache.CacheManager.java
private CacheManager(final Context context) { if (!isAlreadyInitialized.compareAndSet(false, true)) { throw new RuntimeException("Attempt to initialize the cache twice."); }/* ww w . j a v a 2 s.co m*/ this.context = context; dbManager = new CacheDbManager(context); requestHandler = new RequestHandlerThread(); // TODo put somewhere else -- make request specific, no restart needed on prefs change! final HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.USER_AGENT, Constants.ua(context)); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); // TODO remove hardcoded params, put in network prefs params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); params.setParameter(CoreConnectionPNames.MAX_HEADER_COUNT, 100); params.setParameter(ClientPNames.HANDLE_REDIRECTS, true); params.setParameter(ClientPNames.MAX_REDIRECTS, 2); params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 50); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRoute() { public int getMaxForRoute(HttpRoute route) { return 25; } }); final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); final ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(params, schemeRegistry); final DefaultHttpClient defaultHttpClient = new DefaultHttpClient(connManager, params); defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); defaultHttpClient.addResponseInterceptor(new HttpResponseInterceptor() { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { final HttpEntity entity = response.getEntity(); final Header encHeader = entity.getContentEncoding(); if (encHeader == null) return; for (final HeaderElement elem : encHeader.getElements()) { if ("gzip".equalsIgnoreCase(elem.getName())) { response.setEntity(new GzipDecompressingEntity(entity)); return; } } } }); downloadQueue = new PrioritisedDownloadQueue(defaultHttpClient); requestHandler.start(); for (int i = 0; i < 5; i++) { // TODO remove constant --- customizable final CacheDownloadThread downloadThread = new CacheDownloadThread(downloadQueue, true); downloadThreads.add(downloadThread); } }
From source file:NioHttpClient.java
public NioHttpClient(String user_agent, HttpRequestExecutionHandler request_handler, EventListener connection_listener) throws Exception { // Construct the long-lived HTTP parameters. HttpParams parameters = new BasicHttpParams(); parameters// www . jav a2 s . c om // Socket data timeout is 5,000 milliseconds (5 seconds). .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000) // Maximum time allowed for connection establishment is 10,00 milliseconds (10 seconds). .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000) // Socket buffer size is 8 kB. .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024) // Don't bother to check for stale TCP connections. .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false) // Don't use Nagle's algorithm (in other words minimize latency). .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true) // Set the user agent string that the client sends to the server. .setParameter(CoreProtocolPNames.USER_AGENT, user_agent); // Construct the core HTTP request processor. BasicHttpProcessor http_processor = new BasicHttpProcessor(); // Add Content-Length header to request where appropriate. http_processor.addInterceptor(new RequestContent()); // Always include Host header in requests. http_processor.addInterceptor(new RequestTargetHost()); // Maintain connection keep-alive by default. http_processor.addInterceptor(new RequestConnControl()); // Include user agent information in each request. http_processor.addInterceptor(new RequestUserAgent()); // Allocate an HTTP client handler. BufferingHttpClientHandler client_handler = new BufferingHttpClientHandler(http_processor, // Basic HTTP Processor. request_handler, new DefaultConnectionReuseStrategy(), parameters); client_handler.setEventListener(connection_listener); // Use two worker threads for the IO reactor. io_reactor = new DefaultConnectingIOReactor(2, parameters); io_event_dispatch = new DefaultClientIOEventDispatch(client_handler, parameters); }
From source file:org.s1.testing.httpclient.TestHttpClient.java
/** * * @param u/*from w w w . jav a2s . c o m*/ * @param data * @param headers * @return */ public HttpResponseBean get(String u, Map<String, Object> data, Map<String, String> headers) { if (headers == null) headers = new HashMap<String, String>(); u = getURL(u, data); HttpGet get = new HttpGet(u); try { for (String h : headers.keySet()) { get.setHeader(h, headers.get(h)); } //HttpPost post = new HttpPost(LOGIN_URL); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Test Browser"); client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); //client.getParams().setParameter(ClientPNames.COOKIE_POLICY, org.apache.http.client.params.CookiePolicy.BROWSER_COMPATIBILITY); HttpResponse resp = null; try { resp = client.execute(host, get, context); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } Map<String, String> rh = new HashMap<String, String>(); for (Header h : resp.getAllHeaders()) { rh.put(h.getName(), h.getValue()); } try { HttpResponseBean r = new HttpResponseBean(resp.getStatusLine().getStatusCode(), rh, EntityUtils.toByteArray(resp.getEntity())); printIfError(u, r); return r; } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } } finally { get.releaseConnection(); } }
From source file:org.xwiki.wysiwyg.internal.plugin.alfresco.server.NoAuthSimpleHttpClient.java
/** * @return the HTTP client// w w w . j a v a 2 s .c o m */ protected HttpClient getHttpClient() { if (httpClient == null) { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); BasicHttpParams parameters = new BasicHttpParams(); ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(parameters, schemeRegistry); httpClient = new DefaultHttpClient(connectionManager, parameters); httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "XWiki's WYSIWYG Content Editor"); } return httpClient; }