use of org.apache.commons.httpclient.params.HttpClientParams in project dianping-open-sdk by dianping.
the class ApiTool method requestApi.
public static String requestApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap) {
String queryString = getQueryString(appKey, secret, paramMap);
StringBuffer response = new StringBuffer();
HttpClientParams httpConnectionParams = new HttpClientParams();
httpConnectionParams.setConnectionManagerTimeout(1000);
HttpClient client = new HttpClient(httpConnectionParams);
HttpMethod method = new GetMethod(apiUrl);
try {
if (StringUtils.isNotBlank(queryString)) {
// Encode query string with UTF-8
String encodeQuery = URIUtil.encodeQuery(queryString, "UTF-8");
LOGGER.debug("Encoded Query:" + encodeQuery);
method.setQueryString(encodeQuery);
}
client.executeMethod(method);
BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
response.append(line).append(System.getProperty("line.separator"));
}
reader.close();
} catch (URIException e) {
LOGGER.error("Can not encode query: " + queryString + " with charset UTF-8. ", e);
} catch (IOException e) {
LOGGER.error("Request URL: " + apiUrl + " failed. ", e);
} finally {
method.releaseConnection();
}
return response.toString();
}
use of org.apache.commons.httpclient.params.HttpClientParams in project alfresco-repository by Alfresco.
the class ExplicitSolrStoreMappingWrapper method init.
public void init() {
httpClientFactory = (HttpClientFactory) beanFactory.getBean(wrapped.getHttpClientFactory());
random = new Random(123);
if ((wrapped.getNodes() == null) || (wrapped.getNodes().length == 0)) {
HttpClient httpClient = httpClientFactory.getHttpClient();
HttpClientParams params = httpClient.getParams();
// params.setBooleanParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true);
// httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("admin", "admin"));
httpClientsAndBaseURLs.add(new HttpClientAndBaseUrl(httpClient, wrapped.getBaseUrl()));
} else {
for (String node : wrapped.getNodes()) {
String nodeHost = httpClientFactory.getHost();
String nodePort = "" + httpClientFactory.getPort();
String nodeBaseUrl = wrapped.getBaseUrl();
if (node.length() > 0) {
int colon = node.indexOf(':');
int forward = (colon > -1) ? node.indexOf('/', colon) : node.indexOf('/');
if (colon == -1) {
if (forward == -1) {
// single value
if (node.startsWith("/")) {
nodeBaseUrl = node;
}
try {
int port = Integer.parseInt(node);
nodePort = "" + port;
} catch (NumberFormatException nfe) {
nodeHost = node;
}
} else {
try {
String potentialPort = node.substring(0, forward);
if (potentialPort.length() > 0) {
int port = Integer.parseInt(potentialPort);
nodePort = "" + port;
}
} catch (NumberFormatException nfe) {
nodeHost = node.substring(0, forward);
}
nodeBaseUrl = node.substring(forward);
}
} else {
if (forward == -1) {
if (colon > 0) {
nodeHost = node.substring(0, colon);
}
if (colon + 1 < node.length()) {
String port = node.substring(colon + 1);
if (port.length() > 0) {
nodePort = port;
}
}
} else {
if (colon > 0) {
nodeHost = node.substring(0, colon);
}
String port = node.substring(colon + 1, forward);
if (port.length() > 0) {
nodePort = port;
}
nodeBaseUrl = node.substring(forward);
}
}
}
try {
int realPort = Integer.parseInt(nodePort);
httpClientsAndBaseURLs.add(new HttpClientAndBaseUrl(httpClientFactory.getHttpClient(nodeHost, realPort), nodeBaseUrl));
} catch (NumberFormatException nfe) {
httpClientsAndBaseURLs.add(new HttpClientAndBaseUrl(httpClientFactory.getHttpClient(nodeHost, httpClientFactory.getPort()), nodeBaseUrl));
}
}
}
policy = new ExplicitShardingPolicy(wrapped.getNumShards(), wrapped.getReplicationFactor(), httpClientsAndBaseURLs.size());
}
Aggregations