获取User-Agent

版权声明:本文为博主原创文章,转载请注明出处(http://blog.csdn.net/jaden_hool) https://blog.csdn.net/Jaden_hool/article/details/78196678
/**
 * @return 浏览器的userAgent, 某些机型含有中文。
 */
public static String getUserAgent() {
    String userAgent = getWebViewUserAgent();
    if (StringUtil.isEmpty(userAgent)) userAgent = getSystemUserAgent();
    return userAgent;
}

private static String getWebViewUserAgent() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        try {
            return WebSettings.getDefaultUserAgent(mContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

private static String getSystemUserAgent() {
    return System.getProperty("http.agent");
}

自定义Volley请求头中的user-agent:

Volley的请求头中如果没有自定义”User-agent”字段,构建请求的时候就会自动加载系统的User-agent,所以只要在getHeaders()方法中加上”User_agent”字段就可以了。

@Override
public Map<String, String> getHeaders(){
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("User-agent", "YOUR_USER_AGENT");
    return headers;
}

猜你喜欢

转载自blog.csdn.net/Jaden_hool/article/details/78196678