本文整理了Java中com.google.appengine.api.urlfetch.HTTPResponse.getHeaders()
方法的一些代码示例,展示了HTTPResponse.getHeaders()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HTTPResponse.getHeaders()
方法的具体详情如下:
包路径:com.google.appengine.api.urlfetch.HTTPResponse
类名称:HTTPResponse
方法名:getHeaders
暂无
代码示例来源:origin: googlemaps/google-maps-services-java
for (HTTPHeader header : response.getHeaders()) {
if (header.getName().equalsIgnoreCase("Content-Type")) {
contentType = header.getValue();
代码示例来源: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: 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: 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.google.http-client/google-http-client-extensions
UrlFetchResponse(HTTPResponse fetchResponse) {
this.fetchResponse = fetchResponse;
for (HTTPHeader header : fetchResponse.getHeaders()) {
String name = header.getName();
String value = header.getValue();
// Note: URLFetch will merge any duplicate headers with the same key and join their values
// using ", " as separator. However, ", " is also common inside values, such as in Expires or
// Set-Cookie headers.
if (name != null && value != null) {
headerNames.add(name);
headerValues.add(value);
if ("content-type".equalsIgnoreCase(name)) {
contentType = value;
} else if ("content-encoding".equalsIgnoreCase(name)) {
contentEncoding = value;
} else if ("content-length".equalsIgnoreCase(name)) {
try {
contentLength = Long.parseLong(value);
} catch (NumberFormatException e) {
// ignore
}
}
}
}
}
代码示例来源: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();
}
代码示例来源: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: 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: jclouds/legacy-jclouds
@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);
}
代码示例来源:origin: apache/jclouds
@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);
}
代码示例来源:origin: jclouds/legacy-jclouds
@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");
}
代码示例来源:origin: apache/jclouds
@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");
}
代码示例来源:origin: com.google.maps/google-maps-services
for (HTTPHeader header : response.getHeaders()) {
if (header.getName().equalsIgnoreCase("Content-Type")) {
contentType = header.getValue();
代码示例来源:origin: jgritman/httpbuilder
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: 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: GoogleCloudPlatform/appengine-gcs-client
private GcsFileMetadata getMetadataFromResponse(
GcsFilename filename, HTTPResponse resp, long length) {
List<HTTPHeader> headers = resp.getHeaders();
GcsFileOptions.Builder optionsBuilder = new GcsFileOptions.Builder();
String etag = null;
代码示例来源:origin: jclouds/legacy-jclouds
@Override
public HttpResponse apply(HTTPResponse gaeResponse) {
Payload payload = gaeResponse.getContent() != null ? Payloads.newByteArrayPayload(gaeResponse.getContent())
: null;
Multimap<String, String> headers = LinkedHashMultimap.create();
String message = null;
for (HTTPHeader header : gaeResponse.getHeaders()) {
if (header.getName() == null)
message = header.getValue();
else
headers.put(header.getName(), header.getValue());
}
if (payload != null) {
contentMetadataCodec.fromHeaders(payload.getContentMetadata(), headers);
}
return HttpResponse.builder()
.statusCode(gaeResponse.getResponseCode())
.message(message)
.payload(payload)
.headers(filterOutContentHeaders(headers)).build();
}
}
代码示例来源:origin: apache/jclouds
@Override
public HttpResponse apply(HTTPResponse gaeResponse) {
Payload payload = gaeResponse.getContent() != null ? Payloads.newByteArrayPayload(gaeResponse.getContent())
: null;
Multimap<String, String> headers = LinkedHashMultimap.create();
String message = null;
for (HTTPHeader header : gaeResponse.getHeaders()) {
if (header.getName() == null)
message = header.getValue();
else
headers.put(header.getName(), header.getValue());
}
if (payload != null) {
contentMetadataCodec.fromHeaders(payload.getContentMetadata(), headers);
}
return HttpResponse.builder()
.statusCode(gaeResponse.getResponseCode())
.message(message)
.payload(payload)
.headers(filterOutContentHeaders(headers)).build();
}
}
内容来源于网络,如有侵权,请联系作者删除!