2017-10-12 96 views
2

我試圖通過使用CookieJar將保存的身份驗證Cookie添加到更多請求。並且,在檢查response.request().headers()時,獲取正確的身份驗證Cookie並將其保存到jar中效果很好,但無法找到cookie。OkHttp CookieJar無法將Cookie添加到請求

我發現這個特別奇怪,因爲我發現通過調試發現loadForRequest()被請求並返回正確的cookie。當在Postman中使用完全相同的cookie來僞造請求時,它會返回所需的結果(沒有登錄表單的頁面)。

有人可以試圖解釋我失蹤了嗎?其中罐子利用

class HTMLRoutes { 
    var scheme = "https" 
    var host = "www.mangaupdates.com" 
    val cookieJar = MULoginCookieJar() 
    var client = OkHttpClient.Builder() 
      .cookieJar(cookieJar) 
      .build() 
/*Other code*/ 

private fun getHTMLFromUrl(url: HttpUrl): String { 
     var request = Request.Builder() 
       .url(url) 
       .build() 
     client.newCall(request).execute().use { response -> 
     //Right before returning response the loadForRequest() method gets called in the MUCookieJar class 
      if (response.isSuccessful) { 
      //response.request.headers = "" 
       if (response.body() != null) { 
        return response.body()!!.string() 
       } else { 
        throw IOException("Response body is empty") 
       } 
      } else { 
       throw IOException("Unexpected code" + response) 
      } 
     } 
    } 
} 

我cookiejar

class MULoginCookieJar : CookieJar { 
    private var secureSessionCookie: Cookie? = null 

    override fun saveFromResponse(url: HttpUrl?, cookies: MutableList<Cookie>?) { 
     if (url != null && cookies != null) { 
      if (url.pathSegments().size > 0 && url.pathSegments()[0] == "login.html") { 
       for (cookie in cookies) { 
        if(cookie.name() == "secure_session") { 
         secureSessionCookie = cookie 
        } 
       } 
      } 
     } 
    } 

    override fun loadForRequest(url: HttpUrl?): List<Cookie>? { // url = https://www.mangaupdates.com/series.html?id=14829 
     if(url != null && url.pathSegments().size > 0 && url.pathSegments()[0] == "login.html") { 
      return emptyList() 
     } 

     val cookies: List<Cookie> = if(secureSessionCookie==null) emptyList() else listOf(secureSessionCookie!!) 
     return cookies // = ["secure_session=601bbc74; expires=Sun, 07 Oct 2018 20:45:24 GMT; domain=www.mangaupdates.com; path=/; secure; httponly"] 
    } 
} 

幫助

類是非常理解的。我已經很久沒有潛伏,但這是我第一次遇到這個問題。

+0

你在做什麼來檢查cookie是否未被髮送?請注意,取決於您的配置,攔截器可能在將Cookie添加到請求之前運行。 –

+0

@Jesse Wilson在'client.newCall(request).execute()。使用{response - >',response'存儲在response中的OkHttp Response有一個空字符串作爲響應後, request.headers'。我不知道這是否是確保Cookie未發送的最終方式,但是如果cookie已發送,我覺得應在頭文件中設置「Set-Cookie .......」。在我的實施中,我也不會有一個Interceptor。你建議添加一個嗎? –

+0

相同的response.networkResponse()。請求()? –

回答

0

我會建議使用現有的餅乾罐而不是創建自己的。 cookie的規則可能會變得很複雜。

import okhttp3.JavaNetCookieJar; 

CookieManager cookieManager = new CookieManager(); 
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); 
JavaNetCookieJar cookieJar = new JavaNetCookieJar(cookieManager); 

OkHttpClient client = new OkHttpClient.Builder().cookieJar(cookieJar).build(); 

也可以幫助調試你可以設置OkHttp debug logging with an interceptor

Logger logger = LoggerFactory.getLogger(HttpUtil.class); 
HttpLoggingInterceptor logging = 
    new HttpLoggingInterceptor((msg) -> { 
     logger.debug(msg); 
    }); 
logging.setLevel(Level.BODY); 

client.addNetworkInterceptor(logging);