List of usage examples for org.apache.http.params BasicHttpParams BasicHttpParams
public BasicHttpParams()
From source file:com.dmsl.anyplace.googleapi.GMapV2Direction.java
public Document getDocument(double fromLatitude, double fromLongitude, GeoPoint toPosition, String mode) throws Exception { String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + fromLatitude + "," + fromLongitude + "&destination=" + toPosition.lat + "," + toPosition.lng + "&sensor=false&units=metric&mode=" + mode; HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 20000); // 20seconds HttpConnectionParams.setSoTimeout(httpParameters, 20000); // 20 seconds HttpClient httpClient = new DefaultHttpClient(httpParameters); HttpPost httpPost = new HttpPost(url); HttpContext localContext = new BasicHttpContext(); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(in); return doc;//w w w. j av a 2s. c o m }
From source file:marytts.tools.perceptiontest.PerceptionTestHttpServer.java
public void run() { logger.info("Starting server."); System.out.println("Starting server...."); //int localPort = MaryProperties.needInteger("socket.port"); int localPort = serverPort; HttpParams params = new BasicHttpParams(); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 0) // 0 means no timeout, any positive value means time out in miliseconds (i.e. 50000 for 50 seconds) .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024) .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false) .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true) .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1"); BasicHttpProcessor httpproc = new BasicHttpProcessor(); httpproc.addInterceptor(new ResponseDate()); httpproc.addInterceptor(new ResponseServer()); httpproc.addInterceptor(new ResponseContent()); httpproc.addInterceptor(new ResponseConnControl()); BufferingHttpServiceHandler handler = new BufferingHttpServiceHandler(httpproc, new DefaultHttpResponseFactory(), new DefaultConnectionReuseStrategy(), params); // Set up request handlers HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry(); //registry.register("/perceptionTest", new FileDataRequestHandler("perception.html")); //registry.register("/process", new FileDataRequestHandler("perception.html")); //registry.register("/perceptionTest", new UtterancePlayRequestHandler()); DataRequestHandler infoRH = new DataRequestHandler(this.testXmlName); UserRatingStorer userRatingRH = new UserRatingStorer(this.userRatingsDirectory, infoRH); registry.register("/options", infoRH); registry.register("/queryStatement", infoRH); registry.register("/process", new UtterancePlayRequestHandler(infoRH)); registry.register("/perceptionTest", new PerceptionRequestHandler(infoRH, userRatingRH)); registry.register("/userRating", new StoreRatingRequestHandler(infoRH, userRatingRH)); registry.register("*", new FileDataRequestHandler()); handler.setHandlerResolver(registry); // Provide an event logger handler.setEventListener(new EventLogger()); IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(handler, params); //int numParallelThreads = MaryProperties.getInteger("server.http.parallelthreads", 5); int numParallelThreads = 5; logger.info("Waiting for client to connect on port " + localPort); System.out.println("Waiting for client to connect on port " + localPort); try {//from w ww. ja va 2 s . c o m ListeningIOReactor ioReactor = new DefaultListeningIOReactor(numParallelThreads, params); ioReactor.listen(new InetSocketAddress(localPort)); ioReactor.execute(ioEventDispatch); } catch (InterruptedIOException ex) { logger.info("Interrupted", ex); System.out.println("Interrupted" + ex.toString()); } catch (IOException e) { logger.info("Problem with HTTP connection ", e); System.out.println("Problem with HTTP connection " + e.toString()); } logger.debug("Shutdown"); System.out.println("Shutdown"); }
From source file:com.gs.tools.doc.extractor.core.util.HttpUtility.java
/** * Get data from HTTP POST method.// www .j ava 2s.co m * * This method uses * <ul> * <li>Connection timeout = 300000 (5 minutes)</li> * <li>Socket/Read timeout = 300000 (5 minutes)</li> * <li>Socket Read Buffer = 10485760 (10MB) to provide more space to read</li> * </ul> * -- in case the site is slow * * @return * @throws MalformedURLException * @throws URISyntaxException * @throws UnsupportedEncodingException * @throws IOException * @throws ClientProtocolException * @throws Exception */ public static String getPostData(String sourceUrl, String postString) throws MalformedURLException, URISyntaxException, UnsupportedEncodingException, IOException, ClientProtocolException, Exception { String result = ""; URL targetUrl = new URL(sourceUrl); /* * Create http parameter to set Connection timeout = 300000 Socket/Read * timeout = 300000 Socket Read Buffer = 10485760 */ HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 300000); HttpConnectionParams.setSocketBufferSize(httpParams, 10485760); HttpConnectionParams.setSoTimeout(httpParams, 300000); // set the http param to the DefaultHttpClient HttpClient httpClient = new DefaultHttpClient(httpParams); // create POST method and set the URL and POST data HttpPost post = new HttpPost(targetUrl.toURI()); StringEntity entity = new StringEntity(postString, "UTF-8"); post.setEntity(entity); logger.info("Execute the POST request with all input data"); // Execute the POST request on the http client to get the response HttpResponse response = httpClient.execute(post, new BasicHttpContext()); if (null != response && response.getStatusLine().getStatusCode() == 200) { HttpEntity httpEntity = response.getEntity(); if (null != httpEntity) { long contentLength = httpEntity.getContentLength(); logger.info("Content length: " + contentLength); // no data, if the content length is insufficient if (contentLength <= 0) { return ""; } // read the response to String InputStream responseStream = httpEntity.getContent(); if (null != responseStream) { BufferedReader reader = null; StringWriter writer = null; try { reader = new BufferedReader(new InputStreamReader(responseStream)); writer = new StringWriter(); int count = 0; int size = 1024 * 1024; char[] chBuff = new char[size]; while ((count = reader.read(chBuff, 0, size)) >= 0) { writer.write(chBuff, 0, count); } result = writer.getBuffer().toString(); } catch (Exception ex) { ex.printStackTrace(); throw ex; } finally { IOUtils.closeQuietly(responseStream); IOUtils.closeQuietly(reader); IOUtils.closeQuietly(writer); } } } } logger.info("data read complete"); return result; }
From source file:com.eventcentric.helper.AndroidHttpClient.java
/** * Create a new HttpClient with reasonable defaults (which you can update). * * @param userAgent to report in your HTTP requests. * @param sessionCache persistent session cache * @return AndroidHttpClient for you to use for all your requests. *//*from w w w . j a v a 2 s .c om*/ public static AndroidHttpClient newInstance(String userAgent) { HttpParams params = new BasicHttpParams(); // Turn off stale checking. Our connections break all the time anyway, // and it's not worth it to pay the penalty of checking every time. HttpConnectionParams.setStaleCheckingEnabled(params, false); // Default connection and socket timeout of 20 seconds. Tweak to taste. HttpConnectionParams.setConnectionTimeout(params, 20 * 1000); HttpConnectionParams.setSoTimeout(params, 20 * 1000); HttpConnectionParams.setSocketBufferSize(params, 8192); // Don't handle redirects -- return them to the caller. Our code // often wants to re-POST after a redirect, which we must do ourselves. HttpClientParams.setRedirecting(params, false); // Set the specified user agent and register standard protocols. HttpProtocolParams.setUserAgent(params, userAgent); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); //schemeRegistry.register(new Scheme("https", // socketFactoryWithCache(sessionCache), 443)); ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry); // We use a factory method to modify superclass initialization // parameters without the funny call-a-static-method dance. return new AndroidHttpClient(manager, params); }
From source file:org.planetmono.dcuploader.Application.java
@Override public void onCreate() { HttpParams defaultParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(defaultParams, TIMEOUT_CONNECTION); HttpConnectionParams.setSoTimeout(defaultParams, TIMEOUT_SOCKET); client = new DefaultHttpClient(defaultParams); }
From source file:net.instantcom.keikosniffer.http.SessionInputBufferMockup.java
public SessionInputBufferMockup(final String s, final String charset, int buffersize) throws UnsupportedEncodingException { this(s.getBytes(charset), buffersize, new BasicHttpParams()); }
From source file:com.mymed.android.myjam.controller.CallManager.java
/** * Private constructor.// w w w .ja v a 2s . c om * * @throws KeyManagementException * @throws NoSuchAlgorithmException * @throws KeyStoreException * @throws UnrecoverableKeyException */ private CallManager(Context context) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException { // Create and initialize HTTP parameters HttpParams httpParams = new BasicHttpParams(); /** Sets the version of the HTTP protocol to 1.1 */ HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); /** Sets a timeout until a request is established. [ms] */ HttpConnectionParams.setConnectionTimeout(httpParams, 10000); /** Sets a timeout for waiting for data. [ms] */ HttpConnectionParams.setSoTimeout(httpParams, 5000); /** Set the maximum number of total connections. */ ConnManagerParams.setMaxTotalConnections(httpParams, lpPoolSize + 1); /** Set the maximum number of connections per route. */ ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(lpPoolSize + 1)); SchemeRegistry schemeRegistry = createSchemeRegistry(context); ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpClient = new DefaultHttpClient(connManager, httpParams); lowPriorityPool = Executors.newFixedThreadPool(lpPoolSize); // highPriorityPool = Executors.newFixedThreadPool(hpPoolSize); Log.i(TAG, "Executor pool created"); }
From source file:com.yanzhenjie.andserver.CoreThread.java
/** * Create HttpParams.//from w w w.j a va2 s.c o m * * @return {@link HttpParams}. */ private HttpParams createHttpParams() { return new BasicHttpParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, mTimeout) .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024) .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false) .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true) .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "WebServer/1.1"); }
From source file:net.noisetube.io.android.AndroidHttpClient.java
/** * @param agent/* w ww .j a v a 2 s.co m*/ * * Note: setting the time-outs seems to cause A LOT more connection problems than without, so we don't use them (for now) */ public AndroidHttpClient(String agent) { super(agent); HttpParams httpParameters = new BasicHttpParams(); httpParameters.setParameter("http.useragent", agent); //HttpConnectionParams.setConnectionTimeout(httpParameters, timeout); //Set the timeout in milliseconds until a connection is established //HttpConnectionParams.setSoTimeout(httpParameters, timeout); //Set the default socket timeout in milliseconds which is the timeout for waiting for data. //ConnManagerParams.setTimeout(httpParameters, timeout); final SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); //SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); //sslSocketFactory.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); //registry.register(new Scheme("https", sslSocketFactory, 443)); final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(httpParameters, registry); httpClient = new DefaultHttpClient(manager, httpParameters); }
From source file:com.supremainc.biostar2.sdk.volley.toolbox.HttpClientStack.java
public HttpClient getNewHttpClient() { try {//from w w w. ja v a 2 s . c om KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new MySSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } }