最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Java中用OkHttp的请求传递header等参数

Java crifan 425浏览 0评论
折腾:
【未解决】用Java实现百度模拟登陆
期间,先要去搞清楚如何实现,用Java的OkHttp库去抓取网络时,传递header等参数
okhttp header
java – How to add headers to OkHttp request interceptor? – Stack Overflow
java – How to handle this OkHttp header response? – Stack Overflow
android – OKHttp adding headers – Stack Overflow
android – Adding auth token as header to okhttp – Stack Overflow
好像:
.header(“Authorization”, userToken)
就可以了?
OkHttp使用进阶 译自OkHttp Github官方教程 – GavinCT – 博客园
Headers (OkHttp 3.11.0 API)
Okhttp 请求添加公共参数、公共Headers 方法 – Android – CSDN博客
OKHttp设置Header的那些坑 | Glan Wang
http://glanwang.com/2017/06/21/Android/OKHttp设置Header的那些坑/
Retrofit 2.0 对Okhttp每个Request统一添加header和Paramet… – 简书
Recipes · square/okhttp Wiki
Accessing Headers

Typically HTTP headers work like a Map<String, String>: each field has one value or none. But some headers permit multiple values, like Guava's Multimap. For example, it's legal and common for an HTTP response to supply multiple Vary headers. OkHttp's APIs attempt to make both cases comfortable.
When writing request headers, use header(name, value) to set the only occurrence of name to value. If there are existing values, they will be removed before the new value is added. Use addHeader(name, value) to add a header without removing the headers already present.
When reading response a header, use header(name) to return the last occurrence of the named value. Usually this is also the only occurrence! If no value is present, header(name) will return null. To read all of a field's values as a list, use headers(name).
To visit all headers, use the Headers class which supports access by index.
  private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("https://api.github.com/repos/square/okhttp/issues")
        .header("User-Agent", "OkHttp Headers.java")
        .addHeader("Accept", "application/json; q=0.5")
        .addHeader("Accept", "application/vnd.github.v3+json")
        .build();

    try (Response response = client.newCall(request).execute()) {
      if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

      System.out.println("Server: " + response.header("Server"));
      System.out.println("Date: " + response.header("Date"));
      System.out.println("Vary: " + response.headers("Vary"));
    }
  }
okhttp/AccessHeaders.java at master · square/okhttp
【总结】
去试试用.header(key, value):
Request request = new Request.Builder()
        .url(url)
        .header("User-Agent", UserAgent_Mac_Chrome)
        .header("Host", "www.baidu.com")
        .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
        .header("Accept-Encoding", "gzip, deflate, br")
        .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
        .header("Cache-Control", "no-cache")
        .build();
是可以获取正常返回的值的:
注:
关于所谓的interceptor,暂时用不到。等用到了再去研究,再去参考:
java – How to add headers to OkHttp request interceptor? – Stack Overflow

转载请注明:在路上 » 【已解决】Java中用OkHttp的请求传递header等参数

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.150 seconds, using 20.68MB memory