public CloseableHttpResponse execute(HttpRequestBase request) throws IOException { request.setURI(request.getURI().normalize()); return client.execute(request); }
public CloseableHttpResponse execute(HttpRequestBase request, HttpContext context) throws IOException { request.setURI(request.getURI().normalize()); return client.execute(request, context); }
/** * Get the HttpRequest class that represents this request type. * * @return a non-abstract class that implements {@link HttpRequest} */ static HttpRequestBase createHttpRequest(URI uri, String httpMethod, boolean hasBody) { String method = notNull(upperCase(trimToNull(httpMethod)), "Http method"); Class<? extends HttpRequestBase> type = HTTP_METHOD_TO_HTTP_REQUEST_TYPE.get(method); final HttpRequestBase httpRequest; // If we are sending HTTP method that does not allow body (like GET) then HTTP library prevents // us from including it, however we chose to allow deviations from standard if user wants so, // so it needs custom handling - hence the second condition below. // Otherwise we should use standard implementation found in the map if (type == null || (!(type.isInstance(HttpEntityEnclosingRequest.class)) && hasBody)) { httpRequest = new CustomHttpMethod(method, uri); } else { try { httpRequest = type.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } httpRequest.setURI(uri); } return httpRequest; } }
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; }
/** * Create a {@link RequestConfigDelegate} from the given arguments, execute the * config closure, then pass the delegate to {@link #doRequest(RequestConfigDelegate)}, * which actually executes the request. */ protected Object doRequest( URI uri, Method method, Object contentType, Closure configClosure ) throws ClientProtocolException, IOException { HttpRequestBase reqMethod; try { reqMethod = method.getRequestType().newInstance(); // this exception should reasonably never occur: } catch ( Exception e ) { throw new RuntimeException( e ); } reqMethod.setURI( uri ); RequestConfigDelegate delegate = new RequestConfigDelegate( reqMethod, contentType, this.defaultRequestHeaders, this.defaultResponseHandlers ); configClosure.setDelegate( delegate ); configClosure.setResolveStrategy( Closure.DELEGATE_FIRST ); configClosure.call( reqMethod ); return this.doRequest( delegate ); }
/** * 设置请求Url * * @return */ public HttpUtils setURI(URI uri){ request.setURI(uri); return this; }
/** * @throws FakeIOException thrown only during test simulation */ HttpRequestBase newApacheRequest( final HttpRequestFactory<HttpRequestBase> httpRequestFactory, final Request<?> request, final HttpClientSettings options) throws IOException { apacheRequest = httpRequestFactory.create(request, options); if (redirectedURI != null) apacheRequest.setURI(redirectedURI); return apacheRequest; }
/** * Execute request. * * @return executed result * @throws URISyntaxException * @throws IOException */ public Mech2Result execute() throws URISyntaxException, IOException { this.request.setURI(this.uriBuilder.build()); return this.mech2.request(request); }
@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); }
@Override public <T> T execute(final HttpRequestBase request, final ResponseHandler<T> responseHandler) 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, responseHandler); }
private static void buildURI(final HttpRequestBase httpRequestBase, final RequestBuilder request) { // build URI with query by URIBuilder content base on client request // implementation . try { httpRequestBase.setURI(request.getURIBuilder().build()); } catch (URISyntaxException e) { String msg = "request URIBuilder do build error !"; logger.error(msg, e); throw new HttpClientException(msg, e); } }
protected void modifyURI(QueryParameters queryParameters) { request.setURI(URI.create(queryParameters.createUrl(request.getURI().toString()))); }
protected void modifyURI(QueryParameters queryParameters) { request.setURI(URI.create(queryParameters.createUrl(request.getURI().toString()))); }
private void createConnection(HttpClient client, HttpRequestBase method, String uri, Map<String, ?> params) throws IOException, URISyntaxException { String remoteUrl; if (uri.startsWith("http:/") || uri.startsWith("https:/")) { remoteUrl = uri; } else { String apiUrl = uri.startsWith("/api/") ? apiProvider.getHostUrl() : apiProvider.getApiUrl(); remoteUrl = apiUrl + uri; } String finalUrl = afterFinalUriConstructed(method, remoteUrl, params); method.setURI(new URI(finalUrl)); logRequest(method, finalUrl, params); onConnectionCreated(client, method, params); }
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); }
/** 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) {} }
private HttpRequestBase createRequest(String identifier, String acceptHeader, UriProvider uriProvider, RequestProvider requestProvider, int attempts) { HttpRequestBase request = requestProvider.getRequest(); URI uriFromIdentifier = getUri(identifier, uriProvider, attempts); request.setURI(uriFromIdentifier); request.addHeader(HttpHeaders.ACCEPT, acceptHeader); return request; }
@Override protected HttpResponse doExecute() throws Exception { final HttpMethod method = getMethod(); final HttpRequestBase httpRequest = FACTORY.create(method); httpRequest.setURI(createRequestURI()); handleHeaders(httpRequest); handleCookies(httpRequest); handleBody(httpRequest); final long start = nanoTime(); final org.apache.http.HttpResponse httpResponse = client.execute(httpRequest); final long duration = nanoTime() - start; return ApacheHttpResponseFactory.of(httpResponse, duration); }
private void verifyErrorResponse(HttpRequestBase httpRequest, String path, HttpStatus expectedStatus, String expectedBody) throws Exception { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { httpRequest.setURI(getUri(path)); try (CloseableHttpResponse response = httpClient.execute(httpRequest)) { assertThat(response.getStatusLine().getStatusCode(), is(expectedStatus.getStatusCode())); assertThat(response.getStatusLine().getReasonPhrase(), is(expectedStatus.getReasonPhrase())); assertThat(response.getFirstHeader(CONTENT_TYPE).getValue(), is(TEXT.toRfcString())); assertThat(IOUtils.toString(response.getEntity().getContent()), is(expectedBody)); } } }
@Test public void testUpdateRequestShouldCallBrowsermobRequestsMethods() throws URISyntaxException { //GIVEN URI uri = new URI("MOCK"); given(browserMobHttpRequest.getMethod()).willReturn(requestBase); given(wilmaHttpRequest.getUri()).willReturn(uri); String mockID = "WILMA-LOG-MOCK-ID"; given(wilmaHttpRequest.getWilmaMessageId()).willReturn(mockID); //WHEN underTest.updateRequest(browserMobHttpRequest, wilmaHttpRequest); //THEN verify(requestBase).setURI(uri); }