public HttpClient createHttpClient( String user, String password, String proxyHost, int proxyPort, AuthScope authScope ) { HttpHost httpHost = new HttpHost( proxyHost, proxyPort ); RequestConfig requestConfig = RequestConfig.custom() .setProxy( httpHost ) .build(); CredentialsProvider provider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials( user, password ); provider.setCredentials( authScope, credentials ); return HttpClientBuilder .create() .setDefaultCredentialsProvider( provider ) .setDefaultRequestConfig( requestConfig ) .setConnectionManager( manager ) .build(); }
public CloseableHttpClient build() { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setConnectionManager( manager ); RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); if ( socketTimeout > 0 ) { requestConfigBuilder.setSocketTimeout( socketTimeout ); } if ( connectionTimeout > 0 ) { requestConfigBuilder.setConnectTimeout( socketTimeout ); } if ( proxy != null ) { requestConfigBuilder.setProxy( proxy ); } httpClientBuilder.setDefaultRequestConfig( requestConfigBuilder.build() ); if ( provider != null ) { httpClientBuilder.setDefaultCredentialsProvider( provider ); } if ( redirectStrategy != null ) { httpClientBuilder.setRedirectStrategy( redirectStrategy ); } return httpClientBuilder.build(); } }
Integer proxyPort = Integer.parseInt(proxyPortStr); requestConfigBuilder.setProxy(new HttpHost(proxyHost, proxyPort)); logger.log(Level.INFO, "Using proxy: " + proxyHost + ":" + proxyPort);
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpPost.setConfig(config);
HttpGet httpGet = new HttpGet(url); if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpGet.setConfig(config);
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpGet.setConfig(config);
public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, ClientProtocolException, IOException { HttpPost httpPost = new HttpPost(uri); if (httpProxy != null) { RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build(); httpPost.setConfig(response);
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpPost.setConfig(config);
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpGet.setConfig(config);
private void addProxy( HttpClientContext context ) { String proxyHost; String proxyPort; String nonProxyHosts; String hostName; lock.readLock().lock(); try { proxyHost = environmentSubstitute( this.proxyHostname ); proxyPort = environmentSubstitute( this.proxyPort ); nonProxyHosts = environmentSubstitute( this.nonProxyHosts ); hostName = environmentSubstitute( this.hostname ); } finally { lock.readLock().unlock(); } if ( Utils.isEmpty( proxyHost ) || Utils.isEmpty( proxyPort ) ) { return; } // skip applying proxy if non-proxy host matches if ( !Utils.isEmpty( nonProxyHosts ) && hostName.matches( nonProxyHosts ) ) { return; } HttpHost httpHost = new HttpHost( proxyHost, Integer.valueOf( proxyPort ) ); RequestConfig requestConfig = RequestConfig.custom() .setProxy( httpHost ) .build(); context.setRequestConfig( requestConfig ); }
public InputStream execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException { HttpPost httpPost = new HttpPost(uri); if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpPost.setConfig(config); } Map<String, String> params = new HashMap<>(); params.put("media_id", materialId); httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); CloseableHttpResponse response = httpclient.execute(httpPost); // 下载媒体文件出错 InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); byte[] responseContent = IOUtils.toByteArray(inputStream); String responseContentString = new String(responseContent, "UTF-8"); if (responseContentString.length() < 100) { try { WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); if (wxError.getErrorCode() != 0) { throw new WxErrorException(wxError); } } catch (com.google.gson.JsonSyntaxException ex) { return new ByteArrayInputStream(responseContent); } } return new ByteArrayInputStream(responseContent); }
@Override public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, ClientProtocolException, IOException { HttpPost httpPost = new HttpPost(uri); if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpPost.setConfig(config); } if (file != null) { HttpEntity entity = MultipartEntityBuilder .create() .addBinaryBody("media", file) .setMode(HttpMultipartMode.RFC6532) .build(); httpPost.setEntity(entity); httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); } try (CloseableHttpResponse response = httpclient.execute(httpPost)) { String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } return WxMediaUploadResult.fromJson(responseContent); } }
@Override public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException { if (queryParam != null) { if (uri.indexOf('?') == -1) { uri += '?'; } uri += uri.endsWith("?") ? queryParam : '&' + queryParam; } HttpGet httpGet = new HttpGet(uri); if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpGet.setConfig(config); } try (CloseableHttpResponse response = httpclient.execute(httpGet)) { String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } return responseContent; } }
@Override public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String postEntity) throws WxErrorException, ClientProtocolException, IOException { HttpPost httpPost = new HttpPost(uri); if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpPost.setConfig(config); } if (postEntity != null) { StringEntity entity = new StringEntity(postEntity, Consts.UTF_8); httpPost.setEntity(entity); } try (CloseableHttpResponse response = httpclient.execute(httpPost)) { String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } return responseContent; } }
public Boolean execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException { HttpPost httpPost = new HttpPost(uri); if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpPost.setConfig(config); } Map<String, String> params = new HashMap<>(); params.put("media_id", materialId); httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); CloseableHttpResponse response = httpclient.execute(httpPost); String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } else { return true; } }
public WxMpMaterialNews execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException { HttpPost httpPost = new HttpPost(uri); if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpPost.setConfig(config); } Map<String, String> params = new HashMap<>(); params.put("media_id", materialId); httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); CloseableHttpResponse response = httpclient.execute(httpPost); String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } else { return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class); } }
public WxMpMaterialVideoInfoResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException { HttpPost httpPost = new HttpPost(uri); if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpPost.setConfig(config); } Map<String, String> params = new HashMap<>(); params.put("media_id", materialId); httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); CloseableHttpResponse response = httpclient.execute(httpPost); String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } else { return WxMpMaterialVideoInfoResult.fromJson(responseContent); } }
private void setupRequestConfig() { final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom() .setConnectionRequestTimeout(connectTimeoutMillis) .setConnectTimeout(connectTimeoutMillis) .setSocketTimeout(readTimeoutMillis); if (localAddress != null) { requestConfigBuilder.setLocalAddress(localAddress); } if (proxy != null) { requestConfigBuilder.setProxy(proxy.getHttpHost()); } requestConfig = requestConfigBuilder.build(); }
private HttpUriRequest convertHttpUriRequest(Request request, Site site, Proxy proxy) { RequestBuilder requestBuilder = selectRequestMethod(request).setUri(UrlUtils.fixIllegalCharacterInUrl(request.getUrl())); if (site.getHeaders() != null) { for (Map.Entry<String, String> headerEntry : site.getHeaders().entrySet()) { requestBuilder.addHeader(headerEntry.getKey(), headerEntry.getValue()); } } RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); if (site != null) { requestConfigBuilder.setConnectionRequestTimeout(site.getTimeOut()) .setSocketTimeout(site.getTimeOut()) .setConnectTimeout(site.getTimeOut()) .setCookieSpec(CookieSpecs.STANDARD); } if (proxy != null) { requestConfigBuilder.setProxy(new HttpHost(proxy.getHost(), proxy.getPort())); } requestBuilder.setConfig(requestConfigBuilder.build()); HttpUriRequest httpUriRequest = requestBuilder.build(); if (request.getHeaders() != null && !request.getHeaders().isEmpty()) { for (Map.Entry<String, String> header : request.getHeaders().entrySet()) { httpUriRequest.addHeader(header.getKey(), header.getValue()); } } return httpUriRequest; }
private CloseableHttpResponse executeViaProxy(Proxy proxy, String requestUrl) throws IOException { final CloseableHttpClient proxyClient; if (proxy.useAuthentication()){ proxyClient = buildHttpClient(makeCredentialsProvider(proxy.host,proxy.port,proxy.username,proxy.password)); } else { proxyClient = buildHttpClient(null); } final HttpHost proxyHttpHost = new HttpHost(proxy.host, proxy.port); final RequestConfig requestConfig = RequestConfig.custom().setProxy(proxyHttpHost).build(); final HttpGet request = new HttpGet(requestUrl); request.setConfig(requestConfig); return proxyClient.execute(request); }