org.vertx.java.core.http.HttpClient类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(144)

本文整理了Java中org.vertx.java.core.http.HttpClient类的一些代码示例,展示了HttpClient类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpClient类的具体详情如下:
包路径:org.vertx.java.core.http.HttpClient
类名称:HttpClient

HttpClient介绍

[英]An HTTP client that maintains a pool of connections to a specific host, at a specific port. The client supports pipelining of requests.

As well as HTTP requests, the client can act as a factory for WebSocket websockets.

If an instance is instantiated from an event loop then the handlers of the instance will always be called on that same event loop. If an instance is instantiated from some other arbitrary Java thread then and event loop will be assigned to the instance and used when any of its handlers are called.

Instances cannot be used from worker verticles
[中]在特定端口维护到特定主机的连接池的HTTP客户端。客户端支持请求的管道化。
除了HTTP请求,客户端还可以充当WebSocket WebSocket的工厂。
如果实例是从事件循环实例化的,那么实例的处理程序将始终在同一事件循环上被调用。如果一个实例是从其他任意Java线程实例化的,那么和事件循环将被分配给该实例,并在调用其任何处理程序时使用。
无法从辅助垂直线使用实例

代码示例

代码示例来源:origin: com.englishtown/vertx-mod-when

protected <T> HttpClient createClient(URI url, final Deferred<T> d) {
  if (url == null) throw new IllegalArgumentException("url is null");
  if (!url.isAbsolute()) throw new IllegalArgumentException("url must be absolute");
  int port = (url.getPort() > 0) ? url.getPort() : 80;
  return vertx.createHttpClient()
      .setHost(url.getHost())
      .setPort(port)
      .setConnectTimeout(CONNECT_TIMEOUT)
      .exceptionHandler(t -> d.reject(t));
}

代码示例来源:origin: io.vertx/mod-rxvertx

/** Convenience wrapper */
public void close() {
 this.core.close();
}

代码示例来源:origin: io.vertx/mod-rxvertx

public Observable<RxWebSocket> connectWebsocket(String uri, WebSocketVersion wsVersion, MultiMap headers) {
 final MemoizeHandler<RxWebSocket,WebSocket> rh=new MemoizeHandler<RxWebSocket,WebSocket>() {
  @Override
  public void handle(WebSocket s) {
   complete(new RxWebSocket(s));
  }
 };
 core.connectWebsocket(uri,wsVersion,headers,rh);
 return Observable.create(rh.subscribe);
}

代码示例来源:origin: stackoverflow.com

HttpClient client = vertx.createHttpClient();
 String proxyHost = System.getProperty("http.proxyHost", "none");
 Integer proxyPort = Integer.valueOf(System.getProperty("http.proxyPort", "80"));
 if(!"none".equalsIgnoreCase(proxyHost)){
   client.setHost(proxyHost);
   client.setPort(proxyPort);
 }

代码示例来源:origin: io.vertx/vertx-platform

protected HttpClient createClient(String scheme, String host, int port) {
 if (client != null) {
  throw new IllegalStateException("Client already created");
 }
 client = vertx.createHttpClient();
 client.setKeepAlive(false); // Not all servers will allow keep alive connections
 if (proxyHost != null) {
  client.setHost(proxyHost);
  if (proxyPort != 80) {
   client.setPort(proxyPort);
  } else {
   client.setPort(80);
  }
 } else {
  client.setHost(host);
  client.setPort(port);
 }
 if (scheme.equals("https")) {
  client.setSSL(true);
 }
 client.exceptionHandler(new Handler<Throwable>() {
  public void handle(Throwable t) {
   end(false);
  }
 });
 return client;
}

代码示例来源:origin: org.vert-x/vertx-platform

HttpClient client = vertx.createHttpClient();
if (proxyHost != null) {
 client.setHost(proxyHost);
 if (proxyPort != 80) {
  client.setPort(proxyPort);
 } else {
  client.setPort(80);
 client.setHost(repoHost);
 client.setPort(repoPort);
client.exceptionHandler(new Handler<Exception>() {
 public void handle(Exception e) {
  log.error("Unable to connect to repository");
 uri = new StringBuffer("http://").append(DEFAULT_REPO_HOST).append(uri).toString();
HttpClientRequest req = client.get(uri, new Handler<HttpClientResponse>() {
 public void handle(HttpClientResponse resp) {
  if (resp.statusCode == 200) {

代码示例来源:origin: jboss-fuse/fabric8

final HttpClient finalClient = client;
client.exceptionHandler(new Handler<Throwable>() {
  @Override
  public void handle(Throwable throwable) {
client.setConnectTimeout( connectionTimeout );
final HttpClientRequest clientRequest = client.request(request.method(), servicePath, responseHandler);
clientRequest.headers().set(request.headers());
clientRequest.setChunked(true);

代码示例来源:origin: vert-x/mod-lang-php

/**
 * Executes a request.
 */
public HttpClientRequest request(Env env, StringValue method, StringValue uri, Value handler) {
 PhpTypes.assertCallable(env, handler, "Argument to Vertx\\Http\\HttpClient::request() must be callable.");
 return new HttpClientRequest(client.request(method.toString(), uri.toString(), createResponseHandler(env, handler)));
}

代码示例来源:origin: vert-x/mod-lang-php

/**
 * Sets the HTTP host.
 */
public HttpClient host(Env env, StringValue host) {
 if (PhpTypes.notNull(host)) {
  client.setHost(host.toString());
 }
 else {
  client.setHost(null);
 }
 return this;
}

代码示例来源:origin: vert-x/mod-lang-php

/**
 * Sets the HTTP port.
 */
public HttpClient port(Env env, NumberValue port) {
 PhpTypes.assertNotNull(env, port, "Port to Vertx\\Http\\HttpClient::port() must be an integer.");
 client.setPort(port.toInt());
 return this;
}

代码示例来源:origin: vert-x/mod-lang-php

/**
 * Sets the client exception handler.
 */
public HttpClient exceptionHandler(Env env, Value handler) {
 PhpTypes.assertCallable(env, handler, "Argument to Vertx\\Http\\HttpClient::exceptionHandler() must be callable.");
 client.exceptionHandler(HandlerFactory.createExceptionHandler(env, handler));
 return this;
}

代码示例来源:origin: vert-x/mod-lang-php

/**
 * Sets the connection timeout.
 */
public HttpClient connectTimeout(Env env, NumberValue timeout) {
 PhpTypes.assertNotNull(env, timeout, "Timeout to Vertx\\Http\\HttpClient::connectTimeout() must be an integer.");
 client.setConnectTimeout(timeout.toInt());
 return this;
}

代码示例来源:origin: vert-x/mod-lang-php

/**
 * Executes a GET request.
 */
public HttpClientRequest get(Env env, StringValue uri, Value handler) {
 PhpTypes.assertCallable(env, handler, "Argument to Vertx\\Http\\HttpClient::get() must be callable.");
 return new HttpClientRequest(client.get(uri.toString(), createResponseHandler(env, handler)));
}

代码示例来源:origin: vert-x/mod-lang-php

/**
 * Sets the max connection pool size.
 */
public HttpClient maxPoolSize(Env env, NumberValue size) {
 PhpTypes.assertNotNull(env, size, "Size to Vertx\\Http\\HttpClient::maxPoolSize() must be an integer.");
 client.setMaxPoolSize(size.toInt());
 return this;
}

代码示例来源:origin: jboss-fuse/fabric8

protected HttpClient createClient(URL url) throws MalformedURLException {
  // lets create a client
  HttpClient client = vertx.createHttpClient();
  client.setHost(url.getHost());
  client.setPort(url.getPort());
  return client;
}

代码示例来源:origin: io.vertx/mod-rxvertx

HttpClientRequest req=core.request(method,uri,rh);

代码示例来源:origin: io.vertx/vertx-platform

protected void sendRequest(String scheme, String host, int port, String uri, Handler<HttpClientResponse> respHandler) {
 final String proxyHost = getProxyHost();
 if (proxyHost != null) {
  // We use an absolute URI
  uri = scheme + "://" + host + ":" + port + uri;
 }
 HttpClientRequest req = client.get(uri, respHandler);
 if (proxyHost != null){
  if (isUseDestinationHostHeaderForProxy()) {
   req.putHeader("host", host);
  } else {
   req.putHeader("host", proxyHost);
  }
 } else {
  req.putHeader("host", host);
 }
 if (getBasicAuth() != null) {
  log.debug("Using HTTP Basic Authorization");
  req.putHeader("Authorization","Basic " + getBasicAuth());
 }
 req.putHeader("user-agent", "Vert.x Module Installer");
 req.end();
}

代码示例来源:origin: boonproject/boon

private void connect() {
  int index = this.currentIndex.get();
  int oldIndex = index;
  if (index + 1 == hosts.length) {
    index = 0;
  } else {
    index++;
  }
  if (this.currentIndex.compareAndSet(oldIndex, index)) {
    final URI uri = this.hosts[index];
    logger.info("Connecting to ", uri);
    httpClient = vertx.createHttpClient().setHost(uri.getHost()).setPort(uri.getPort())
        .setConnectTimeout(this.timeOutInMilliseconds).setMaxPoolSize(poolSize);
    httpClient.exceptionHandler(new Handler<Throwable>() {
      @Override
      public void handle(Throwable throwable) {
        if (throwable instanceof ConnectException) {
          closed.set(true);
        } else {
          logger.error(throwable, "Unable to connect to ", uri);
        }
      }
    });
    configureSSL(httpClient);
    closed.set(false);
  }
}

代码示例来源:origin: liveoak-io/liveoak

@Override
public void createMember(RequestContext ctx, ResourceState state, Responder responder) {
  String path = (String) state.getProperty("path");
  String destination = (String) state.getProperty("destination");
  String contentType = (String) state.getProperty("content-type");
  if (contentType == null) {
    contentType = "application/json";
  }
  ResourceCodec codec = this.codecManager.getResourceCodec(new MediaType(contentType));
  if (codec == null) {
    responder.internalError("content-type not supported: " + contentType);
    return;
  }
  try {
    URI destinationUri = new URI(destination);
    HttpClient httpClient = this.vertx.createHttpClient();
    httpClient.setHost(destinationUri.getHost());
    httpClient.setPort(destinationUri.getPort());
    SecurityContext requestSecurityContext = ctx.securityContext();
    HttpSubscription sub = new HttpSubscription(httpClient, path, destinationUri, codec, requestSecurityContext);
    this.subscriptionManager.addSubscription(sub);
    responder.resourceCreated(new HttpSubscriptionResource(this, sub));
  } catch (URISyntaxException e) {
    responder.internalError(e.getMessage());
  }
}

代码示例来源:origin: boonproject/boon

final HttpClientRequest httpClientRequest = httpClient.request(request.getMethod(), request.uri(),
    handleResponse(request, responseHandler));

相关文章