List of usage examples for org.apache.http.client HttpClient getParams
@Deprecated HttpParams getParams();
From source file:org.apache.oozie.action.callback.CallbackActionExecutor.java
private HttpClient getHttpClient() { CallbackActionService callbackActionService = Services.get().get(CallbackActionService.class); HttpClient client = new DefaultHttpClient(callbackActionService.getConnectionManager()); client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, ConfigurationService.getInt(CallbackActionExecutor.CALLBACK_ACTION_HTTP_CONNECTION_TIMEOUT_SECS) * 1000);//from w w w.ja v a 2s . c om client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, ConfigurationService.getInt(CallbackActionExecutor.CALLBACK_ACTION_HTTP_SOCKET_TIMEOUT_SECS) * 1000); client.getParams().setParameter(CoreConnectionPNames.SO_KEEPALIVE, ConfigurationService.getInt(CallbackActionExecutor.CALLBACK_ACTION_KEEPALIVE_SECS) * 1000); return client; }
From source file:com.zia.freshdocs.cmis.CMIS.java
public InputStream makeHttpRequest(boolean isPost, String path, String payLoad, String contentType) { try {/*from w ww. j av a2 s . c om*/ HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "utf-8"); params.setBooleanParameter("http.protocol.expect-continue", false); params.setParameter("http.connection.timeout", new Integer(TIMEOUT)); // registers schemes for both http and https SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), _prefs.getPort())); registry.register(new Scheme("https", new EasySSLSocketFactory(), _prefs.getPort())); ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry); String url = new URL(_prefs.isSSL() ? "https" : "http", _prefs.getHostname(), buildRelativeURI(path)) .toString(); HttpClient client = new DefaultHttpClient(manager, params); client.getParams(); _networkStatus = NetworkStatus.OK; HttpRequestBase request = null; if (isPost) { request = new HttpPost(url); ((HttpPost) request).setEntity(new StringEntity(payLoad)); } else { request = new HttpGet(url); } try { if (contentType != null) { request.setHeader("Content-type", contentType); } HttpResponse response = client.execute(request); StatusLine status = response.getStatusLine(); HttpEntity entity = response.getEntity(); int statusCode = status.getStatusCode(); if ((statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) && entity != null) { // Just return the whole chunk return entity.getContent(); } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) { _networkStatus = NetworkStatus.CREDENTIALS_ERROR; } } catch (Exception ex) { Log.e(CMIS.class.getName(), "Get method error", ex); _networkStatus = NetworkStatus.CONNECTION_ERROR; } } catch (Exception ex) { Log.e(CMIS.class.getName(), "Get method error", ex); _networkStatus = NetworkStatus.UNKNOWN_ERROR; } return null; }
From source file:com.github.seratch.signedrequest4j.SignedRequestApacheHCImpl.java
/** * {@inheritDoc}/*ww w.j a v a 2 s . c o m*/ */ @Override public HttpResponse doRequest(String url, HttpMethod method, Map<String, Object> requestParameters, String charset) throws IOException { HttpEntity entity = null; if (method == HttpMethod.GET) { List<NameValuePair> params = toNameValuePairList(requestParameters); String queryString = URLEncodedUtils.format(params, "UTF-8"); if (queryString != null && !queryString.isEmpty()) { url = url.contains("?") ? url + "&" + queryString : url + "?" + queryString; } } else { List<NameValuePair> params = toNameValuePairList(requestParameters); entity = new UrlEncodedFormEntity(params, "UTF-8"); } // read get parameters for signature base string readQueryStringAndAddToSignatureBaseString(url); HttpClient httpClient = new DefaultHttpClient(); HttpUriRequest request = getRequest(method, url); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeoutMillis); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeoutMillis); httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT); httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset); for (String name : headersToOverwrite.keySet()) { request.setHeader(name, headersToOverwrite.get(name)); } String oAuthNonce = String.valueOf(new SecureRandom().nextLong()); Long oAuthTimestamp = System.currentTimeMillis() / 1000; String signature = getSignature(url, method, oAuthNonce, oAuthTimestamp); String authorizationHeader = getAuthorizationHeader(signature, oAuthNonce, oAuthTimestamp); request.setHeader("Authorization", authorizationHeader); if (entity != null) { if (method == HttpMethod.POST) { HttpPost postRequest = (HttpPost) request; postRequest.setEntity(entity); } else if (method == HttpMethod.PUT) { HttpPut putRequest = (HttpPut) request; putRequest.setEntity(entity); } } org.apache.http.HttpResponse apacheHCResponse = httpClient.execute(request); if (apacheHCResponse.getStatusLine().getStatusCode() >= 400) { HttpResponse httpResponse = toReturnValue(apacheHCResponse, charset); throw new HttpException(apacheHCResponse.getStatusLine().getReasonPhrase(), httpResponse); } return toReturnValue(apacheHCResponse, charset); }
From source file:org.openrepose.commons.utils.http.ServiceClient.java
private ServiceClientResponse execute(HttpRequestBase base, String... queryParameters) { try {// w w w . j a v a2s . c o m HttpClient client = getClientWithBasicAuth(); for (int index = 0; index < queryParameters.length; index = index + 2) { client.getParams().setParameter(queryParameters[index], queryParameters[index + 1]); } HttpResponse httpResponse = client.execute(base); HttpEntity entity = httpResponse.getEntity(); InputStream stream = null; if (entity != null) { stream = new ByteArrayInputStream(RawInputStreamReader.instance().readFully(entity.getContent())); EntityUtils.consume(entity); } return new ServiceClientResponse(httpResponse.getStatusLine().getStatusCode(), httpResponse.getAllHeaders(), stream); } catch (ServiceClientException ex) { LOG.error("Failed to obtain an HTTP default client connection", ex); } catch (IOException ex) { LOG.error("Error executing request", ex); } finally { base.releaseConnection(); } return new ServiceClientResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null); }
From source file:net.sourceforge.subsonic.service.VersionService.java
/** * Resolves the latest available Subsonic version by screen-scraping a web page. * * @throws IOException If an I/O error occurs. *//*w w w. ja v a 2 s . co m*/ private void readLatestVersion() throws IOException { HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); HttpConnectionParams.setSoTimeout(client.getParams(), 10000); HttpGet method = new HttpGet(VERSION_URL + "?v=" + getLocalVersion()); String content; try { ResponseHandler<String> responseHandler = new BasicResponseHandler(); content = client.execute(method, responseHandler); } finally { client.getConnectionManager().shutdown(); } BufferedReader reader = new BufferedReader(new StringReader(content)); Pattern finalPattern = Pattern.compile("SUBSONIC_FULL_VERSION_BEGIN(.*)SUBSONIC_FULL_VERSION_END"); Pattern betaPattern = Pattern.compile("SUBSONIC_BETA_VERSION_BEGIN(.*)SUBSONIC_BETA_VERSION_END"); try { String line = reader.readLine(); while (line != null) { Matcher finalMatcher = finalPattern.matcher(line); if (finalMatcher.find()) { latestFinalVersion = new Version(finalMatcher.group(1)); LOG.info("Resolved latest Subsonic final version to: " + latestFinalVersion); } Matcher betaMatcher = betaPattern.matcher(line); if (betaMatcher.find()) { latestBetaVersion = new Version(betaMatcher.group(1)); LOG.info("Resolved latest Subsonic beta version to: " + latestBetaVersion); } line = reader.readLine(); } } finally { reader.close(); } }
From source file:com.eastedge.readnovel.weibo.net.Utility.java
public static HttpClient getNewHttpClient(Context context) { try {//from w ww . j a v a 2s . c o m 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(); HttpConnectionParams.setConnectionTimeout(params, 10000); HttpConnectionParams.setSoTimeout(params, 10000); 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); // Set the default socket timeout (SO_TIMEOUT) // in // milliseconds which is the timeout for waiting for data. HttpConnectionParams.setConnectionTimeout(params, Utility.SET_CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, Utility.SET_SOCKET_TIMEOUT); HttpClient client = new DefaultHttpClient(ccm, params); WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (!wifiManager.isWifiEnabled()) { Uri uri = Uri.parse("content://telephony/carriers/preferapn"); Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null); if (mCursor != null && mCursor.moveToFirst()) { String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy")); if (proxyStr != null && proxyStr.trim().length() > 0) { HttpHost proxy = new HttpHost(proxyStr, 80); client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy); } mCursor.close(); } } return client; } catch (Exception e) { return new DefaultHttpClient(); } }
From source file:de.spqrinfo.cups4j.operations.IppOperation.java
/** * Sends a request to the provided url//from ww w. j ava2s . c o m * * @param url * @param ippBuf * * @param documentStream * @return result * @throws Exception */ private IppResult sendRequest(URL url, ByteBuffer ippBuf, InputStream documentStream) throws Exception { IppResult ippResult = null; if (ippBuf == null) { return null; } if (url == null) { return null; } HttpClient client = new DefaultHttpClient(); // will not work with older versions of CUPS! client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1); client.getParams().setParameter("http.socket.timeout", new Integer(10000)); client.getParams().setParameter("http.connection.timeout", new Integer(10000)); client.getParams().setParameter("http.protocol.content-charset", "UTF-8"); client.getParams().setParameter("http.method.response.buffer.warnlimit", new Integer(8092)); // probabaly not working with older CUPS versions client.getParams().setParameter("http.protocol.expect-continue", Boolean.valueOf(true)); HttpPost httpPost = new HttpPost(new URI("http://" + url.getHost() + ":" + ippPort) + url.getPath()); httpPost.getParams().setParameter("http.socket.timeout", new Integer(10000)); byte[] bytes = new byte[ippBuf.limit()]; ippBuf.get(bytes); ByteArrayInputStream headerStream = new ByteArrayInputStream(bytes); // If we need to send a document, concatenate InputStreams InputStream inputStream = headerStream; if (documentStream != null) { inputStream = new SequenceInputStream(headerStream, documentStream); } // set length to -1 to advice the entity to read until EOF InputStreamEntity requestEntity = new InputStreamEntity(inputStream, -1); requestEntity.setContentType(IPP_MIME_TYPE); httpPost.setEntity(requestEntity); httpStatusLine = null; ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() { public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException { HttpEntity entity = response.getEntity(); httpStatusLine = response.getStatusLine().toString(); if (entity != null) { return EntityUtils.toByteArray(entity); } else { return null; } } }; byte[] result = client.execute(httpPost, handler); IppResponse ippResponse = new IppResponse(); ippResult = ippResponse.getResponse(ByteBuffer.wrap(result)); ippResult.setHttpStatusResponse(httpStatusLine); // IppResultPrinter.print(ippResult); client.getConnectionManager().shutdown(); return ippResult; }
From source file:wsattacker.plugin.intelligentdos.requestSender.Http4RequestSenderImpl.java
private void setParamsToClient(HttpClient client) { int timeout = TIMEOUT; if (httpConnectionTimeout > 0) { timeout = httpConnectionTimeout; }//w w w .ja v a 2s . c o m HttpParams params = client.getParams(); params.setParameter("http.socket.timeout", timeout); params.setParameter("http.connection.timeout", timeout); params.setParameter("http.connection-manager.max-per-host", 3000); params.setParameter("http.connection-manager.max-total", 3000); HttpConnectionParams.setConnectionTimeout(params, timeout); HttpConnectionParams.setSoTimeout(params, timeout); }
From source file:org.madsonic.service.VersionService.java
/** * Resolves the latest available Subsonic version by screen-scraping a web page. * * @throws IOException If an I/O error occurs. *///from w w w. j ava2 s .co m private void readLatestVersion() throws IOException { HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); HttpConnectionParams.setSoTimeout(client.getParams(), 10000); HttpGet method = new HttpGet(VERSION_URL); String content; try { ResponseHandler<String> responseHandler = new BasicResponseHandler(); content = client.execute(method, responseHandler); } finally { client.getConnectionManager().shutdown(); } BufferedReader reader = new BufferedReader(new StringReader(content)); Pattern finalPattern = Pattern.compile("MADSONIC_FULL_VERSION_BEGIN_(.*)_MADSONIC_FULL_VERSION_END"); Pattern betaPattern = Pattern.compile("MADSONIC_BETA_VERSION_BEGIN_(.*)_MADSONIC_BETA_VERSION_END"); try { String line = reader.readLine(); while (line != null) { Matcher finalMatcher = finalPattern.matcher(line); if (finalMatcher.find()) { latestFinalVersion = new Version(finalMatcher.group(1)); LOG.info("Resolved latest Madsonic final version to: " + latestFinalVersion); } Matcher betaMatcher = betaPattern.matcher(line); if (betaMatcher.find()) { latestBetaVersion = new Version(betaMatcher.group(1)); LOG.info("Resolved latest Madsonic beta version to: " + latestBetaVersion); } line = reader.readLine(); } } finally { reader.close(); } }