当前位置: 首页>>代码示例>>Java>>正文


Java BasicHttpResponse.addHeader方法代码示例

本文整理汇总了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;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:24,代码来源:OkApacheClient.java

示例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;
}
 
开发者ID:MLNO,项目名称:airgram,代码行数:43,代码来源:HurlStack.java

示例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;
}
 
开发者ID:HanyeeWang,项目名称:GeekZone,代码行数:41,代码来源:BaseStack.java

示例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;
}
 
开发者ID:dreaminglion,项目名称:iosched-reader,代码行数:41,代码来源:HurlStack.java

示例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;
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:37,代码来源:MyHttpStack.java

示例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;
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:34,代码来源:OkHttpStack.java

示例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;
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:34,代码来源:HurlStack.java

示例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;
}
 
开发者ID:TeskaLabs,项目名称:SeaCat-Volley-Android,代码行数:39,代码来源:SeaCatStack.java

示例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"));
}
 
开发者ID:MyPureCloud,项目名称:purecloud-iot,代码行数:15,代码来源:TestHttpOptions.java

示例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;
}
 
开发者ID:DLKanth,项目名称:Stetho-Volley,代码行数:51,代码来源:StethoVolleyStack.java

示例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;
}
 
开发者ID:tengbinlive,项目名称:ooooim_android,代码行数:47,代码来源:OkHttpStack.java

示例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;
}
 
开发者ID:joy-inc,项目名称:core-http,代码行数:48,代码来源:HurlStack.java

示例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;
}
 
开发者ID:jessie345,项目名称:RealArchitecture,代码行数:76,代码来源:OkHttpStack.java

示例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;
}
 
开发者ID:CaMnter,项目名称:SaveVolley,代码行数:81,代码来源:HurlStack.java


注:本文中的org.apache.http.message.BasicHttpResponse.addHeader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。