List of usage examples for org.apache.http.client ClientProtocolException ClientProtocolException
public ClientProtocolException(final Throwable cause)
From source file:org.apache.http.impl.client.CloseableHttpClient.java
private static HttpHost determineTarget(final HttpUriRequest request) throws ClientProtocolException { // A null target may be acceptable if there is a default target. // Otherwise, the null target is detected in the director. HttpHost target = null;//from w w w . j a v a2 s. c om final URI requestURI = request.getURI(); if (requestURI.isAbsolute()) { target = URIUtils.extractHost(requestURI); if (target == null) { throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI); } } return target; }
From source file:org.apache.http.impl.client.InternalHttpClient.java
@Override protected CloseableHttpResponse doExecute(final HttpHost target, final HttpRequest request, final HttpContext context) throws IOException, ClientProtocolException { Args.notNull(request, "HTTP request"); HttpExecutionAware execAware = null; if (request instanceof HttpExecutionAware) { execAware = (HttpExecutionAware) request; }/*from w w w .j ava 2 s . c o m*/ try { final HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request); final HttpClientContext localcontext = HttpClientContext .adapt(context != null ? context : new BasicHttpContext()); RequestConfig config = null; if (request instanceof Configurable) { config = ((Configurable) request).getConfig(); } if (config == null) { final HttpParams params = request.getParams(); if (params instanceof HttpParamsNames) { if (!((HttpParamsNames) params).getNames().isEmpty()) { config = HttpClientParamConfig.getRequestConfig(params); } } else { config = HttpClientParamConfig.getRequestConfig(params); } } if (config != null) { localcontext.setRequestConfig(config); } setupContext(localcontext); final HttpRoute route = determineRoute(target, wrapper, localcontext); return this.execChain.execute(route, wrapper, localcontext, execAware); } catch (final HttpException httpException) { throw new ClientProtocolException(httpException); } }
From source file:org.apache.http.impl.nio.client.AbstractHttpAsyncClient.java
private HttpHost determineTarget(final HttpUriRequest request) throws ClientProtocolException { // A null target may be acceptable if there is a default target. // Otherwise, the null target is detected in the director. HttpHost target = null;/*from w w w . j ava 2s . c o m*/ final URI requestURI = request.getURI(); if (requestURI.isAbsolute()) { target = URIUtils.extractHost(requestURI); if (target == null) { throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI); } } return target; }
From source file:org.apache.stratos.kubernetes.client.rest.KubernetesResponseHandler.java
@Override public HttpResponse handleResponse(org.apache.http.HttpResponse response) throws ClientProtocolException, IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (entity == null) { throw new ClientProtocolException("Response contains no content"); }// w w w. j av a 2 s.co m BufferedReader reader = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; String result = ""; while ((output = reader.readLine()) != null) { result += output; } HttpResponse httpResponse = new HttpResponse(); httpResponse.setStatusCode(statusLine.getStatusCode()); httpResponse.setContent(result); if (StringUtils.isNotBlank(result) && (isJson(result))) { httpResponse.setKubernetesResponse(parseKubernetesResponse(result)); } httpResponse.setReason(statusLine.getReasonPhrase()); if (log.isDebugEnabled()) { log.debug("Extracted Kubernetes Response: " + httpResponse.toString()); } return httpResponse; }
From source file:xin.nic.sdk.registrar.util.HttpUtil.java
public static String doPost(String requestUrl, Map<String, String> paramsMap, Map<String, InputStream> files) throws Exception { RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(500) .setSocketTimeout(20000).setConnectTimeout(20000).build(); // ?//from w w w . java2 s . com MultipartEntityBuilder mbuilder = MultipartEntityBuilder.create() .setMode(HttpMultipartMode.BROWSER_COMPATIBLE).setCharset(Charset.forName(charset)); if (paramsMap != null) { Set<Entry<String, String>> paramsSet = paramsMap.entrySet(); for (Entry<String, String> entry : paramsSet) { mbuilder.addTextBody(entry.getKey(), entry.getValue(), ContentType.create("text/plain", charset)); } } if (files != null) { Set<Entry<String, InputStream>> filesSet = files.entrySet(); for (Entry<String, InputStream> entry : filesSet) { ByteArrayOutputStream os = new ByteArrayOutputStream(); InputStream is = entry.getValue(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) > 0) { os.write(buffer, 0, len); } os.close(); is.close(); mbuilder.addBinaryBody("attachment", os.toByteArray(), ContentType.APPLICATION_OCTET_STREAM, entry.getKey()); } } HttpPost httpPost = new HttpPost(requestUrl); httpPost.setConfig(requestConfig); HttpEntity httpEntity = mbuilder.build(); httpPost.setEntity(httpEntity); ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; return httpClient.execute(httpPost, responseHandler); }