@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")); }
@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)); }
@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)); }
@Override public String getAccessToken(boolean forceRefresh) throws WxErrorException { Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); try { lock.lock(); if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) { String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret()); HttpRequest request = HttpRequest.get(url); if (this.getRequestHttpProxy() != null) { SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider(); provider.useProxy(getRequestHttpProxy()); request.withConnectionProvider(provider); } HttpResponse response = request.send(); String resultContent = response.bodyText(); WxError error = WxError.fromJson(resultContent, WxType.MP); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); } } finally { lock.unlock(); } return this.getWxMpConfigStorage().getAccessToken(); }
@Override public String getAccessToken(boolean forceRefresh) throws WxErrorException { Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); try { lock.lock(); if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) { String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret()); HttpRequest request = HttpRequest.get(url); if (this.getRequestHttpProxy() != null) { SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider(); provider.useProxy(getRequestHttpProxy()); request.withConnectionProvider(provider); } HttpResponse response = request.send(); String resultContent = response.bodyText(); WxError error = WxError.fromJson(resultContent, WxType.MP); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); } } finally { lock.unlock(); } return this.getWxMpConfigStorage().getAccessToken(); }
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; }
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; }