本文整理匯總了Java中org.apache.http.HttpResponse.getHeaders方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpResponse.getHeaders方法的具體用法?Java HttpResponse.getHeaders怎麽用?Java HttpResponse.getHeaders使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.HttpResponse
的用法示例。
在下文中一共展示了HttpResponse.getHeaders方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: sendResponseMessage
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
@Override
public final void sendResponseMessage(HttpResponse response) throws IOException {
StatusLine status = response.getStatusLine();
Header[] contentTypeHeaders = response.getHeaders("Content-Type");
if (contentTypeHeaders.length != 1) {
//malformed/ambiguous HTTP Header, ABORT!
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(status.getStatusCode(), "None, or more than one, Content-Type Header found!"));
return;
}
Header contentTypeHeader = contentTypeHeaders[0];
boolean foundAllowedContentType = false;
for (String anAllowedContentType : getAllowedContentTypes()) {
try {
if (Pattern.matches(anAllowedContentType, contentTypeHeader.getValue())) {
foundAllowedContentType = true;
}
} catch (PatternSyntaxException e) {
}
}
if (!foundAllowedContentType) {
//Content-Type not in allowed list, ABORT!
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), null, new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"));
return;
}
super.sendResponseMessage(response);
}
示例2: headers
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
@Test public void headers() throws Exception {
server.enqueue(new MockResponse().addHeader("Foo", "Bar"));
server.enqueue(new MockResponse().addHeader("Foo", "Bar").addHeader("Foo", "Baz"));
HttpGet request1 = new HttpGet(server.url("/").url().toURI());
HttpResponse response1 = client.execute(request1);
Header[] headers1 = response1.getHeaders("Foo");
assertEquals(1, headers1.length);
assertEquals("Bar", headers1[0].getValue());
HttpGet request2 = new HttpGet(server.url("/").url().toURI());
HttpResponse response2 = client.execute(request2);
Header[] headers2 = response2.getHeaders("Foo");
assertEquals(2, headers2.length);
assertEquals("Bar", headers2[0].getValue());
assertEquals("Baz", headers2[1].getValue());
}
示例3: jsonGzipResponse
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
@Test public void jsonGzipResponse() throws Exception {
String text = "{\"Message\": { \"text\": \"Hello, World!\" } }";
server.enqueue(new MockResponse().setBody(gzip(text))
.setHeader("Content-Encoding", "gzip")
.setHeader("Content-Type", "application/json"));
HttpGet request1 = new HttpGet(server.url("/").url().toURI());
request1.setHeader("Accept-encoding", "gzip"); // Not transparent gzip.
HttpResponse response = client.execute(request1);
HttpEntity entity = response.getEntity();
Header[] encodingHeaders = response.getHeaders("Content-Encoding");
assertEquals(1, encodingHeaders.length);
assertEquals("gzip", encodingHeaders[0].getValue());
assertNotNull(entity.getContentEncoding());
assertEquals("gzip", entity.getContentEncoding().getValue());
Header[] typeHeaders = response.getHeaders("Content-Type");
assertEquals(1, typeHeaders.length);
assertEquals("application/json", typeHeaders[0].getValue());
assertNotNull(entity.getContentType());
assertEquals("application/json", entity.getContentType().getValue());
assertEquals(text, gunzip(entity));
}
示例4: sendResponseMessage
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
protected void sendResponseMessage(HttpResponse response) {
super.sendResponseMessage(response);
Header[] headers = response.getHeaders("Set-Cookie");
if (headers != null && headers.length > 0) {
CookieSyncManager.createInstance(this.val$context).sync();
CookieManager instance = CookieManager.getInstance();
instance.setAcceptCookie(true);
instance.removeSessionCookie();
String mm = "";
for (Header header : headers) {
String[] split = header.toString().split("Set-Cookie:");
EALogger.i("正式登錄", "split[1]===>" + split[1]);
instance.setCookie(Constants.THIRDLOGIN, split[1]);
int index = split[1].indexOf(";");
if (TextUtils.isEmpty(mm)) {
mm = split[1].substring(index + 1);
EALogger.i("正式登錄", "mm===>" + mm);
}
}
EALogger.i("正式登錄", "split[1222]===>COOKIE_DEVICE_ID=" + LemallPlatform.getInstance().uuid + ";" + mm);
instance.setCookie(Constants.THIRDLOGIN, "COOKIE_DEVICE_ID=" + LemallPlatform.getInstance().uuid + ";" + mm);
instance.setCookie(Constants.THIRDLOGIN, "COOKIE_APP_ID=" + LemallPlatform.getInstance().getmAppInfo().getId() + ";" + mm);
CookieSyncManager.getInstance().sync();
this.val$iLetvBrideg.reLoadWebUrl();
}
}
示例5: addCookie
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
private List<Cookie> addCookie(final HttpResponse httpResponse) {
String cookieValue = "";
List<Cookie> cookieList = new ArrayList<Cookie>();
for (Header header : httpResponse.getHeaders("Set-Cookie")) {
List<HttpCookie> httpCookies = HttpCookie.parse(header.getValue());
for (HttpCookie httpCookie : httpCookies) {
if (!cookieValue.isEmpty()) {
cookieValue += "; ";
}
cookieList.add(new Cookie(httpCookie.getName(), httpCookie.getValue(), httpCookie.getDomain(), httpCookie.getPath(), null, httpCookie.getSecure(), httpCookie.isHttpOnly()));
}
}
return cookieList;
}
示例6: jsonTransparentGzipResponse
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
@Test public void jsonTransparentGzipResponse() throws Exception {
String text = "{\"Message\": { \"text\": \"Hello, World!\" } }";
server.enqueue(new MockResponse().setBody(gzip(text))
.setHeader("Content-Encoding", "gzip")
.setHeader("Content-Type", "application/json"));
HttpGet request = new HttpGet(server.url("/").url().toURI());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
// Expecting transparent gzip response by not adding header "Accept-encoding: gzip"
Header[] encodingHeaders = response.getHeaders("Content-Encoding");
assertEquals(0, encodingHeaders.length);
assertNull(entity.getContentEncoding());
// Content length should be absent.
Header[] lengthHeaders = response.getHeaders("Content-Length");
assertEquals(0, lengthHeaders.length);
assertEquals(-1, entity.getContentLength());
Header[] typeHeaders = response.getHeaders("Content-Type");
assertEquals(1, typeHeaders.length);
assertEquals("application/json", typeHeaders[0].getValue());
assertNotNull(entity.getContentType());
assertEquals("application/json", entity.getContentType().getValue());
assertEquals(text, EntityUtils.toString(entity, UTF_8));
}
示例7: getResponseBodyInputStream
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
/**
* レスポンスボディのストリームを受け取る.
* @param res Responseオブジェクト
* @return ストリーム
* @throws IOException IO例外
*/
protected final InputStream getResponseBodyInputStream(final HttpResponse res) throws IOException {
// GZip 圧縮されていたら解凍する。
Header[] contentEncodingHeaders = res.getHeaders("Content-Encoding");
if (contentEncodingHeaders.length > 0 && "gzip".equalsIgnoreCase(contentEncodingHeaders[0].getValue())) {
return new GZIPInputStream(res.getEntity().getContent());
} else {
return res.getEntity().getContent();
}
}
示例8: contentType
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
@Test public void contentType() throws Exception {
server.enqueue(new MockResponse().setBody("<html><body><h1>Hello, World!</h1></body></html>")
.setHeader("Content-Type", "text/html"));
server.enqueue(new MockResponse().setBody("{\"Message\": { \"text\": \"Hello, World!\" } }")
.setHeader("Content-Type", "application/json"));
server.enqueue(new MockResponse().setBody("Hello, World!"));
HttpGet request1 = new HttpGet(server.url("/").url().toURI());
HttpResponse response1 = client.execute(request1);
Header[] headers1 = response1.getHeaders("Content-Type");
assertEquals(1, headers1.length);
assertEquals("text/html", headers1[0].getValue());
assertNotNull(response1.getEntity().getContentType());
assertEquals("text/html", response1.getEntity().getContentType().getValue());
HttpGet request2 = new HttpGet(server.url("/").url().toURI());
HttpResponse response2 = client.execute(request2);
Header[] headers2 = response2.getHeaders("Content-Type");
assertEquals(1, headers2.length);
assertEquals("application/json", headers2[0].getValue());
assertNotNull(response2.getEntity().getContentType());
assertEquals("application/json", response2.getEntity().getContentType().getValue());
HttpGet request3 = new HttpGet(server.url("/").url().toURI());
HttpResponse response3 = client.execute(request3);
Header[] headers3 = response3.getHeaders("Content-Type");
assertEquals(0, headers3.length);
assertNull(response3.getEntity().getContentType());
}
示例9: getCookie
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
/**
* @param httpResponse http響應
* @return
*/
public static String getCookie(HttpResponse httpResponse) {
StringBuffer cookie = new StringBuffer();
Header[] headers = httpResponse.getHeaders("Set-Cookie");
if (headers != null) {
for (Header header : headers) {
String Set_Cookie = header.getValue().split(";")[0];
cookie.append(Set_Cookie + ";");
}
}
return cookie.toString();
}
示例10: extractCookies
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
private void extractCookies(URI uri, HttpResponse response, Cookies cookies) throws Exception
{
Header[] headers = response.getHeaders("Set-Cookie");
for( Header header : headers )
{
ACookie cookie = new ACookie(uri, header.getValue());
cookies.recvd.add(cookie);
cookies.send.add(cookie);
}
}
示例11: getChallenges
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
public Map<String, Header> getChallenges(
final HttpResponse response,
final HttpContext context) throws MalformedChallengeException {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
Header[] headers = response.getHeaders(AUTH.WWW_AUTH);
return parseChallenges(headers);
}
示例12: process
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
@Override
public void process(HttpResponse response, final HttpContext context)
throws HttpException, IOException {
final HttpEntity entity = response.getEntity();
// Only Json protocol has this header, we only wrap CRC32ChecksumCalculatingInputStream in json protocol clients.
Header[] headers = response.getHeaders("x-amz-crc32");
if (entity == null || headers == null || headers.length == 0) {
return;
}
HttpEntity crc32ResponseEntity = new HttpEntityWrapper(entity) {
private final InputStream content = new CRC32ChecksumCalculatingInputStream(
wrappedEntity.getContent());
@Override
public InputStream getContent() throws IOException {
return content;
}
/**
* It's important to override writeTo. Some versions of Apache HTTP
* client use writeTo for {@link org.apache.http.entity.BufferedHttpEntity}
* and the default implementation just delegates to the wrapped entity
* which completely bypasses our CRC32 calculating input stream. The
* {@link org.apache.http.entity.BufferedHttpEntity} is used for the
* request timeout and client execution timeout features.
*
* @see <a href="https://github.com/aws/aws-sdk-java/issues/526">Issue #526</a>
*
* @param outstream OutputStream to write contents to
*/
@Override
public void writeTo(OutputStream outstream) throws IOException {
try {
IOUtils.copy(this.getContent(), outstream);
} finally {
this.getContent().close();
}
}
};
response.setEntity(crc32ResponseEntity);
context.setAttribute(CRC32ChecksumCalculatingInputStream.class.getName(),
crc32ResponseEntity.getContent());
}
示例13: getCookies
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
public Header[] getCookies(HttpResponse response) {
return response.getHeaders("Set-Cookie");
}
示例14: sendResponseMessage
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
@Override
public final void sendResponseMessage(HttpResponse response) throws IOException {
StatusLine status = response.getStatusLine();
Header[] contentTypeHeaders = response.getHeaders(AsyncHttpClient.HEADER_CONTENT_TYPE);
if (contentTypeHeaders.length != 1) {
//malformed/ambiguous HTTP Header, ABORT!
sendFailureMessage(
status.getStatusCode(),
response.getAllHeaders(),
null,
new HttpResponseException(
status.getStatusCode(),
"None, or more than one, Content-Type Header found!"
)
);
return;
}
Header contentTypeHeader = contentTypeHeaders[0];
boolean foundAllowedContentType = false;
for (String anAllowedContentType : getAllowedContentTypes()) {
try {
if (Pattern.matches(anAllowedContentType, contentTypeHeader.getValue())) {
foundAllowedContentType = true;
}
} catch (PatternSyntaxException e) {
Log.e("BinaryHttpResponseHandler", "Given pattern is not valid: " + anAllowedContentType, e);
}
}
if (!foundAllowedContentType) {
//Content-Type not in allowed list, ABORT!
sendFailureMessage(
status.getStatusCode(),
response.getAllHeaders(),
null,
new HttpResponseException(
status.getStatusCode(),
"Content-Type not allowed!"
)
);
return;
}
super.sendResponseMessage(response);
}
示例15: logHeaders
import org.apache.http.HttpResponse; //導入方法依賴的package包/類
public static void logHeaders(StringBuilder sb, int id, char prefix, HttpResponse response) {
for (String key : sortKeys(response.getAllHeaders())) {
Header[] headers = response.getHeaders(key);
logHeaderLine(sb, id, prefix, key, headers);
}
}