本文整理了Java中com.google.appengine.api.urlfetch.HTTPResponse
类的一些代码示例,展示了HTTPResponse
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HTTPResponse
类的具体详情如下:
包路径:com.google.appengine.api.urlfetch.HTTPResponse
类名称:HTTPResponse
暂无
代码示例来源:origin: googlemaps/google-maps-services-java
byte[] bytes = response.getContent();
R resp;
for (HTTPHeader header : response.getHeaders()) {
if (header.getName().equalsIgnoreCase("Content-Type")) {
contentType = header.getValue();
&& contentType.startsWith("image")
&& responseClass == ImageResult.Response.class
&& response.getResponseCode() == 200) {
ImageResult result = new ImageResult(contentType, bytes);
return (T) result;
} catch (JsonSyntaxException e) {
if (response.getResponseCode() > 399) {
String.format(
"Server Error: %d %s",
response.getResponseCode(),
new String(response.getContent(), Charset.defaultCharset())));
代码示例来源:origin: GoogleCloudPlatform/appengine-gcs-client
@Override
protected GcsFileMetadata wrap(HTTPResponse resp) throws IOException {
long totalLength;
switch (resp.getResponseCode()) {
case 200:
totalLength = getLengthFromHeader(resp, X_GOOG_CONTENT_LENGTH);
break;
case 206:
totalLength = getLengthFromContentRange(resp);
break;
case 404:
throw new FileNotFoundException("Could not find: " + filename);
case 416:
throw new BadRangeException("Requested Range not satisfiable; perhaps read past EOF? "
+ URLFetchUtils.describeRequestAndResponse(info, resp));
default:
throw HttpErrorHandler.error(info, resp);
}
byte[] content = resp.getContent();
Preconditions.checkState(content.length <= want, "%s: got %s > wanted %s", this,
content.length, want);
dst.put(content);
return getMetadataFromResponse(filename, resp, totalLength);
}
代码示例来源:origin: googlemaps/google-maps-services-java
private boolean shouldRetry(HTTPResponse response) {
return RETRY_ERROR_CODES.contains(response.getResponseCode())
&& cumulativeSleepTime < errorTimeOut
&& (maxRetries == null || retryCounter < maxRetries);
}
代码示例来源:origin: com.squareup.retrofit/retrofit
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);
}
}
代码示例来源:origin: com.google.http-client/google-http-client-extensions
@Override
public InputStream getContent() {
byte[] content = fetchResponse.getContent();
return content == null ? null : new ByteArrayInputStream(content);
}
代码示例来源:origin: GoogleCloudPlatform/appengine-gcs-client
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');
}
代码示例来源:origin: org.apache.camel/camel-gae
protected void readResponseHeaders(GHttpEndpoint endpoint, Exchange exchange, HTTPResponse response) {
HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
Message in = exchange.getIn();
Message out = exchange.getOut();
out.setHeaders(in.getHeaders());
out.setHeader(Exchange.HTTP_RESPONSE_CODE, response.getResponseCode());
String contentType = getResponseHeader("Content-Type", response);
if (contentType != null) {
out.setHeader(Exchange.CONTENT_TYPE, contentType);
}
for (HTTPHeader header : response.getHeaders()) {
String name = header.getName();
String value = header.getValue();
if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
out.setHeader(name, value);
}
}
}
代码示例来源:origin: org.apache.camel/camel-gae
protected String getResponseHeader(String name, HTTPResponse response) {
for (HTTPHeader header : response.getHeaders()) {
if (header.getName().equalsIgnoreCase(name)) {
return header.getValue();
}
}
return null;
}
代码示例来源:origin: GoogleCloudPlatform/appengine-tck
@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);
}
代码示例来源:origin: GoogleCloudPlatform/appengine-gcs-client
/** Gets all headers with the name {@code headerName}, case-insensitive. */
private static Iterable<HTTPHeader> getHeaders(HTTPResponse resp, String headerName) {
final String lowercaseHeaderName = headerName.toLowerCase();
return Iterables.filter(resp.getHeadersUncombined(), new Predicate<HTTPHeader>() {
@Override public boolean apply(HTTPHeader header) {
return header.getName().toLowerCase().equals(lowercaseHeaderName);
}
});
}
代码示例来源:origin: com.threewks.thundr/thundr-gae
@Override
public URI getUri() {
try {
// final url is only non-null when we follow redirects
URL finalUrl = response().getFinalUrl();
return finalUrl == null ? url.toURI() : finalUrl.toURI();
} catch (URISyntaxException e) {
throw new HttpResponseException(e, "Uri cannot be parsed: %s", e.getMessage());
}
}
代码示例来源:origin: com.force.api/force-wsc
byte[] bytes = resp.getContent();
this.successful = resp.getResponseCode() < 400;
String encoding = getHeader(resp, "Content-Encoding");
代码示例来源:origin: com.google.http-client/google-http-client-extensions
@Override
public int getStatusCode() {
return fetchResponse.getResponseCode();
}
代码示例来源:origin: com.google.http-client/google-http-client-appengine
@Override
public InputStream getContent() {
byte[] content = fetchResponse.getContent();
return content == null ? null : new ByteArrayInputStream(content);
}
代码示例来源:origin: com.dropbox.core/dropbox-core-sdk
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);
}
代码示例来源:origin: org.codehaus.groovy.modules.http-builder/http-builder
public HttpResponse receiveResponseHeader()
throws HttpException, IOException {
if (this.response == null) {
flush();
}
HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1),
this.response.getResponseCode(),
null);
// System.err.println("RECV: " + response.getStatusLine());
for (HTTPHeader h : this.response.getHeaders()) {
// System.err.println("RECV: " + h.getName() + ": " + h.getValue());
response.addHeader(h.getName(), h.getValue());
}
return response;
}
代码示例来源:origin: stackoverflow.com
HTTPResponse response = URLFetchServiceFactory.getURLFetchService()
.fetch(new URL("url_to_fetch"));
List<HTTPHeader> headers = response.getHeaders();
for (HTTPHeader h : headers) {
if (h.getName().equals("Content-Type")) {
/*
* could be text/html; charset=iso-8859-1.
*/
if (h.getValue().startsWith("text/html")) {
/* TODO do sth. */
}
}
}
代码示例来源:origin: com.force.api/force-wsc
/** Gets the values of all headers with the name {@code headerName}. */
List<String> getHeaders(HTTPResponse resp, String headerName) {
List<String> b = new ArrayList<String>();
for (HTTPHeader h : resp.getHeadersUncombined()) {
if (headerName.equalsIgnoreCase(h.getName())) {
b.add(h.getValue());
}
}
return b;
}
代码示例来源:origin: GoogleCloudPlatform/appengine-tck
public void handle(HTTPResponse response) throws Exception {
URL finalURL = response.getFinalUrl();
Assert.assertEquals(getUrl(""), finalURL);
}
});
代码示例来源:origin: SSilence/scotty
private byte[] createResponse(HTTPResponse response) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int code = response.getResponseCode();
byte[] content = response.getContent();
List<HTTPHeader> heads = response.getHeaders();
try {
bos.write("HTTP/1.1 ".getBytes());
bos.write(String.valueOf(code).getBytes());
bos.write(" ".getBytes());
bos.write(getHttpReply(code).getBytes());
bos.write("\r\n".getBytes());
for (HTTPHeader responseHeader : heads) {
bos.write(responseHeader.getName().getBytes());
bos.write(":".getBytes());
bos.write(responseHeader.getValue().getBytes());
bos.write("\r\n".getBytes());
}
bos.write("\r\n".getBytes());
if (content != null) {
bos.write(content);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bos.toByteArray();
}
内容来源于网络,如有侵权,请联系作者删除!