List of usage examples for org.apache.http.client HttpClient getParams
@Deprecated HttpParams getParams();
From source file:uk.co.techblue.alfresco.client.Service.java
/** * Gets the client service.//from www.j av a 2 s . co m * * @param <T> the generic type * @param clazz the clazz * @param serverUri the server uri * @return the client service */ private final <T> T getClientService(final Class<T> clazz, final String serverUri) { logger.info("Generating REST resource proxy for: " + clazz.getName() + " with socket timeout as: "); final HttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, HTTP_SOCKET_TIMEOUT); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, HTTP_CONNECTION_TIMEOUT); final ClientExecutor clientExecutor = new ApacheHttpClient4Executor(httpClient); return ProxyFactory.create(clazz, serverUri, clientExecutor); }
From source file:org.mobiletrial.license.connect.RestClient.java
public void post(final String path, final JSONObject requestJson, final OnRequestFinishedListener listener) { Thread t = new Thread() { public void run() { Looper.prepare(); //For Preparing Message Pool for the child Thread // Register Schemes for http and https final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", createAdditionalCertsSSLSocketFactory(), 443)); final HttpParams params = new BasicHttpParams(); final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); HttpClient client = new DefaultHttpClient(cm, params); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit HttpResponse response;//from www . j a va 2 s.c om try { String urlStr = mServiceUrl.toExternalForm(); if (urlStr.charAt(urlStr.length() - 1) != '/') urlStr += "/"; urlStr += path; URI actionURI = new URI(urlStr); HttpPost post = new HttpPost(actionURI); StringEntity se = new StringEntity(requestJson.toString()); se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(se); response = client.execute(post); /* Checking response */ if (response == null) { listener.gotError(ERROR_CONTACTING_SERVER); Log.w(TAG, "Error contacting licensing server."); return; } int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { Log.w(TAG, "An error has occurred on the licensing server."); listener.gotError(ERROR_SERVER_FAILURE); return; } /* Convert response to JSON */ InputStream in = response.getEntity().getContent(); //Get the data in the entity String responseStr = inputstreamToString(in); listener.gotResponse(responseStr); } catch (ClientProtocolException e) { Log.w(TAG, "ClientProtocolExeption: " + e.getLocalizedMessage()); e.printStackTrace(); } catch (IOException e) { Log.w(TAG, "Error on contacting server: " + e.getLocalizedMessage()); listener.gotError(ERROR_CONTACTING_SERVER); } catch (URISyntaxException e) { //This shouldn't happen Log.w(TAG, "Could not build URI.. Your service Url is propably not a valid Url: " + e.getLocalizedMessage()); e.printStackTrace(); } Looper.loop(); //Loop in the message queue } }; t.start(); }
From source file:org.piwik.sdk.TrackerBulkURLProcessor.java
private boolean doRequest(HttpRequestBase requestBase, String body) { HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), timeout * 1000); HttpResponse response;/*from w w w .java 2 s . c o m*/ if (dryRun) { Log.i(Tracker.LOGGER_TAG, "Request wasn't send due to dry run is on"); logRequest(requestBase, body); } else { try { response = client.execute(requestBase); int statusCode = response.getStatusLine().getStatusCode(); Log.d(Tracker.LOGGER_TAG, String.format("status code %s", statusCode)); return statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_OK; } catch (ClientProtocolException e) { Log.w(Tracker.LOGGER_TAG, "Cannot send request", e); } catch (IOException e) { Log.w(Tracker.LOGGER_TAG, "Cannot send request", e); } logRequest(requestBase, body); } return false; }
From source file:nl.gridline.zieook.workflow.rest.CollectionImportCompleteTest.java
@Ignore private HttpResponse executePOST(String url, String data) { HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPut post = new HttpPut(url); post.setHeader("Accept", "application/json"); try {// ww w .j a va 2 s .c o m post.setEntity(new StringEntity(data)); return httpclient.execute(post); } catch (ClientProtocolException e) { LOG.error("upload failed", e); fail("upload failed"); } catch (IOException e) { LOG.error("upload failed", e); fail("upload failed"); } return null; }
From source file:cl.mmoscoso.geocomm.sync.GeoCommGetRoutesOwnerAsyncTask.java
@Override protected Boolean doInBackground(String... params) { // TODO Auto-generated method stub HttpClient httpclient = new DefaultHttpClient(); HttpParams httpParams = httpclient.getParams(); httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000); HttpPost httppost = new HttpPost(this.hostname); Log.i(TAGNAME, "Try to connect to " + this.hostname); try {//from ww w .j a v a 2s . co m List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("id", Integer.toString(this.id_user))); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); Log.i(TAGNAME, "Post Request"); HttpResponse response = httpclient.execute(httppost); int responseCode = response.getStatusLine().getStatusCode(); switch (responseCode) { default: Toast.makeText(this.context, R.string.error_not200code, Toast.LENGTH_SHORT).show(); break; case 200: HttpEntity entity = response.getEntity(); if (entity != null) { String responseBody = EntityUtils.toString(entity); //Log.i(TAGNAME, responseBody); JSONObject jObj, ruta; try { Log.i(TAGNAME, "Reading JSONResponse"); jObj = new JSONObject(responseBody); SharedPreferences preferences = this.context.getSharedPreferences("userInformation", context.MODE_PRIVATE); Boolean is_allroutes = preferences.getBoolean("is_allroutes", false); for (int i = 0; i < jObj.length(); i++) { ruta = new JSONObject(jObj.getString(jObj.names().getString(i))); if (is_allroutes) { if (ruta.getBoolean("public") == false) { list_routes.add(new GeoCommRoute(ruta.getString("name"), ruta.getString("owner"), ruta.getInt("id"), ruta.getInt("totalPoints"), ruta.getString("description"), ruta.getBoolean("public"))); } } else list_routes.add(new GeoCommRoute(ruta.getString("name"), ruta.getString("owner"), ruta.getInt("id"), ruta.getInt("totalPoints"), ruta.getString("description"), ruta.getBoolean("public"))); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } break; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block Log.e(TAGNAME, e.toString()); return false; } catch (IOException e) { // TODO Auto-generated catch block Log.e(TAGNAME, e.toString()); return false; } return true; }
From source file:nl.gridline.zieook.workflow.rest.CollectionImportCompleteTest.java
@Ignore private int collectionUpload(String url, File file) { // upload binary collection data HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpEntity entity = new FileEntity(file, "binary/octet-stream"); HttpPut post = new HttpPut(url); post.setEntity(entity);//from w w w. j a v a2 s . c o m try { HttpResponse response = httpclient.execute(post); return response.getStatusLine().getStatusCode(); } catch (ClientProtocolException e) { LOG.error("upload failed", e); fail("upload failed"); } catch (IOException e) { LOG.error("upload failed", e); fail("upload failed"); } return 500; }
From source file:org.cretz.sbnstat.scrape.Scraper.java
private Document loadUrl(String url, Cache cache) throws IOException, InterruptedException { String html = null;/* www.j ava 2 s .c om*/ if (cache != null) { html = cache.load(url); } if (html == null) { //wait three seconds... logger.debug("Waiting 3 seconds..."); Thread.sleep(3000); //get HTML from URL HttpClient httpClient = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 20000); HttpConnectionParams.setSoTimeout(httpClient.getParams(), 20000); HttpGet get = new HttpGet(url); //spoof user agent get.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15 ( .NET CLR 3.5.30729; .NET4.0E)"); HttpResponse response; try { response = httpClient.execute(get); } catch (SocketTimeoutException e) { //we timed out, wait logger.info("Timed out waiting for {}, will restart in 2 minutes", url); Thread.sleep(120000); return loadUrl(url, cache); } //SocketTimeoutException above if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException( "Unable to load URL " + url + ", received code " + response.getStatusLine().getStatusCode() + " with reason: " + response.getStatusLine().getReasonPhrase()); } HttpEntity entity = response.getEntity(); if (entity == null) { throw new RuntimeException("Got no entity for url " + url); } html = CharStreams.toString(new InputStreamReader(entity.getContent())); //add it to the cache if (cache != null) { cache.add(url, html); } } return Jsoup.parse(html, url); }
From source file:com.tomgibara.android.veecheck.VeecheckThread.java
private VeecheckResult performRequest(VeecheckVersion version, String uri) throws ClientProtocolException, IOException, IllegalStateException, SAXException { HttpClient client = new DefaultHttpClient(); // TODO ideally it should be possible to adjust these constants HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT); HttpGet request = new HttpGet(version.substitute(uri)); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); try {//from ww w. j a v a 2 s . c om StatusLine line = response.getStatusLine(); // TODO this is lazy, we should consider other codes here if (line.getStatusCode() != 200) { throw new IOException("Request failed: " + line.getReasonPhrase()); } Header header = response.getFirstHeader(HTTP.CONTENT_TYPE); Encoding encoding = identityEncoding(header); VeecheckResult handler = new VeecheckResult(version); Xml.parse(entity.getContent(), encoding, handler); return handler; } finally { entity.consumeContent(); } }
From source file:org.godotengine.godot.utils.HttpRequester.java
private String request(HttpUriRequest request) { // Log.d("XXX", "Haciendo request a: " + request.getURI() ); Log.d("PPP", "Haciendo request a: " + request.getURI()); long init = new Date().getTime(); HttpClient httpclient = getNewHttpClient(); HttpParams httpParameters = httpclient.getParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 0); HttpConnectionParams.setSoTimeout(httpParameters, 0); HttpConnectionParams.setTcpNoDelay(httpParameters, true); try {/*from w w w . j a v a 2s . co m*/ HttpResponse response = httpclient.execute(request); Log.d("PPP", "Fin de request (" + (new Date().getTime() - init) + ") a: " + request.getURI()); // Log.d("XXX1", "Status:" + response.getStatusLine().toString()); if (response.getStatusLine().getStatusCode() == 200) { String strResponse = EntityUtils.toString(response.getEntity()); // Log.d("XXX2", strResponse); return strResponse; } else { Log.d("XXX3", "Response status code:" + response.getStatusLine().getStatusCode() + "\n" + EntityUtils.toString(response.getEntity())); return null; } } catch (ClientProtocolException e) { Log.d("XXX3", e.getMessage()); } catch (IOException e) { Log.d("XXX4", e.getMessage()); } return null; }
From source file:org.jtalks.poulpe.service.JcommuneHttpNotifier.java
/** * Sets http-specific parameters and actually sends the request, this was factored out in a separate method in order * to be mocked out since it's impossible to mock the HTTP call itself. * * @param request prepared http request that has to be only sent, no URL or other configuration should be required * @return the HTTP response that actually came from the host * @throws IOException if problems (e.g. no connection) were found while sending request *///from ww w. j a v a 2 s . c o m @VisibleForTesting HttpResponse doSendRequest(HttpUriRequest request) throws IOException { HttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter("http.socket.timeout", HTTP_CONNECTION_TIMEOUT); httpClient.getParams().setParameter("http.connection.timeout", HTTP_CONNECTION_TIMEOUT); return httpClient.execute(request); }