Refine search
@Override public <T, R extends ApiResponse<T>> PendingResult<T> handlePost( String hostName, String url, String payload, String userAgent, Class<R> clazz, FieldNamingPolicy fieldNamingPolicy, long errorTimeout, Integer maxRetries, ExceptionsAllowedToRetry exceptionsAllowedToRetry) { FetchOptions fetchOptions = FetchOptions.Builder.withDeadline(10); HTTPRequest req = null; try { req = new HTTPRequest(new URL(hostName + url), HTTPMethod.POST, fetchOptions); req.setHeader(new HTTPHeader("Content-Type", "application/json; charset=utf-8")); req.setPayload(payload.getBytes(UTF_8)); } catch (MalformedURLException e) { LOG.error("Request: {}{}", hostName, url, e); throw (new RuntimeException(e)); } return new GaePendingResult<>( req, client, clazz, fieldNamingPolicy, errorTimeout, maxRetries, exceptionsAllowedToRetry); }
static HTTPRequest copyRequest(HTTPRequest in) { HTTPRequest out = new HTTPRequest(in.getURL(), in.getMethod(), in.getFetchOptions()); for (HTTPHeader h : in.getHeaders()) { out.addHeader(h); } out.setPayload(in.getPayload()); return out; } }
httpRequest.addHeader(new HTTPHeader("Content-Type","application/json")); httpRequest.addHeader(new HTTPHeader("Authorization", "key=" + API_KEY)); httpRequest.setPayload(jsonString.getBytes("UTF-8")); LOG.info("Sending POST request to: " + GCM_URL); response = URLFetchServiceFactory.getURLFetchService().fetch(httpRequest); List<HTTPHeader> hdrs = response.getHeaders(); for(HTTPHeader header : hdrs) { LOG.info("Header: " + header.getName()); LOG.info("Value: " + header.getValue());
@Test public void testHeaders() throws Exception { URLFetchService service = URLFetchServiceFactory.getURLFetchService(); URL url = getFetchUrl(); HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST); req.setHeader(new HTTPHeader("Content-Type", "application/octet-stream")); req.setPayload("Headers!".getBytes(UTF_8)); HTTPResponse response = service.fetch(req); boolean found = false; List<HTTPHeader> headers = response.getHeadersUncombined(); for (HTTPHeader h : headers) { if (h.getName().equals("ABC")) { Assert.assertEquals("123", h.getValue()); found = true; break; } } Assert.assertTrue("Cannot find matching header <ABC : 123>: " + headers, found); found = false; headers = response.getHeaders(); for (HTTPHeader h : headers) { if (h.getName().equals("XYZ")) { Assert.assertEquals("1, 2, 3", h.getValue()); found = true; break; } } Assert.assertTrue("Cannot find matching header <XYZ : 1,2,3>: " + headers, found); }
URL fetchurl = new URL(url); String nameAndPassword = credentials.get("name")+":"+credentials.get("password"); String authorizationString = "Basic " + Base64.encode(nameAndPassword.getBytes()); HTTPRequest request = new HTTPRequest(fetchurl); request.addHeader(new HTTPHeader("Authorization", authorizationString)); HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request); System.out.println(new String(response.getContent()));
@Test void testConvertRequestNoContent() throws IOException { HttpRequest request = HttpRequest.builder().method(HttpMethod.GET).endpoint(endPoint).build(); HTTPRequest gaeRequest = req.apply(request); assert gaeRequest.getPayload() == null; assertEquals(gaeRequest.getHeaders().size(), 1);// user agent assertEquals(gaeRequest.getHeaders().get(0).getName(), HttpHeaders.USER_AGENT); assertEquals(gaeRequest.getHeaders().get(0).getValue(), "jclouds/1.0 urlfetch/1.4.3"); }
@Override public void addHeader(String name, String value) { request.addHeader(new HTTPHeader(name, value)); }
String json ="{}"; URL url = new URL("https://android.googleapis.com/gcm/send"); HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST); request.addHeader(new HTTPHeader("Content-Type","application/json")); request.addHeader(new HTTPHeader("Authorization", "key=<>")); request.setPayload(json.getBytes("UTF-8")); HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST, fetchOptions); request.addHeader(new HTTPHeader("User-Agent", VersionInfo.info())); request.addHeader(new HTTPHeader(ent.getKey(), ent.getValue())); request.addHeader(new HTTPHeader("Content-Encoding", "gzip")); request.addHeader(new HTTPHeader("Accept-Encoding", "gzip")); + config.getProxyPassword(); String auth = "Basic " + new String(Base64.encode(token.getBytes())); request.addHeader(new HTTPHeader("Proxy-Authorization", auth)); request.addHeader(new HTTPHeader("Https-Proxy-Authorization", auth)); request.addHeader(new HTTPHeader(entry.getKey(), entry.getValue())); config.getTraceStream().println("WSC: Request configured to have headers " + request.getHeaders());
@Override public <T, R extends ApiResponse<T>> PendingResult<T> handle( String hostName, String url, String userAgent, Class<R> clazz, FieldNamingPolicy fieldNamingPolicy, long errorTimeout, Integer maxRetries, ExceptionsAllowedToRetry exceptionsAllowedToRetry) { FetchOptions fetchOptions = FetchOptions.Builder.withDeadline(10); HTTPRequest req; try { req = new HTTPRequest(new URL(hostName + url), HTTPMethod.POST, fetchOptions); } catch (MalformedURLException e) { LOG.error("Request: {}{}", hostName, url, e); throw (new RuntimeException(e)); } return new GaePendingResult<>( req, client, clazz, fieldNamingPolicy, errorTimeout, maxRetries, exceptionsAllowedToRetry); }
private void setContentTypeIfNotPresent(HttpRequestImpl request, HTTPRequest fetchRequest, ContentType contentType) { if (!request.getHeaders().containsKey(Header.ContentType)) { fetchRequest.setHeader(new HTTPHeader(Header.ContentType, contentType.value())); } }
@Test void testConvertRequestSetsHeaders() throws IOException { HttpRequest request = HttpRequest.builder() .method(HttpMethod.GET) .endpoint(endPoint) .addHeader("foo", "bar").build(); HTTPRequest gaeRequest = req.apply(request); assertEquals(gaeRequest.getHeaders().get(0).getName(), "foo"); assertEquals(gaeRequest.getHeaders().get(0).getValue(), "bar"); }
static Response parseResponse(HTTPResponse response, HTTPRequest creatingRequest) { // Response URL will be null if it is the same as the request URL. URL responseUrl = response.getFinalUrl(); String urlString = (responseUrl != null ? responseUrl : creatingRequest.getURL()).toString(); int status = response.getResponseCode(); List<HTTPHeader> fetchHeaders = response.getHeaders(); List<Header> headers = new ArrayList<Header>(fetchHeaders.size()); String contentType = "application/octet-stream"; for (HTTPHeader fetchHeader : fetchHeaders) { String name = fetchHeader.getName(); String value = fetchHeader.getValue(); if ("Content-Type".equalsIgnoreCase(name)) { contentType = value; } headers.add(new Header(name, value)); } TypedByteArray body = null; byte[] fetchBody = response.getContent(); if (fetchBody != null) { body = new TypedByteArray(contentType, fetchBody); } return new Response(urlString, status, "", headers, body); } }
private void addBody(HttpRequestImpl request, HTTPRequest fetchRequest, boolean createFormPostIfBodyIsEmpty) { byte[] data = null; try { Object body = request.getBody(); if (body == null && createFormPostIfBodyIsEmpty) { body = createFormPostBody(request); } if (isMultipart(request)) { HttpEntity multipart = prepareMultipart(request); ByteArrayOutputStream baos = new ByteArrayOutputStream(4096); multipart.writeTo(baos); data = baos.toByteArray(); fetchRequest.setHeader(new HTTPHeader(Header.ContentType, multipart.getContentType().getValue())); } else if (body != null) { InputStream is = convertOutgoing(body); data = Streams.readBytes(is); } } catch (Exception e) { throw new HttpException(e, "Failed to generate request body: %s", e.getMessage()); } if (data != null) { fetchRequest.setPayload(data); } }
protected void writeRequestBody(GHttpEndpoint endpoint, Exchange exchange, HTTPRequest request) { request.setPayload(exchange.getIn().getBody(byte[].class)); }
@Override public Response finish() throws IOException { if (request == null) { throw new IllegalStateException("Uploader already closed."); } request.setPayload(body.toByteArray()); HTTPResponse response = service.fetch(request); Response requestorResponse = toRequestorResponse(response); close(); if (progressListener != null) { progressListener.onProgress(request.getPayload().length); } return requestorResponse; } }
HTTPRequestInfo(HTTPRequest req) { method = req.getMethod().toString(); byte[] payload = req.getPayload(); length = payload == null ? -1 : payload.length; url = req.getURL(); h1 = null; h2 = req.getHeaders(); }
URL url = new URL(request); HTTPRequest req = new HTTPRequest(url, HTTPMethod.GET); req.addHeader(new HTTPHeader("Authorization", "Bearer " + bearerToken)); HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(req); System.out.println(new String(response.getContent()));
@Override public void setTimeout(int connectTimeout, int readTimeout) { request.getFetchOptions().setDeadline( connectTimeout == 0 || readTimeout == 0 ? Double.MAX_VALUE : (connectTimeout + readTimeout) / 1000.0); }
protected HttpResponseImpl fetch(HTTPRequest request, HTTPMethod headGetDelete) { try { Future<HTTPResponse> fetchAsync = fetchService.fetchAsync(request); return new HttpResponseImpl(fetchAsync, this, request.getURL()); } catch (Exception e) { throw new HttpRequestException(e, "Failed to create a %s request: %s", headGetDelete, e.getMessage()); } }