List of usage examples for org.apache.commons.httpclient.params HttpMethodParams HTTP_CONTENT_CHARSET
String HTTP_CONTENT_CHARSET
To view the source code for org.apache.commons.httpclient.params HttpMethodParams HTTP_CONTENT_CHARSET.
Click Source Link
From source file:net.duckling.ddl.web.bean.ClbHelper.java
public static String getClbToken(int docId, int version, Properties properties) { HttpClient client = HttpClientUtil.getHttpClient(LOGGER); PostMethod method = new PostMethod(getClbTokenUrl(properties)); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); method.addParameter("appname", properties.getProperty("duckling.clb.aone.user")); method.addParameter("docid", docId + ""); method.addParameter("version", version + ""); try {/*from www. j av a2 s .com*/ method.addParameter("password", Base64.encodeBytes(properties.getProperty("duckling.clb.aone.password").getBytes("utf-8"))); } catch (IllegalArgumentException | UnsupportedEncodingException e) { } try { int status = client.executeMethod(method); String responseString = null; if (status < 400) { responseString = method.getResponseBodyAsString(); org.json.JSONObject j = new org.json.JSONObject(responseString); Object st = j.get("status"); if ("failed".equals(st)) { LOGGER.error("?clb token?"); return null; } else { return j.get("pf").toString(); } } else { LOGGER.error("STAUTS:" + status + ";MESSAGE:" + responseString); } } catch (HttpException e) { LOGGER.error("", e); } catch (IOException e) { LOGGER.error("", e); } catch (ParseException e) { LOGGER.error("", e); } return null; }
From source file:com.glaf.core.util.http.CommonsHttpClientUtils.java
/** * ??GET//from w w w .j a v a 2s . c o m * * @param url * ?? * @param encoding * * @param dataMap * ? * * @return */ public static String doGet(String url, String encoding, Map<String, String> dataMap) { GetMethod method = null; String content = null; try { method = new GetMethod(url); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding); method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding); if (dataMap != null && !dataMap.isEmpty()) { NameValuePair[] nameValues = new NameValuePair[dataMap.size()]; int i = 0; for (Map.Entry<String, String> entry : dataMap.entrySet()) { String name = entry.getKey().toString(); String value = entry.getValue(); nameValues[i] = new NameValuePair(name, value); i++; } method.setQueryString(nameValues); } HttpClient client = new HttpClient(); int status = client.executeMethod(method); if (status == HttpStatus.SC_OK) { content = method.getResponseBodyAsString(); } } catch (IOException ex) { throw new RuntimeException(ex); } finally { if (method != null) { method.releaseConnection(); method = null; } } return content; }
From source file:colt.nicity.performance.agent.LatentHttpPump.java
private org.apache.commons.httpclient.HttpClient createApacheClient(String host, int port, int maxConnections, int socketTimeoutInMillis) { HttpConnectionManager connectionManager = createConnectionManager(maxConnections); org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient( connectionManager);//w ww .j ava 2 s .c o m client.getParams().setParameter(HttpMethodParams.COOKIE_POLICY, CookiePolicy.RFC_2109); client.getParams().setParameter(HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); client.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); client.getParams().setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, true); client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, socketTimeoutInMillis > 0 ? socketTimeoutInMillis : 0); client.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, socketTimeoutInMillis > 0 ? socketTimeoutInMillis : 0); HostConfiguration hostConfiguration = new HostConfiguration(); configureSsl(hostConfiguration, host, port); configureProxy(hostConfiguration); client.setHostConfiguration(hostConfiguration); return client; }
From source file:com.panoramagl.downloaders.PLHTTPFileDownloader.java
/**download methods*/ @Override/*from w ww. j a va2 s .c om*/ protected byte[] downloadFile() { this.setRunning(true); byte[] result = null; InputStream is = null; ByteArrayOutputStream bas = null; String url = this.getURL(); PLFileDownloaderListener listener = this.getListener(); boolean hasListener = (listener != null); int responseCode = -1; long startTime = System.currentTimeMillis(); // HttpClient instance HttpClient client = new HttpClient(); // Method instance HttpMethod method = new GetMethod(url); // Method parameters HttpMethodParams methodParams = method.getParams(); methodParams.setParameter(HttpMethodParams.USER_AGENT, "PanoramaGL Android"); methodParams.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); methodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(this.getMaxAttempts(), false)); try { // Execute the method responseCode = client.executeMethod(method); if (responseCode != HttpStatus.SC_OK) throw new IOException(method.getStatusText()); // Get content length Header header = method.getRequestHeader("Content-Length"); long contentLength = (header != null ? Long.parseLong(header.getValue()) : 1); if (this.isRunning()) { if (hasListener) listener.didBeginDownload(url, startTime); } else throw new PLRequestInvalidatedException(url); // Get response body as stream is = method.getResponseBodyAsStream(); bas = new ByteArrayOutputStream(); byte[] buffer = new byte[256]; int length = 0, total = 0; // Read stream while ((length = is.read(buffer)) != -1) { if (this.isRunning()) { bas.write(buffer, 0, length); total += length; if (hasListener) listener.didProgressDownload(url, (int) (((float) total / (float) contentLength) * 100.0f)); } else throw new PLRequestInvalidatedException(url); } if (total == 0) throw new IOException("Request data has invalid size (0)"); // Get data if (this.isRunning()) { result = bas.toByteArray(); if (hasListener) listener.didEndDownload(url, result, System.currentTimeMillis() - startTime); } else throw new PLRequestInvalidatedException(url); } catch (Throwable e) { if (this.isRunning()) { PLLog.error("PLHTTPFileDownloader::downloadFile", e); if (hasListener) listener.didErrorDownload(url, e.toString(), responseCode, result); } } finally { if (bas != null) { try { bas.close(); } catch (IOException e) { PLLog.error("PLHTTPFileDownloader::downloadFile", e); } } if (is != null) { try { is.close(); } catch (IOException e) { PLLog.error("PLHTTPFileDownloader::downloadFile", e); } } // Release the connection method.releaseConnection(); } this.setRunning(false); return result; }
From source file:com.cloudmaster.cmp.util.AlarmSystem.transfer.HttpSender.java
public ResponseObject send(TransportObject object) throws Exception { ResponseObject rs = new ResponseObject(); ByteArrayOutputStream bOs = null; DataOutputStream dOs = null;/* w w w .ja v a2s . co m*/ DataInputStream dIs = null; HttpClient client; PostMethod meth = null; byte[] rawData; try { bOs = new ByteArrayOutputStream(); dOs = new DataOutputStream(bOs); object.toStream(dOs); bOs.flush(); rawData = bOs.toByteArray(); client = new HttpClient(); client.setConnectionTimeout(this.timeout); client.setTimeout(this.datatimeout); client.setHttpConnectionFactoryTimeout(this.timeout); meth = new PostMethod(object.getValue(SERVER_URL)); // meth = new UTF8PostMethod(url); meth.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, ENCODING); // meth.addParameter(SERVER_ARGS, new String(rawData,"UTF-8")); // meth.setRequestBody(new String(rawData)); // meth.setRequestBody(new String(rawData,"UTF-8")); byte[] base64Array = Base64.encode(rawData).getBytes(); meth.setRequestBody(new String(base64Array)); // System.out.println(new String(rawData)); client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false)); client.executeMethod(meth); dIs = new DataInputStream(meth.getResponseBodyAsStream()); if (meth.getStatusCode() == HttpStatus.SC_OK) { Header errHeader = meth.getResponseHeader(HDR_ERROR); if (errHeader != null) { rs.setError(meth.getResponseBodyAsString()); return rs; } rs = ResponseObject.fromStream(dIs); return rs; } else { meth.releaseConnection(); throw new IOException("Connection failure: " + meth.getStatusLine().toString()); } } finally { if (meth != null) { meth.releaseConnection(); } if (bOs != null) { bOs.close(); } if (dOs != null) { dOs.close(); } if (dIs != null) { dIs.close(); } } }
From source file:com.glaf.core.util.http.CommonsHttpClientUtils.java
/** * ??POST/*from w w w . ja v a 2s . c om*/ * * @param url * ?? * @param encoding * * @param dataMap * ? * * @return */ public static String doPost(String url, String encoding, Map<String, String> dataMap) { PostMethod method = null; String content = null; try { method = new PostMethod(url); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding); method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding); if (dataMap != null && !dataMap.isEmpty()) { NameValuePair[] nameValues = new NameValuePair[dataMap.size()]; int i = 0; for (Map.Entry<String, String> entry : dataMap.entrySet()) { String name = entry.getKey().toString(); String value = entry.getValue(); nameValues[i] = new NameValuePair(name, value); i++; } method.setRequestBody(nameValues); } HttpClient client = new HttpClient(); int status = client.executeMethod(method); if (status == HttpStatus.SC_OK) { content = method.getResponseBodyAsString(); } } catch (IOException ex) { throw new RuntimeException(ex); } finally { if (method != null) { method.releaseConnection(); method = null; } } return content; }
From source file:com.jivesoftware.os.jive.utils.http.client.HttpClientFactoryProvider.java
public HttpClientFactory createHttpClientFactory(final Collection<HttpClientConfiguration> configurations) { return new HttpClientFactory() { @Override// w w w .ja v a 2 s . c o m public HttpClient createClient(String host, int port) { ApacheHttpClient31BackedHttpClient httpClient = createApacheClient(); HostConfiguration hostConfiguration = new HostConfiguration(); configureSsl(hostConfiguration, host, port, httpClient); configureProxy(hostConfiguration, httpClient); httpClient.setHostConfiguration(hostConfiguration); configureOAuth(httpClient); return httpClient; } private ApacheHttpClient31BackedHttpClient createApacheClient() { HttpClientConfig httpClientConfig = locateConfig(HttpClientConfig.class, HttpClientConfig.newBuilder().build()); HttpConnectionManager connectionManager = createConnectionManager(httpClientConfig); org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient( connectionManager); client.getParams().setParameter(HttpMethodParams.COOKIE_POLICY, CookiePolicy.RFC_2109); client.getParams().setParameter(HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); client.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); client.getParams().setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, true); client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, httpClientConfig.getSocketTimeoutInMillis() > 0 ? httpClientConfig.getSocketTimeoutInMillis() : 0); client.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, httpClientConfig.getSocketTimeoutInMillis() > 0 ? httpClientConfig.getSocketTimeoutInMillis() : 0); return new ApacheHttpClient31BackedHttpClient(client, httpClientConfig.getCopyOfHeadersForEveryRequest()); } @SuppressWarnings("unchecked") private <T> T locateConfig(Class<? extends T> _class, T defaultConfiguration) { for (HttpClientConfiguration configuration : configurations) { if (_class.isInstance(configuration)) { return (T) configuration; } } return defaultConfiguration; } private boolean hasValidProxyUsernameAndPasswordSettings(HttpClientProxyConfig httpClientProxyConfig) { return StringUtils.isNotBlank(httpClientProxyConfig.getProxyUsername()) && StringUtils.isNotBlank(httpClientProxyConfig.getProxyPassword()); } private HttpConnectionManager createConnectionManager(HttpClientConfig config) { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); if (config.getMaxConnectionsPerHost() > 0) { connectionManager.getParams() .setDefaultMaxConnectionsPerHost(config.getMaxConnectionsPerHost()); } else { connectionManager.getParams().setDefaultMaxConnectionsPerHost(Integer.MAX_VALUE); } if (config.getMaxConnections() > 0) { connectionManager.getParams().setMaxTotalConnections(config.getMaxConnections()); } return connectionManager; } private void configureOAuth(ApacheHttpClient31BackedHttpClient httpClient) { HttpClientOAuthConfig httpClientOAuthConfig = locateConfig(HttpClientOAuthConfig.class, null); if (httpClientOAuthConfig != null) { String serviceName = httpClientOAuthConfig.getServiceName(); HttpClientConsumerKeyAndSecretProvider consumerKeyAndSecretProvider = httpClientOAuthConfig .getConsumerKeyAndSecretProvider(); String consumerKey = consumerKeyAndSecretProvider.getConsumerKey(serviceName); if (StringUtils.isEmpty(consumerKey)) { throw new RuntimeException( "could create oauth client because consumerKey is null or empty for service:" + serviceName); } String consumerSecret = consumerKeyAndSecretProvider.getConsumerSecret(serviceName); if (StringUtils.isEmpty(consumerSecret)) { throw new RuntimeException( "could create oauth client because consumerSecret is null or empty for service:" + serviceName); } httpClient.setConsumerTokens(consumerKey, consumerSecret); } } private void configureProxy(HostConfiguration hostConfiguration, ApacheHttpClient31BackedHttpClient httpClient) { HttpClientProxyConfig httpClientProxyConfig = locateConfig(HttpClientProxyConfig.class, null); if (httpClientProxyConfig != null) { hostConfiguration.setProxy(httpClientProxyConfig.getProxyHost(), httpClientProxyConfig.getProxyPort()); if (hasValidProxyUsernameAndPasswordSettings(httpClientProxyConfig)) { HttpState state = new HttpState(); state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(httpClientProxyConfig.getProxyUsername(), httpClientProxyConfig.getProxyPassword())); httpClient.setState(state); } } } private void configureSsl(HostConfiguration hostConfiguration, String host, int port, ApacheHttpClient31BackedHttpClient httpClient) throws IllegalStateException { HttpClientSSLConfig httpClientSSLConfig = locateConfig(HttpClientSSLConfig.class, null); if (httpClientSSLConfig != null) { Protocol sslProtocol; if (httpClientSSLConfig.getCustomSSLSocketFactory() != null) { sslProtocol = new Protocol(HTTPS_PROTOCOL, new CustomSecureProtocolSocketFactory( httpClientSSLConfig.getCustomSSLSocketFactory()), SSL_PORT); } else { sslProtocol = Protocol.getProtocol(HTTPS_PROTOCOL); } hostConfiguration.setHost(host, port, sslProtocol); httpClient.setUsingSSL(); } else { hostConfiguration.setHost(host, port); } } }; }
From source file:com.cloudmaster.cmp.util.AlarmSystem.transfer.HttpSender.java
public ResponseObject send(Object object, Map<String, String> paramMap) throws Exception { ResponseObject rs = new ResponseObject(); ByteArrayOutputStream bOs = null; DataOutputStream dOs = null;//from ww w . j a va 2 s .com DataInputStream dIs = null; HttpClient client; PostMethod meth = null; byte[] rawData; try { client = new HttpClient(); client.setConnectionTimeout(this.timeout); client.setTimeout(this.datatimeout); client.setHttpConnectionFactoryTimeout(this.timeout); meth = new PostMethod(paramMap.get("SERVER_URL")); // meth = new UTF8PostMethod(url); meth.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, ENCODING); // meth.addParameter(SERVER_ARGS, new String(rawData,"UTF-8")); meth.setRequestBody(object.toString()); System.out.println(object.toString()); /** * "type"="ruleSync",XML? "syncType"="***" * 1??2??3? "ruleName"="***" * XML?XML???XML */ meth.addRequestHeader("type", paramMap.get("type")); meth.addRequestHeader("syncType", paramMap.get("syncType")); meth.addRequestHeader("ruleName", URLEncoder.encode(paramMap.get("ruleName"), "UTF-8")); client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false)); client.executeMethod(meth); dIs = new DataInputStream(meth.getResponseBodyAsStream()); if (meth.getStatusCode() == HttpStatus.SC_OK) { Header errHeader = meth.getResponseHeader(HDR_ERROR); if (errHeader != null) { rs.setError(meth.getResponseBodyAsString()); return rs; } rs = ResponseObject.fromStream(dIs); return rs; } else { meth.releaseConnection(); throw new IOException("Connection failure: " + meth.getStatusLine().toString()); } } finally { if (meth != null) { meth.releaseConnection(); } if (bOs != null) { bOs.close(); } if (dOs != null) { dOs.close(); } if (dIs != null) { dIs.close(); } } }
From source file:cn.vlabs.duckling.aone.client.impl.EmailAttachmentSenderImpl.java
/** * ?DDL/*from www .ja v a 2 s. c om*/ * @param email * @param attachment * @return */ private AttachmentPushResult validateExsit(String email, int teamId, AttachmentInfo attachment) { if (attachment.getCoverFlag()) { return null; } PostMethod method = new PostMethod(getDDLValidateIp()); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); method.addParameter(FILENAME, attachment.getFileName()); method.addParameter(EMAIL, email); method.addParameter(FILESIZE, attachment.getFileSize() + ""); method.addParameter(FILEID, attachment.getFileId()); method.addParameter(TEAMID, teamId + ""); AttachmentPushResult result = new AttachmentPushResult(); result.setFileName(attachment.getFileName()); try { int status = ddlClient.executeMethod(method); if (status >= 200 && status < 300) { String responseString = method.getResponseBodyAsString(); return dealValidateResult(responseString); } else { result.setStatusCode(AttachmentPushResult.NETWORK_ERROR); result.setMessage("DDL?" + status + ""); return result; } } catch (HttpException e) { result.setStatusCode(AttachmentPushResult.NETWORK_ERROR); result.setMessage("DDL?" + e.getMessage() + ""); return result; } catch (IOException e) { result.setStatusCode(AttachmentPushResult.IO_ERROR); result.setMessage("ddlIo"); e.printStackTrace(); return result; } finally { method.releaseConnection(); } }
From source file:com.zimbra.cs.service.FeedManager.java
private static RemoteDataInfo retrieveRemoteData(String url, Folder.SyncData fsd) throws ServiceException, HttpException, IOException { assert !Strings.isNullOrEmpty(url); HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient(); HttpProxyUtil.configureProxy(client); // cannot set connection timeout because it'll affect all HttpClients associated with the conn mgr. // see comments in ZimbraHttpConnectionManager // client.setConnectionTimeout(10000); HttpMethodParams params = new HttpMethodParams(); params.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, MimeConstants.P_CHARSET_UTF8); params.setSoTimeout(60000);//from ww w . j av a 2 s .c om GetMethod get = null; BufferedInputStream content = null; long lastModified = 0; String expectedCharset = MimeConstants.P_CHARSET_UTF8; int redirects = 0; int statusCode = HttpServletResponse.SC_NOT_FOUND; try { do { String lcurl = url.toLowerCase(); if (lcurl.startsWith("webcal:")) { url = "http:" + url.substring(7); } else if (lcurl.startsWith("feed:")) { url = "http:" + url.substring(5); } else if (!lcurl.startsWith("http:") && !lcurl.startsWith("https:")) { throw ServiceException.INVALID_REQUEST("url must begin with http: or https:", null); } // username and password are encoded in the URL as http://user:pass@host/... if (url.indexOf('@') != -1) { HttpURL httpurl = lcurl.startsWith("https:") ? new HttpsURL(url) : new HttpURL(url); if (httpurl.getUser() != null) { String user = httpurl.getUser(); if (user.indexOf('%') != -1) { try { user = URLDecoder.decode(user, "UTF-8"); } catch (OutOfMemoryError e) { Zimbra.halt("out of memory", e); } catch (Throwable t) { } } UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, httpurl.getPassword()); client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(AuthScope.ANY, creds); } } try { get = new GetMethod(url); } catch (OutOfMemoryError e) { Zimbra.halt("out of memory", e); return null; } catch (Throwable t) { throw ServiceException.INVALID_REQUEST("invalid url for feed: " + url, t); } get.setParams(params); get.setFollowRedirects(true); get.setDoAuthentication(true); get.addRequestHeader("User-Agent", HTTP_USER_AGENT); get.addRequestHeader("Accept", HTTP_ACCEPT); if (fsd != null && fsd.getLastSyncDate() > 0) { String lastSyncAt = org.apache.commons.httpclient.util.DateUtil .formatDate(new Date(fsd.getLastSyncDate())); get.addRequestHeader("If-Modified-Since", lastSyncAt); } HttpClientUtil.executeMethod(client, get); Header locationHeader = get.getResponseHeader("location"); if (locationHeader != null) { // update our target URL and loop again to do another HTTP GET url = locationHeader.getValue(); get.releaseConnection(); } else { statusCode = get.getStatusCode(); if (statusCode == HttpServletResponse.SC_OK) { Header contentEncoding = get.getResponseHeader("Content-Encoding"); InputStream respInputStream = get.getResponseBodyAsStream(); if (contentEncoding != null) { if (contentEncoding.getValue().indexOf("gzip") != -1) { respInputStream = new GZIPInputStream(respInputStream); } } content = new BufferedInputStream(respInputStream); expectedCharset = get.getResponseCharSet(); Header lastModHdr = get.getResponseHeader("Last-Modified"); if (lastModHdr == null) { lastModHdr = get.getResponseHeader("Date"); } if (lastModHdr != null) { try { Date d = org.apache.commons.httpclient.util.DateUtil .parseDate(lastModHdr.getValue()); lastModified = d.getTime(); } catch (DateParseException e) { ZimbraLog.misc.warn( "unable to parse Last-Modified/Date header: " + lastModHdr.getValue(), e); lastModified = System.currentTimeMillis(); } } else { lastModified = System.currentTimeMillis(); } } else if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) { ZimbraLog.misc.debug("Remote data at " + url + " not modified since last sync"); return new RemoteDataInfo(statusCode, redirects, null, expectedCharset, lastModified); } else { throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusLine().toString(), null); } break; } } while (++redirects <= MAX_REDIRECTS); } catch (ServiceException ex) { if (get != null) { get.releaseConnection(); } throw ex; } catch (HttpException ex) { if (get != null) { get.releaseConnection(); } throw ex; } catch (IOException ex) { if (get != null) { get.releaseConnection(); } throw ex; } RemoteDataInfo rdi = new RemoteDataInfo(statusCode, redirects, content, expectedCharset, lastModified); rdi.setGetMethod(get); return rdi; }