简体   繁体   English

在Android应用程序中使用volley api访问所有响应头

[英]Accessing all response headers using volley api in android application

while making connection using HttpClient in android from HttpResponse able to get all possible "set-cookie" header value (JESSIONID and XSCRF-TOKEN).Check below screenshot. 从HttpResponse在Android中使用HttpClient进行连接时能够获得所有可能的“set-cookie”标头值(JESSIONID和XSCRF-TOKEN)。查看下面的截图。 HttpResponse标题

Now working with android studio with volley api for connection , i am getting only single value of "set-cookie" header (JESSIONID only).See below : 现在使用android studio与volley api进行连接,我只得到“set-cookie”标题的单个值(仅限JESSIONID)。参见下文: 排球响应头球

I have also check https://groups.google.com/forum/#!topic/volley-users/rNTlV-LORzY . 我还查看了https://groups.google.com/forum/#!topic/volley-users/rNTlV-LORzY For which have to make change in volley api jar project. 必须在volley api jar项目中进行更改。 But don't know how to edit volley api. 但不知道如何编辑凌空api。 If any other solution present kindly guide. 如果有任何其他解决方案,请提供指导。

Kindly help to retrieve multiple value of "set-cookie" using volley api. 请使用volley api帮助检索“set-cookie”的多个值。

Problem: 问题:
The problem is inside the Volley unfortunately. 不幸的是,问题出在了Volley内部。 I had this problem and after many searches i figured out that there is a method called convertHeaders in BasicNetwork class that handles headers like this: 我有这个问题,经过多次搜索,我convertHeadersBasicNetwork类中有一个名为convertHeaders的方法,它处理这样的标题:

protected static Map<String, String> convertHeaders(Header[] headers) {
    Map<String, String> result = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    for (int i = 0; i < headers.length; i++) {
        result.put(headers[i].getName(), headers[i].getValue());
    }
    return result;
}

You see the result is Map<String, String> which can't contain same keys with different values. 您会看到结果是Map<String, String> ,它不能包含具有不同值的相同键。 so you always have only last cookie. 所以你总是只有最后一个cookie。
The standard of cookie setting tells us we should separate cookies with ; cookie设置的标准告诉我们应该将cookie分开; for example if you want to contain 2 key-value in a request cookie you should put them like this: Cookies: k1=v1;k2=v2 例如,如果你想在请求cookie中包含2个键值,你应该像这样: Cookies: k1=v1;k2=v2

Solution: 解:
In your case you have two options. 在您的情况下,您有两个选择。

1 - change your code in Server-Side so that the response contains only 1 Set-Cookie separated key-values by ; 1 - 更改服务器端的代码,以便响应仅包含1个 Set-Cookie分隔的键值; . example of your response : 你回复的例子

Set-Cookie: JESSIONID=qZtQ...;Path=/;HttpOnly;XSRF-TOKEN=6c65...

2 - get Volley source code and change that buggy method and make a fixed .jar again! 2 - 获取Volley源代码并更改该buggy方法并再次修复.jar! this option is my favorite cause you didn't touch the response of server 这个选项是我最喜欢的,因为你没有触及服务器的响应

My implementation of this method is: 我对这种方法的实现是:

protected static Map<String, String> convertHeaders(Header[] headers) {
    TreeMap result = new TreeMap(String.CASE_INSENSITIVE_ORDER);

    for(int i = 0; i < headers.length; ++i) {
        String headerName = headers[i].getName();
        if(!result.containsKey(headerName)) {
            result.put(headers[i].getName(), headers[i].getValue());
        } else {
            String value = (String)result.get(headerName);
            String mergedValue = value + ";" + headers[i].getValue();
            result.remove(headerName);
            result.put(headerName, mergedValue);
        }
    }

    return result;
}

There is workaround for this in: 有以下解决方法:

implementation "com.android.volley:volley:1.1.0"

"NetworkResponse (and Cache.Entry) now includes an "allHeaders" field which is the raw list of all headers returned by the server and thus can include duplicates by name." “NetworkResponse(和Cache.Entry)现在包含一个”allHeaders“字段,该字段是服务器返回的所有标头的原始列表,因此可以按名称包含重复项。”

Source: https://github.com/google/volley/issues/21 资料来源: https//github.com/google/volley/issues/21

Example: 例:

    private static final String COOKIE_KEY = "Set-Cookie";
    private static final String COOKIE_NAME = "NameOfOneOfTheCookies";

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        handleCookies(response);
        String parsed;
        try {
            parsed = new String(response.data, "utf-8");
        } catch (UnsupportedEncodingException e) {
            parsed = new String(response.data);
        }
        return Response.success(parsed,
                HttpHeaderParser.parseCacheHeaders(response));
    }

    private void handleCookies(NetworkResponse response) {
        for (Header header : response.allHeaders) {
            if (header.getName().equals(COOKIE_KEY) && header.getValue().startsWith(COOKIE_NAME)) {
                getCookies(response);
            }
        }
    }

    private void getCookies(NetworkResponse response) {
        ArrayList<String> cookiesList = new ArrayList<>();
        for (Header header : response.allHeaders) {
            if (header.getName().equals(COOKIE_KEY)) {
                cookiesList.add(header.getValue());
            }
        }
        // TODO Do something with the cookiesList
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM