byte[] bytes = response.getContent(); R resp; "Server Error: %d %s", response.getResponseCode(), new String(response.getContent(), Charset.defaultCharset())));
@Override public InputStream getContent() { byte[] content = fetchResponse.getContent(); return content == null ? null : new ByteArrayInputStream(content); }
@Override public InputStream getContent() { byte[] content = fetchResponse.getContent(); return content == null ? null : new ByteArrayInputStream(content); }
@Override public InputStream getBodyAsStream() { byte[] content = response().getContent(); content = content == null ? new byte[0] : content; return new ByteArrayInputStream(content); }
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 byte[] getBodyAsBytes() { return response().getContent(); }
//Classes to import import com.google.appengine.api.urlfetch.HTTPMethod; import com.google.appengine.api.urlfetch.HTTPRequest; import com.google.appengine.api.urlfetch.HTTPResponse; import com.google.appengine.api.urlfetch.URLFetchService; import com.google.appengine.api.urlfetch.URLFetchServiceFactory; //Shortening download URL URL url=new URL("http://goo.gl/api/shorten"); HTTPRequest req=new HTTPRequest(url,HTTPMethod.POST); req.setPayload(("url=www.google.com").getBytes()); URLFetchService service = URLFetchServiceFactory.getURLFetchService(); HTTPResponse response = service.fetch(req); byte[] content = response.getContent(); String urlshort=new String(content); //here is the JSON data from goo.gl
protected void readResponseBody(GHttpEndpoint endpoint, Exchange exchange, HTTPResponse response) throws Exception { byte[] content = response.getContent(); if (content != null) { InputStream stream = new ByteArrayInputStream(content); if (isGzip(getResponseHeader("Content-Encoding", response))) { stream = new GZIPInputStream(stream); } exchange.getOut().setBody(stream); } }
public void receiveResponseEntity(HttpResponse response) throws HttpException, IOException { if (this.response == null) { throw new IOException("receiveResponseEntity() called on closed connection"); } ByteArrayEntity bae = new ByteArrayEntity(this.response.getContent()); bae.setContentType(response.getFirstHeader("Content-Type")); response.setEntity(bae); response = null; }
public void receiveResponseEntity(HttpResponse response) throws HttpException, IOException { if (this.response == null) { throw new IOException("receiveResponseEntity() called on closed connection"); } ByteArrayEntity bae = new ByteArrayEntity(this.response.getContent()); bae.setContentType(response.getFirstHeader("Content-Type")); response.setEntity(bae); response = null; }
static void appendResponse(HTTPResponse resp, StringBuilder b) { byte[] content = resp.getContent(); b.append(resp.getResponseCode()).append(" with ").append(content == null ? 0 : content.length); b.append(" bytes of content"); for (HTTPHeader h : resp.getHeadersUncombined()) { b.append('\n').append(h.getName()).append(": ").append(h.getValue()); } b.append('\n').append(content == null ? "" : new String(content, UTF_8)).append('\n'); }
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()));
protected void printResponse(HTTPResponse response) throws Exception { System.out.println("response = " + new String(response.getContent())); }
private static Response toRequestorResponse(HTTPResponse response) { Map<String, List<String>> headers = new HashMap<String, List<String>>(); for (HTTPHeader header : response.getHeadersUncombined()) { List<String> existing = headers.get(header.getName()); if (existing == null) { existing = new ArrayList<String>(); headers.put(header.getName(), existing); } existing.add(header.getValue()); } return new Response(response.getResponseCode(), new ByteArrayInputStream(response.getContent()), headers); }
@Test void testConvertWithHeaders() throws IOException { HTTPResponse gaeResponse = createMock(HTTPResponse.class); expect(gaeResponse.getResponseCode()).andReturn(200); List<HTTPHeader> headers = Lists.newArrayList(); headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml")); expect(gaeResponse.getHeaders()).andReturn(headers); expect(gaeResponse.getContent()).andReturn(null).atLeastOnce(); replay(gaeResponse); HttpResponse response = req.apply(gaeResponse); assertEquals(response.getStatusCode(), 200); assertEquals(response.getPayload(), null); assertEquals(response.getHeaders().size(), 0); }
@Test void testConvertWithHeaders() throws IOException { HTTPResponse gaeResponse = createMock(HTTPResponse.class); expect(gaeResponse.getResponseCode()).andReturn(200); List<HTTPHeader> headers = Lists.newArrayList(); headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml")); expect(gaeResponse.getHeaders()).andReturn(headers); expect(gaeResponse.getContent()).andReturn(null).atLeastOnce(); replay(gaeResponse); HttpResponse response = req.apply(gaeResponse); assertEquals(response.getStatusCode(), 200); assertEquals(response.getPayload(), null); assertEquals(response.getHeaders().size(), 0); }
@Test void testConvertWithContent() throws IOException { HTTPResponse gaeResponse = createMock(HTTPResponse.class); expect(gaeResponse.getResponseCode()).andReturn(200); List<HTTPHeader> headers = Lists.newArrayList(); headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml")); expect(gaeResponse.getHeaders()).andReturn(headers); expect(gaeResponse.getContent()).andReturn("hello".getBytes()).atLeastOnce(); replay(gaeResponse); HttpResponse response = req.apply(gaeResponse); assertEquals(response.getStatusCode(), 200); assertEquals(Strings2.toString(response.getPayload()), "hello"); assertEquals(response.getHeaders().size(), 0); assertEquals(response.getPayload().getContentMetadata().getContentType(), "text/xml"); }
protected String fetchUrl(String url, int expectedResponse) throws IOException { URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService(); HTTPResponse httpResponse = urlFetchService.fetch(new URL(url)); assertEquals(url, expectedResponse, httpResponse.getResponseCode()); return new String(httpResponse.getContent()); }
@Test void testConvertWithContent() throws IOException { HTTPResponse gaeResponse = createMock(HTTPResponse.class); expect(gaeResponse.getResponseCode()).andReturn(200); List<HTTPHeader> headers = Lists.newArrayList(); headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml")); expect(gaeResponse.getHeaders()).andReturn(headers); expect(gaeResponse.getContent()).andReturn("hello".getBytes()).atLeastOnce(); replay(gaeResponse); HttpResponse response = req.apply(gaeResponse); assertEquals(response.getStatusCode(), 200); assertEquals(Strings2.toStringAndClose(response.getPayload().openStream()), "hello"); assertEquals(response.getHeaders().size(), 0); assertEquals(response.getPayload().getContentMetadata().getContentType(), "text/xml"); }
@Test public void testPayload() 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("Tralala".getBytes(UTF_8)); HTTPResponse response = service.fetch(req); String content = new String(response.getContent()); Assert.assertEquals("Hopsasa", content); }