【问题标题】:Android Volley Request - 403 Error, but works in browserAndroid Volley Request - 403 错误,但在浏览器中有效
【发布时间】:2020-07-01 11:06:52
【问题描述】:

我正在尝试请求一个公共网站。在这个例子中https://shop.rewe.de/mc/api/markets-stationary/98693

响应是一个带有本地存储的 JSON 对象。

如果我在干净的浏览器中执行此操作(已删除 cookie 和缓存数据),它可以正常工作。我在 Chrome 中使用了 websniffer 插件,因此我能够看到带有标头的请求。

所以只有 3 个标头参数。所以现在我想用 volley 和一个简单的自己的 StringRequest 类来做到这一点,我只是重写了 getheaders() 方法来设置这 3 个参数。

@Override
public Map<String, String> getHeaders(){
    HashMap<String, String> _headers = new HashMap<String, String>();
    _headers.put("Upgrade-Insecure-Requests", "1");
    _headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36");
    _headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
    return _headers;
}

现在我只是用 volley 做一个请求

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
    Network network = new BasicNetwork(new HurlStack());
    RequestQueue requestQueue = new RequestQueue(cache, network);
    requestQueue.start();
    
    String url ="https://shop.rewe.de/mc/api/markets-stationary/98693";
    StringRequestImpl stringRequest = new StringRequestImpl(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Do something with the response
                    String tst = response.toString();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    String tst = error.toString();
                }
            });
    // Add the request to the RequestQueue.
    requestQueue.add(stringRequest);
}

我得到的是带有状态码 403 的 com.android.volley.AuthFailureError

我以为我做的和我的 Chrome 浏览器完全一样。但为什么它在 Chrome 中有效,但在 volley 中无效?有人出主意吗?

【问题讨论】:

    标签: android android-volley


    【解决方案1】:

    对于有兴趣的人。服务器所有者现在正在使用 cloudfare bot 保护。我不知道他们如何将一个简单的请求识别为机器人,但也许我找到了解决方案。

    【讨论】:

      猜你喜欢
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      相关资源
      最近更新 更多