Refine search
try { URL url = new URL(request.getUrl()); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), URLDecoder.decode(url.getPath(), "UTF-8"), "", url.getRef()); urlToRequest = uri.toURL().toString(); if (url.getQuery() != null && !url.getQuery().trim().equals("")) { if (!urlToRequest.substring(urlToRequest.length() - 1).equals("?")) { break; case POST: reqObj = new HttpPost(urlToRequest); break; case PUT: if (values != null) { for (String value : values) { reqObj.addHeader(entry.getKey(), value); HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType());
this.httpResponse = httpClient.execute(request, context); } catch (UnknownHostException e) { request.reset(); throw new IOException("client connection failed: unknown host " + request.getURI().getHost()); } catch (SocketTimeoutException e){ request.reset(); throw new IOException("client connection timeout for request: " + request.getURI()); } catch (SSLHandshakeException e){ request.reset(); throw new IOException("client connection handshake error for domain " + request.getURI().getHost() + ": " + e.getMessage()); } catch (Throwable e) { request.reset(); throw new IOException("server fail: " + e.getMessage()); if (this.httpResponse.getStatusLine().getStatusCode() == 200) { try { this.inputStream = new BufferedInputStream(httpEntity.getContent()); } catch (IOException e) { request.reset(); throw e; request.reset(); throw new IOException("client connection to " + request.getURI() + " fail: " + status + ": " + httpResponse.getStatusLine().getReasonPhrase()); request.reset(); throw new IOException("client connection to " + request.getURI() + " fail: no connection");
req = new HttpGet(urlstring); else req = new HttpPost(urlstring); req.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build()); req.setHeader("User-Agent", USER_AGENT); CloseableHttpResponse httpResponse = httpClient.execute(req); HttpEntity httpEntity = httpResponse.getEntity(); int httpStatusCode = httpResponse.getStatusLine().getStatusCode(); if (300 <= httpStatusCode && httpStatusCode <= 308) { String location = getLocationHeader(httpResponse);
public RequestLine getRequestLine() { String method = getMethod(); ProtocolVersion ver = getProtocolVersion(); URI uri = getURI(); String uritext = null; if (uri != null) { uritext = uri.toASCIIString(); } if (uritext == null || uritext.length() == 0) { uritext = "/"; } return new BasicRequestLine(method, uritext, ver); }
private String executeMethod(HttpRequestBase req, String path) { try { URI base = new URI(apiUrl); req.setURI(new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), path, null, null)); } catch (URISyntaxException e) { s_logger.error("invalid API URL " + apiUrl + " path " + path, e); return null; } try { String content = null; try { content = getHttpClient().execute(req, new BasicResponseHandler()); s_logger.info("ssp api call: " + req); } catch (HttpResponseException e) { s_logger.info("ssp api call failed: " + req, e); if (e.getStatusCode() == HttpStatus.SC_UNAUTHORIZED && login()) { req.reset(); content = getHttpClient().execute(req, new BasicResponseHandler()); s_logger.info("ssp api retry call: " + req); } } return content; } catch (ClientProtocolException e) { // includes HttpResponseException s_logger.error("ssp api call failed: " + req, e); } catch (IOException e) { s_logger.error("ssp api call failed: " + req, e); } return null; }
public static boolean processRequestSilently(HttpClient httpClient, HttpRequestBase request) { try { HttpResponse response = httpClient.execute(request); LOG.info("Event sent"); if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) { return false; } HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); return true; } catch (IOException e) { LOG.error("Sending event failed", e); return false; } finally { request.releaseConnection(); } } }
if(request instanceof HttpPostRequest) {//post HttpPostRequest post = (HttpPostRequest)request; reqObj = new HttpPost(post.getUrl()); reqObj.addHeader("User-Agent", UserAgent.getUserAgent(isMobile)); for(Map.Entry<String, String> entry : request.getHeaders().entrySet()) { reqObj.setHeader(entry.getKey(), entry.getValue()); reqObj.setConfig(builder.build()); BasicClientCookie cookie = new BasicClientCookie(entry.getKey(), entry.getValue()); cookie.setPath("/"); cookie.setDomain(reqObj.getURI().getHost()); cookieContext.getCookieStore().addCookie(cookie); int status = response.getStatusLine().getStatusCode(); HttpResponse resp = new HttpResponse(); resp.setStatus(status); if(status == 302 || status == 301) { String redirectUrl = response.getFirstHeader("Location").getValue(); resp.setContent(UrlUtils.relative2Absolute(request.getUrl(), redirectUrl)); } else if(status == 200) { HttpEntity responseEntity = response.getEntity(); ByteArrayInputStream raw = toByteInputStream(responseEntity.getContent()); resp.setRaw(raw); reqObj.releaseConnection();
private void logHttpError(String msg, HttpRequestBase req, HttpResponse resp) throws Exception { log.error(msg + "\nHTTP Status: " + resp.getStatusLine().getStatusCode() + "\nREST API url: " + req.getURI().toString() + "\nResponse:\n" + EntityUtils.toString(resp.getEntity())); }
private HttpResponse getOrDelete(HttpClient client, HttpRequestBase method, boolean authenticate, boolean validate) throws Exception { LOGGER.debug(method.getURI().toString()); if (!(method instanceof HttpGet || method instanceof HttpDelete || method instanceof HttpHead)) { throw new IllegalArgumentException("method must be one of GET, HEAD or DELETE."); } HttpResponse response = client.execute(method); if (response.getStatusLine().getStatusCode() == SC_MOVED_TEMPORARILY) { String redir = response.getFirstHeader(HttpHeaders.LOCATION).getValue(); if (!method.getURI().toString().equals(redir)) { method.setURI(getURI(redir)); response = getOrDelete(client, method, authenticate, validate); } } if (validate) { validateResponse(method.getURI(), response); } return response; }
private HttpRequestBase getNewRequest(String reqMethod, String reqPayload) throws URISyntaxException, UnsupportedEncodingException { HttpRequestBase request; if (reqMethod.equals(HttpConstants.REQ_METHOD_POST)) { HttpPost postRequest = new HttpPost(); postRequest.setEntity(new StringEntity(reqPayload, ContentType.create( DataFormats.JSON.getMediaType(), Constants.UTF_8))); request = postRequest; } else { throw new IllegalArgumentException(Errors.ARGS_HTTP_METHOD_UNSUPPORTED.getDescription()); } request.setURI(new URI(String.format("%s://%s:%s/", nodeConfig.getProperty(NodeProperties.RPC_PROTOCOL.getKey()), nodeConfig.getProperty(NodeProperties.RPC_HOST.getKey()), nodeConfig.getProperty(NodeProperties.RPC_PORT.getKey())))); String authScheme = nodeConfig.getProperty(NodeProperties.HTTP_AUTH_SCHEME.getKey()); request.addHeader(resolveAuthHeader(authScheme)); LOG.debug("<< getNewRequest(..): returning a new HTTP '{}' request with target endpoint " + "'{}' and headers '{}'", reqMethod, request.getURI(), request.getAllHeaders()); return request; }
private HttpClientOauthResponse sendInternal() throws Exception { String urlToGo = getCompleteUrl(); HttpClient client = httpClientProvider.getHttpClient(); // always POST when doing oauth dance on bitbucket // see DefaultAp10Api#getAccessTokenVerb or getRequestTokenVerb HttpRequestBase requestMethod = new HttpPost(); requestMethod.setURI(new URI(urlToGo)); setHeaders(requestMethod); setPayloadParams((HttpEntityEnclosingRequestBase) requestMethod); HttpResponse response = null; response = client.execute(requestMethod); return new HttpClientOauthResponse(response, requestMethod); }
this.httpResponse = httpClient.execute(this.request); } catch (UnknownHostException e) { this.request.releaseConnection(); throw new IOException("client connection failed: unknown host " + this.request.getURI().getHost()); } catch (SocketTimeoutException e){ this.request.releaseConnection(); throw new IOException("client connection timeout for request: " + this.request.getURI()); } catch (SSLHandshakeException e){ this.request.releaseConnection(); throw new IOException("client connection handshake error for domain " + this.request.getURI().getHost() + ": " + e.getMessage()); HttpEntity httpEntity = this.httpResponse.getEntity(); this.contentType = ContentType.get(httpEntity); if (httpEntity != null) { if (this.httpResponse.getStatusLine().getStatusCode() == 200) { try { this.inputStream = new BufferedInputStream(httpEntity.getContent()); } catch (IOException e) { this.request.releaseConnection(); throw e; this.request.releaseConnection(); throw new IOException("client connection to " + this.request.getURI() + " fail: " + status + ": " + httpResponse.getStatusLine().getReasonPhrase()); this.request.releaseConnection(); throw new IOException("client connection to " + this.request.getURI() + " fail: no connection");
@Override protected HttpResponse execute(final HttpRequestBase request) throws IOException { if(StringUtils.isNotBlank(request.getURI().getRawQuery())) { request.setURI(URI.create(String.format("%s%s?%s", uri, request.getURI().getRawPath(), request.getURI().getRawQuery()))); } else { request.setURI(URI.create(String.format("%s%s", uri, request.getURI().getRawPath()))); } return super.execute(request); }
public ClientServicesException(HttpResponse response, HttpRequestBase request) { this(null, createMessage(request, response)); this.response = response; this.request = request; this.setResponseStatusCode(response.getStatusLine().getStatusCode()); this.setReasonPhrase(response.getStatusLine().getReasonPhrase()); this.setRequestURI(request.getURI()); try { HttpEntity entity = response.getEntity(); this.responseBody = EntityUtils.toString(entity, "UTF-8"); } catch (IOException ioe) { } }
public Page getWebPage(HttpRequestBase request) throws IOException { CloseableHttpResponse response = null; response = HttpClientUtil.getResponse(request); Page page = new Page(); page.setStatusCode(response.getStatusLine().getStatusCode()); page.setHtml(EntityUtils.toString(response.getEntity())); page.setUrl(request.getURI().toString()); return page; } /**
private IOException asIOException(HttpRequestBase request, HttpResponse response) throws IOException { return new IOException(request.getMethod() + " failed, error code " + response.getStatusLine().getStatusCode() + " and response: " + EntityUtils.toString(response.getEntity())); }
/** Performs required actions with wrapped {@linkplain HttpRequestBase}. */ void doPreSend() { try { setCoreParameters(getCoreRequest(), parameters); getCoreRequest().setURI(new URI(String.format("http://localhost:%d%s", servletContainter.getLocalPort(), getUri()))); } catch (URISyntaxException ignore) {} }