use of com.ning.http.client.AsyncHttpClient in project protools by SeanDragon.
the class ToolHttp method sendHttp.
/**
* 用于请求http
*
* @param send
* 里面包含请求的信息
*
* @return 响应的信息
*/
public static HttpReceive sendHttp(HttpSend send, HttpBuilder httpBuilder) {
HttpReceive httpReceive = new HttpReceive();
httpReceive.setHaveError(true);
String url = send.getUrl();
URI uri;
try {
uri = new URL(url).toURI();
} catch (MalformedURLException | URISyntaxException e) {
httpReceive.setErrMsg("不是一个有效的URL").setThrowable(e);
return httpReceive;
}
String scheme = uri.getScheme();
String host = uri.getHost();
int port = uri.getPort();
if (port == -1) {
if (HttpScheme.HTTP.equalsIgnoreCase(scheme)) {
port = 80;
} else if (HttpScheme.HTTPS.equalsIgnoreCase(scheme)) {
port = 443;
}
}
Map<String, Object> param = send.getParams();
Map<String, Object> sendHeaders = send.getHeaders();
HttpMethod method = send.getMethod();
Charset charset = send.getCharset();
AsyncHttpClient asyncHttpClient = httpBuilder.buildDefaultClient();
RequestBuilder builder = new RequestBuilder(method.name());
AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.prepareRequest(builder.build());
// 设置编码
requestBuilder.setBodyEncoding(charset.toString());
if (param != null) {
param.forEach((key, value) -> requestBuilder.addQueryParam(key, value.toString()));
}
// 设置基本请求头
requestBuilder.addHeader(HttpHeaderNames.CONTENT_TYPE.toString(), HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED + ";charset" + charset.toString()).addHeader(HttpHeaderNames.CONNECTION.toString(), HttpHeaderValues.KEEP_ALIVE.toString()).addHeader(HttpHeaderNames.ACCEPT_ENCODING.toString(), HttpHeaderValues.GZIP_DEFLATE.toString()).addHeader(HttpHeaderNames.USER_AGENT.toString(), "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)").addHeader(HttpHeaderNames.CACHE_CONTROL.toString(), "max-age=0").addHeader(HttpHeaderNames.HOST.toString(), host + ":" + port).addHeader("DNT", "1");
if (sendHeaders != null) {
sendHeaders.forEach((key, value) -> requestBuilder.addHeader(key, value.toString()));
}
// TODO: 2017/7/27 Cookie未加入
ListenableFuture<Response> future = requestBuilder.execute();
try {
Response response = future.get();
Map<String, String> responseHeaderMap = Maps.newHashMap();
if (send.getNeedReceiveHeaders()) {
FluentCaseInsensitiveStringsMap responseHeaders = response.getHeaders();
responseHeaders.forEach((k, v) -> {
if (v.size() == 1) {
responseHeaderMap.put(k, v.get(0));
} else {
responseHeaderMap.put(k, ToolJson.anyToJson(v));
}
});
}
int responseStatusCode = response.getStatusCode();
if (responseStatusCode != 200) {
throw new HttpException("本次请求响应码不是200,是" + responseStatusCode);
}
String responseBody = response.getResponseBody();
if (log.isDebugEnabled()) {
log.debug(responseBody);
}
httpReceive.setStatusCode(responseStatusCode).setStatusText(response.getStatusText()).setResponseBody(responseBody).setResponseHeader(responseHeaderMap).setHaveError(false);
} catch (InterruptedException e) {
httpReceive.setErrMsg("http组件出现问题!").setThrowable(e);
} catch (IOException e) {
httpReceive.setErrMsg("获取返回内容失败!").setThrowable(e);
} catch (ExecutionException e) {
httpReceive.setErrMsg("访问URL失败!").setThrowable(e);
} catch (HttpException e) {
httpReceive.setErrMsg(e.getMessage()).setThrowable(e);
}
if (httpReceive.getHaveError()) {
if (log.isWarnEnabled()) {
Throwable throwable = httpReceive.getThrowable();
log.warn(ToolFormat.toException(throwable), throwable);
}
}
httpReceive.setIsDone(true);
return httpReceive;
}
use of com.ning.http.client.AsyncHttpClient in project tez by apache.
the class AsyncHttpConnection method initClient.
private void initClient(HttpConnectionParams httpConnParams) throws IOException {
if (httpAsyncClient != null) {
return;
}
if (httpAsyncClient == null) {
synchronized (AsyncHttpConnection.class) {
if (httpAsyncClient == null) {
LOG.info("Initializing AsyncClient (TezBodyDeferringAsyncHandler)");
AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
if (httpConnParams.isSslShuffle()) {
// Configure SSL
SSLFactory sslFactory = httpConnParams.getSslFactory();
Preconditions.checkArgument(sslFactory != null, "SSLFactory can not be null");
sslFactory.configure(builder);
}
/**
* TODO : following settings need fine tuning.
* Change following config to accept common thread pool later.
* Change max connections based on the total inputs (ordered & unordered). Need to tune
* setMaxConnections & addRequestFilter.
*/
builder.setAllowPoolingConnection(httpConnParams.isKeepAlive()).setAllowSslConnectionPool(httpConnParams.isKeepAlive()).setCompressionEnabled(false).setMaximumConnectionsPerHost(1).setConnectionTimeoutInMs(httpConnParams.getConnectionTimeout()).setRequestTimeoutInMs(httpConnParams.getReadTimeout()).setUseRawUrl(true).build();
httpAsyncClient = new AsyncHttpClient(builder.build());
}
}
}
}
use of com.ning.http.client.AsyncHttpClient in project scheduling by ow2-proactive.
the class SchedulerEventReceiver method openAndReceive.
@SuppressWarnings("rawtypes")
private void openAndReceive(final SchedulerEventListener eventListener, boolean myEventsOnly, SchedulerEvent... events) throws IOException {
Client client = ClientFactory.getDefault().newClient();
AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
if (insecure) {
builder = builder.setAcceptAnyCertificate(true).setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}
AsyncHttpClientConfig httpClientConfig = builder.build();
AsyncHttpClient httpClient = new AsyncHttpClient(httpClientConfig);
socket = client.create(client.newOptionsBuilder().runtime(httpClient).reconnect(false).build());
EventNotificationHandler notificationHandler = new EventNotificationHandler(eventListener);
socket.on(Event.MESSAGE, notificationHandler);
socket.on(Event.CLOSE, new Function() {
public void on(Object t) {
SchedulerEventReceiver.logger.info("#### Websocket connection is closed ####");
if (eventListener instanceof DisconnectionAwareSchedulerEventListener) {
((DisconnectionAwareSchedulerEventListener) eventListener).notifyDisconnection();
}
}
});
// initialize the connection
RequestBuilder requestBuilder = client.newRequestBuilder();
requestBuilder.method(Request.METHOD.GET);
requestBuilder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
// authentication header
requestBuilder.header("sessionid", sessionId);
requestBuilder.uri(eventingUrl(restServerUrl));
requestBuilder.encoder(new EventSubscriptionEncoder());
requestBuilder.decoder(new EventNotificationDecoder());
requestBuilder.transport(Request.TRANSPORT.WEBSOCKET);
socket.open(requestBuilder.build());
// submit subscription request
EventSubscription eventSubscription = new EventSubscription(myEventsOnly, asStringArray(events));
socket.fire(EventCodecUtil.toJsonString(eventSubscription));
}
use of com.ning.http.client.AsyncHttpClient in project jersey by jersey.
the class UnderlyingHttpClientAccessTest method testHttpClientInstanceAccess.
/**
* Verifier of JERSEY-2424 fix.
*/
@Test
public void testHttpClientInstanceAccess() {
final Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new GrizzlyConnectorProvider()));
final AsyncHttpClient hcOnClient = GrizzlyConnectorProvider.getHttpClient(client);
// important: the web target instance in this test must be only created AFTER the client has been pre-initialized
// (see org.glassfish.jersey.client.Initializable.preInitialize method). This is here achieved by calling the
// connector provider's static getHttpClient method above.
final WebTarget target = client.target("http://localhost/");
final AsyncHttpClient hcOnTarget = GrizzlyConnectorProvider.getHttpClient(target);
assertNotNull("HTTP client instance set on JerseyClient should not be null.", hcOnClient);
assertNotNull("HTTP client instance set on JerseyWebTarget should not be null.", hcOnTarget);
assertSame("HTTP client instance set on JerseyClient should be the same instance as the one set on JerseyWebTarget" + "(provided the target instance has not been further configured).", hcOnClient, hcOnTarget);
}
use of com.ning.http.client.AsyncHttpClient in project cdap by caskdata.
the class NettyRouterPipelineTest method testChunkRequestSuccess.
@Test
public void testChunkRequestSuccess() throws Exception {
AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
byte[] requestBody = generatePostData();
final Request request = new RequestBuilder("POST").setUrl(String.format("http://%s:%d%s", HOSTNAME, ROUTER.getServiceMap().get(GATEWAY_NAME), "/v1/upload")).setContentLength(requestBody.length).setBody(new ByteEntityWriter(requestBody)).build();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Future<Void> future = asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {
@Override
public Void onCompleted(Response response) throws Exception {
return null;
}
@Override
public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
// TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(10));
content.writeTo(byteArrayOutputStream);
return super.onBodyPartReceived(content);
}
});
future.get();
Assert.assertArrayEquals(requestBody, byteArrayOutputStream.toByteArray());
}
Aggregations