本文整理匯總了Java中org.apache.http.message.BasicHttpResponse.addHeader方法的典型用法代碼示例。如果您正苦於以下問題:Java BasicHttpResponse.addHeader方法的具體用法?Java BasicHttpResponse.addHeader怎麽用?Java BasicHttpResponse.addHeader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.message.BasicHttpResponse
的用法示例。
在下文中一共展示了BasicHttpResponse.addHeader方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: transformResponse
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
private static HttpResponse transformResponse(Response response) {
int code = response.code();
String message = response.message();
BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message);
ResponseBody body = response.body();
InputStreamEntity entity = new InputStreamEntity(body.byteStream(), body.contentLength());
httpResponse.setEntity(entity);
Headers headers = response.headers();
for (int i = 0, size = headers.size(); i < size; i++) {
String name = headers.name(i);
String value = headers.value(i);
httpResponse.addHeader(name, value);
if ("Content-Type".equalsIgnoreCase(name)) {
entity.setContentType(value);
} else if ("Content-Encoding".equalsIgnoreCase(name)) {
entity.setContentEncoding(value);
}
}
return httpResponse;
}
示例2: performRequest
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion,
connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
response.setEntity(entityFromConnection(connection));
}
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}
示例3: performRequest
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException,
AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(),
connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}
示例4: performRequest
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion,
connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}
示例5: performRequest
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (this.mUrlRewriter != null) {
String rewritten = this.mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
HttpURLConnection connection = openConnection(new URL(url), request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, (String) map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
if (connection.getResponseCode() == -1) {
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection
.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
response.setEntity(entityFromConnection(connection));
}
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
response.addHeader(new BasicHeader((String) header.getKey(), (String) ((List)
header.getValue()).get(0)));
}
}
return response;
}
示例6: performRequest
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (this.mUrlRewriter != null) {
String rewritten = this.mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
HttpURLConnection connection = openConnection(new URL(url), request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, (String) map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
if (connection.getResponseCode() == -1) {
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
BasicHttpResponse response = new BasicHttpResponse(new BasicStatusLine(protocolVersion,
connection.getResponseCode(), connection.getResponseMessage()));
response.setEntity(entityFromConnection(connection));
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
response.addHeader(new BasicHeader((String) header.getKey(), (String) ((List)
header.getValue()).get(0)));
}
}
return response;
}
示例7: performRequest
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (this.mUrlRewriter != null) {
String rewritten = this.mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
HttpURLConnection connection = openConnection(new URL(url), request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, (String) map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
if (connection.getResponseCode() == -1) {
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
response.setEntity(entityFromConnection(connection));
}
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
response.addHeader(new BasicHeader((String) header.getKey(), (String) ((List) header.getValue()).get(0)));
}
}
return response;
}
示例8: performRequest
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError
{
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion,
connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
response.setEntity(entityFromConnection(connection));
}
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}
示例9: testMultipleAllows
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
@Test
public void testMultipleAllows() {
final ProtocolVersion proto = new ProtocolVersion("HTTP", 1, 1);
final BasicStatusLine line = new BasicStatusLine(proto, 200, "test reason");
final BasicHttpResponse resp = new BasicHttpResponse(line);
resp.addHeader("Allow", "POST");
resp.addHeader("Allow", "GET");
final HttpOptions opt = new HttpOptions();
final Set<String> methodsName = opt.getAllowedMethods(resp);
Assert.assertTrue(methodsName.contains("POST"));
Assert.assertTrue(methodsName.contains("GET"));
}
示例10: performRequest
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
String friendlyName = System.currentTimeMillis() + "";
StethoURLConnectionManager connectionManager = new StethoURLConnectionManager(friendlyName);
String urlString = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
URL url = new URL(urlString);
HttpURLConnection connection = openConnection(url, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
connection.addRequestProperty("Accept-Encoding","gzip");
boolean isPreConnected = setConnectionParametersForRequest(connectionManager, connection, request);
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
if (!isPreConnected) {
preConnect(connectionManager,connection,null);
}
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion,
connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
postConnect(connectionManager);
if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
response.setEntity(entityFromConnection(connection));
}
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}
示例11: performRequest
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
@Override
public HttpResponse performRequest(Request<?> request,
Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
int timeoutMs = request.getTimeoutMs();
// okhttp 3.0以後的版本構建OkHttpClient使用Builder
OkHttpClient.Builder builder = mClient.newBuilder();
builder.connectTimeout(timeoutMs, TimeUnit.MILLISECONDS)
.readTimeout(timeoutMs, TimeUnit.MILLISECONDS)
.writeTimeout(timeoutMs, TimeUnit.MILLISECONDS);
OkHttpClient client = builder.build();
okhttp3.Request.Builder okHttpRequestBuilder = new okhttp3.Request.Builder();
okHttpRequestBuilder.url(request.getUrl());
Map<String, String> headers = request.getHeaders();
for (final Map.Entry<String, String> entry : headers.entrySet()) {
okHttpRequestBuilder.addHeader(entry.getKey(), entry.getValue());
}
for (final String name : additionalHeaders.keySet()) {
// 這裏用header方法,如果有重複的name,會覆蓋,否則某些請求會被判定為非法
okHttpRequestBuilder.header(name, additionalHeaders.get(name));
}
setConnectionParametersForRequest(okHttpRequestBuilder, request);
okhttp3.Request okHttpRequest = okHttpRequestBuilder.build();
Call okHttpCall = client.newCall(okHttpRequest);
Response okHttpResponse = okHttpCall.execute();
StatusLine responseStatus = new BasicStatusLine(
parseProtocol(okHttpResponse.protocol()), okHttpResponse.code(),
okHttpResponse.message());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromOkHttpResponse(okHttpResponse));
Headers responseHeaders = okHttpResponse.headers();
for (int i = 0, len = responseHeaders.size(); i < len; i++) {
final String name = responseHeaders.name(i), value = responseHeaders.value(i);
if (name != null) {
response.addHeader(new BasicHeader(name, value));
}
}
return response;
}
示例12: performRequest
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion,
connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
response.setEntity(entityFromConnection(connection));
}
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
List<String> values = header.getValue();
String value = "";
for (String v : values) {
value += " " + v;
}
Header h = new BasicHeader(header.getKey(), value);
response.addHeader(h);
}
}
return response;
}
示例13: performRequest
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
@Override
public synchronized HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
/*init request builder*/
okhttp3.Request.Builder okRequestBuilder = new okhttp3.Request.Builder().url(url)
.cacheControl(new CacheControl.Builder()
.maxAge(0, TimeUnit.SECONDS)
.build());
/*set request headers*/
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
for (String headerName : map.keySet()) {
okRequestBuilder.addHeader(headerName, map.get(headerName));
}
/*set request method*/
setRequestMethod(okRequestBuilder, request);
okhttp3.Request okRequest = okRequestBuilder.build();
/*init request client*/
int timeoutMs = request.getTimeoutMs();
OkHttpClient.Builder okClientBuilder = new OkHttpClient.Builder();
okClientBuilder.followRedirects(HttpURLConnection.getFollowRedirects())
.followSslRedirects(HttpURLConnection.getFollowRedirects())
.connectTimeout(timeoutMs, TimeUnit.MILLISECONDS)
.readTimeout(timeoutMs, TimeUnit.MILLISECONDS);
if (okRequest.isHttps() && mSslSocketFactory != null) {
okClientBuilder.sslSocketFactory(mSslSocketFactory);
}
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
OkHttpClient okClient = okClientBuilder.build();
Response okResponse = okClient.newCall(okRequest).execute();
int responseCode = okResponse.code();
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion,
responseCode, okResponse.message());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
response.setEntity(entityFromOkHttp(okResponse));
}
for (int i = 0; i < okResponse.headers().size(); i++) {
String name = okResponse.headers().name(i);
String value = okResponse.headers().value(i);
Header h = new BasicHeader(name, value);
response.addHeader(h);
}
return response;
}
示例14: performRequest
import org.apache.http.message.BasicHttpResponse; //導入方法依賴的package包/類
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
// 獲取 Volley 抽象請求 Request 中的 String 類型的 Url
String url = request.getUrl();
// 實例化一個 HashMap 來存放 Header 信息
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
// 判斷是否有 Url 重寫的邏輯
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
// 重新賦值上 UrlRewriter 接口 重寫的 Url
url = rewritten;
}
// 實例化一個 java.net.Url 對象
URL parsedUrl = new URL(url);
/*
* 將 URL 對象 和 Volley 的抽象請求 Request 傳入到 openConnection(...) 方法內
* 完成 Volley 抽象請求 Request -> HttpURLConnection 的轉換過渡
* 此時拿到一個 HttpURLConnection 對象
*/
HttpURLConnection connection = openConnection(parsedUrl, request);
// 根據剛才存放 Header 信息的 Map,給 HttpURLConnection 添加頭信息
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
// 設置 HttpURLConnection 請求方法類型
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
/*
* 初始化 一個 Apache 的 HTTP 協議 ( ProtocolVersion )
*/
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
/*
* 正常情況下 HttpURLConnection 是依靠 connect() 發請求的
*
* 但是 HttpURLConnection 的 getInputStream() 和 getOutputStream() 也會自動調用 connect()
*
* HttpURLConnection 的 getResponseCode() 會調用 getInputStream()
* 然後 getInputStream() 又會自動調用 connect(),於是
*
* 這裏就是 發請求了
*/
int responseCode = connection.getResponseCode();
// responseCode == -1 表示 沒有返回內容
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
// 實例化 org.apache.http.StatusLine 對象
StatusLine responseStatus = new BasicStatusLine(protocolVersion,
connection.getResponseCode(), connection.getResponseMessage());
// 用 org.apache.http.StatusLine 去實例化一個 Apache 的 Response
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
/*
* 判斷請求結果 Response 是否存在 body
*
* 有的話,給剛才實例話的 Apache Response 設置 HttpEntity( 調用 entityFromConnection(...)
* 通過一個 HttpURLConnection 獲取其對應的 HttpEntity )
*/
if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
response.setEntity(entityFromConnection(connection));
}
// 設置 請求結果 Response 的頭信息
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
// 返回設置好的 Apache Response
return response;
}