Example usage for org.apache.http.message BasicHttpResponse addHeader

List of usage examples for org.apache.http.message BasicHttpResponse addHeader

Introduction

In this page you can find the example usage for org.apache.http.message BasicHttpResponse addHeader.

Prototype

public void addHeader(Header header) 

Source Link

Usage

From source file:ai.eve.volley.stack.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request) throws IOException, AuthFailureError {
    HashMap<String, String> map = new HashMap<String, String>();
    if (!TextUtils.isEmpty(mUserAgent)) {
        map.put(HTTP.USER_AGENT, mUserAgent);
    }//w ww  . j  a  v  a 2s .  c o m
    if (!TextUtils.isEmpty(mSignInfo)) {
        map.put("SIGN", ESecurity.Encrypt(mSignInfo));
    }
    map.putAll(request.getHeaders());

    URL parsedUrl = new URL(request.getUrl());
    HttpURLConnection connection = openConnection(parsedUrl, request);

    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    if (EApplication.getCookies() != null) {
        ELog.I("cookie", EApplication.getCookies().toString());
        connection.addRequestProperty("Cookie", EApplication.getReqCookies());
    } else {
        ELog.I("cookie", "null");
    }
    setConnectionParametersForRequest(connection, request);
    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(new ProtocolVersion("HTTP", 1, 1),
            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) {
            if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
                List<String> cookies = header.getValue();
                HashMap<String, HttpCookie> cookieMap = new HashMap<String, HttpCookie>();
                for (String string : cookies) {
                    List<HttpCookie> cookie = HttpCookie.parse(string);
                    for (HttpCookie httpCookie : cookie) {
                        if (httpCookie.getDomain() != null && httpCookie.getPath() != null) {
                            cookieMap.put(httpCookie.getName() + httpCookie.getDomain() + httpCookie.getPath(),
                                    httpCookie);
                        } else if (httpCookie.getDomain() == null && httpCookie.getPath() != null) {
                            cookieMap.put(httpCookie.getName() + httpCookie.getPath(), httpCookie);
                        } else if (httpCookie.getDomain() != null && httpCookie.getPath() == null) {
                            cookieMap.put(httpCookie.getName() + httpCookie.getDomain(), httpCookie);
                        } else {
                            cookieMap.put(httpCookie.getName(), httpCookie);
                        }
                    }
                }

                EApplication.setCookies(cookieMap);
                if (EApplication.getCookies() != null) {
                    ELog.I("?cookie", EApplication.getCookies().toString());
                }
            } else {
                Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
                response.addHeader(h);
            }
        }
    }

    return response;
}