jodd.http.HttpRequest.withConnectionProvider()方法的使用及代码示例

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

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

HttpRequest.withConnectionProvider介绍

[英]Uses custom connection provider when #open() the connection.
[中]在打开()连接时使用自定义连接提供程序。

代码示例

代码示例来源:origin: oblac/jodd

@Test
@Disabled
void testHttpProxy() {
  SocketHttpConnectionProvider s = new SocketHttpConnectionProvider();
  s.useProxy(ProxyInfo.httpProxy("localhost", 1090, null, null));
  HttpResponse response = HttpRequest.get("http://localhost:1080/get_books")
    .withConnectionProvider(s)
    .send();
  assertEquals(200, response.statusCode());
  assertTrue(response.body().contains("Tatum"));
}

代码示例来源:origin: oblac/jodd

@Test
void testSocks5Proxy() {
  SocketHttpConnectionProvider s = new SocketHttpConnectionProvider();
  s.useProxy(ProxyInfo.socks5Proxy("localhost", 1090, null, null));
  HttpResponse response = HttpRequest.get("http://localhost:1080/get_books")
    .withConnectionProvider(s)
    .send();
  assertEquals(200, response.statusCode());
  assertTrue(response.body().contains("Tatum"));
  proxy.verify(request().withPath("/get_books"), exactly(1));
}

代码示例来源:origin: oblac/jodd

@Test
void testSocks5ProxyWithHttps() {
  SocketHttpConnectionProvider s = new SocketHttpConnectionProvider();
  s.useProxy(ProxyInfo.socks5Proxy("localhost", 1090, null, null));
  HttpResponse response = HttpRequest.get("https://localhost:1080/get_books")
    .withConnectionProvider(s)
    .trustAllCerts(true)
    .send();
  assertEquals(200, response.statusCode());
  assertTrue(response.body().contains("Tatum"));
  proxy.verify(request().withPath("/get_books"), exactly(1));
}

代码示例来源:origin: binarywang/WxJava

@Override
 public File execute(String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
  if (ticket != null) {
   if (uri.indexOf('?') == -1) {
    uri += '?';
   }
   uri += uri.endsWith("?")
    ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")
    : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
  }

  HttpRequest request = HttpRequest.get(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String contentTypeHeader = response.header("Content-Type");
  if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) {
   String responseContent = response.bodyText();
   throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
  }
  try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
   return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
  }
 }
}

代码示例来源:origin: com.github.binarywang/weixin-java-mp

@Override
 public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException {
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }

  HttpRequest request = HttpRequest.post(uri)
   .withConnectionProvider(requestHttp.getRequestHttpClient())
   .body(WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);

  String responseContent = response.bodyText();
  this.logger.debug("响应原始数据:{}", responseContent);
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  } else {
   return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
  }
 }
}

代码示例来源:origin: binarywang/WxJava

@Override
 public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException {
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }

  HttpRequest request = HttpRequest.post(uri)
   .withConnectionProvider(requestHttp.getRequestHttpClient())
   .body(WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);

  String responseContent = response.bodyText();
  this.logger.debug("响应原始数据:{}", responseContent);
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  } else {
   return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
  }
 }
}

代码示例来源:origin: binarywang/WxJava

@Override
public String execute(String uri, String queryParam) throws WxErrorException, IOException {
 if (queryParam != null) {
  if (uri.indexOf('?') == -1) {
   uri += '?';
  }
  uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
 }
 HttpRequest request = HttpRequest.get(uri);
 if (requestHttp.getRequestHttpProxy() != null) {
  requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
 }
 request.withConnectionProvider(requestHttp.getRequestHttpClient());
 HttpResponse response = request.send();
 response.charset(StringPool.UTF_8);
 String responseContent = response.bodyText();
 WxError error = WxError.fromJson(responseContent);
 if (error.getErrorCode() != 0) {
  throw new WxErrorException(error);
 }
 return responseContent;
}

代码示例来源:origin: com.github.binarywang/weixin-java-common

@Override
public String execute(String uri, String queryParam) throws WxErrorException, IOException {
 if (queryParam != null) {
  if (uri.indexOf('?') == -1) {
   uri += '?';
  }
  uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
 }
 HttpRequest request = HttpRequest.get(uri);
 if (requestHttp.getRequestHttpProxy() != null) {
  requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
 }
 request.withConnectionProvider(requestHttp.getRequestHttpClient());
 HttpResponse response = request.send();
 response.charset(StringPool.UTF_8);
 String responseContent = response.bodyText();
 WxError error = WxError.fromJson(responseContent);
 if (error.getErrorCode() != 0) {
  throw new WxErrorException(error);
 }
 return responseContent;
}

代码示例来源:origin: com.github.binarywang/weixin-java-mp

requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
request.withConnectionProvider(requestHttp.getRequestHttpClient());

代码示例来源:origin: binarywang/WxJava

@Override
 public Boolean execute(String uri, String materialId) throws WxErrorException, IOException {
  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  request.query("media_id", materialId);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  } else {
   return true;
  }
 }
}

代码示例来源:origin: com.github.binarywang/weixin-java-mp

@Override
 public Boolean execute(String uri, String materialId) throws WxErrorException, IOException {
  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  request.query("media_id", materialId);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  } else {
   return true;
  }
 }
}

代码示例来源:origin: com.github.binarywang/weixin-java-pay

private HttpRequest buildHttpRequest(String url, String requestStr, boolean useKey) throws WxPayException {
 HttpRequest request = HttpRequest
  .post(url)
  .timeout(this.getConfig().getHttpTimeout())
  .connectionTimeout(this.getConfig().getHttpConnectionTimeout())
  .bodyText(requestStr);
 if (useKey) {
  SSLContext sslContext = this.getConfig().getSslContext();
  if (null == sslContext) {
   sslContext = this.getConfig().initSSLContext();
  }
  final SSLSocketHttpConnectionProvider provider = new SSLSocketHttpConnectionProvider(sslContext);
  request.withConnectionProvider(provider);
 }
 if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost()) && this.getConfig().getHttpProxyPort() > 0) {
  ProxyInfo httpProxy = new ProxyInfo(ProxyType.HTTP, this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort(),
   this.getConfig().getHttpProxyUsername(), this.getConfig().getHttpProxyPassword());
  HttpConnectionProvider provider = request.connectionProvider();
  if (null == provider) {
   provider = new SocketHttpConnectionProvider();
  }
  provider.useProxy(httpProxy);
  request.withConnectionProvider(provider);
 }
 return request;
}

代码示例来源:origin: binarywang/WxJava

private HttpRequest buildHttpRequest(String url, String requestStr, boolean useKey) throws WxPayException {
 HttpRequest request = HttpRequest
  .post(url)
  .timeout(this.getConfig().getHttpTimeout())
  .connectionTimeout(this.getConfig().getHttpConnectionTimeout())
  .bodyText(requestStr);
 if (useKey) {
  SSLContext sslContext = this.getConfig().getSslContext();
  if (null == sslContext) {
   sslContext = this.getConfig().initSSLContext();
  }
  final SSLSocketHttpConnectionProvider provider = new SSLSocketHttpConnectionProvider(sslContext);
  request.withConnectionProvider(provider);
 }
 if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost()) && this.getConfig().getHttpProxyPort() > 0) {
  ProxyInfo httpProxy = new ProxyInfo(ProxyType.HTTP, this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort(),
   this.getConfig().getHttpProxyUsername(), this.getConfig().getHttpProxyPassword());
  HttpConnectionProvider provider = request.connectionProvider();
  if (null == provider) {
   provider = new SocketHttpConnectionProvider();
  }
  provider.useProxy(httpProxy);
  request.withConnectionProvider(provider);
 }
 return request;
}

代码示例来源:origin: com.github.binarywang/weixin-java-mp

@Override
 public WxMpMaterialVideoInfoResult execute(String uri, String materialId) throws WxErrorException, IOException {
  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  request.query("media_id", materialId);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  } else {
   return WxMpMaterialVideoInfoResult.fromJson(responseContent);
  }
 }
}

代码示例来源:origin: com.github.binarywang/weixin-java-common

@Override
 public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException {
  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());
  request.form("media", file);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);

  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  }
  return WxMediaUploadResult.fromJson(responseContent);
 }
}

代码示例来源:origin: binarywang/WxJava

@Override
 public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException {
  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());
  request.form("media", file);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);

  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  }
  return WxMediaUploadResult.fromJson(responseContent);
 }
}

代码示例来源:origin: binarywang/WxJava

@Override
 public WxMpMaterialVideoInfoResult execute(String uri, String materialId) throws WxErrorException, IOException {
  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  request.query("media_id", materialId);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  } else {
   return WxMpMaterialVideoInfoResult.fromJson(responseContent);
  }
 }
}

代码示例来源:origin: binarywang/WxJava

@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
 if (this.configStorage.isAccessTokenExpired() || forceRefresh) {
  synchronized (this.globalAccessTokenRefreshLock) {
   if (this.configStorage.isAccessTokenExpired()) {
    String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
     + "&corpid=" + this.configStorage.getCorpId()
     + "&corpsecret=" + this.configStorage.getCorpSecret();
    HttpRequest request = HttpRequest.get(url);
    if (this.httpProxy != null) {
     httpClient.useProxy(this.httpProxy);
    }
    request.withConnectionProvider(httpClient);
    HttpResponse response = request.send();
    String resultContent = response.bodyText();
    WxError error = WxError.fromJson(resultContent, WxType.CP);
    if (error.getErrorCode() != 0) {
     throw new WxErrorException(error);
    }
    WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
    this.configStorage.updateAccessToken(
     accessToken.getAccessToken(), accessToken.getExpiresIn());
   }
  }
 }
 return this.configStorage.getAccessToken();
}

代码示例来源:origin: com.github.binarywang/weixin-java-mp

@Override
 public WxMediaImgUploadResult execute(String uri, File data) throws WxErrorException, IOException {
  if (data == null) {
   throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("文件对象为空").build());
  }

  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  request.form("media", data);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  }

  return WxMediaImgUploadResult.fromJson(responseContent);
 }
}

代码示例来源:origin: binarywang/WxJava

@Override
 public WxMediaImgUploadResult execute(String uri, File data) throws WxErrorException, IOException {
  if (data == null) {
   throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("文件对象为空").build());
  }

  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
   requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  request.form("media", data);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
   throw new WxErrorException(error);
  }

  return WxMediaImgUploadResult.fromJson(responseContent);
 }
}

相关文章