2017-01-09 35 views
2

我每次this現場進行OKHttp POST請求的響應代碼是302和響應主體是:OKHttp柄302

<html> 
    <head> 
     <title>Object moved</title> 
    </head> 
    <body> 
     <h2>Object moved to <a href="/GradebookSummary.aspx">here</a>. 
     </h2> 
    </body> 
</html> 

這裏是我的代碼:

OkHttpClient client = new OkHttpClient().newBuilder() 
          .followRedirects(false) 
          .followSslRedirects(false) 
          .build(); 
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); 
        RequestBody body = RequestBody.create(mediaType, "checkCookiesEnabled=true&checkMobileDevice=false&checkStandaloneMode=false&checkTabletDevice=false&portalAccountUsername=username&portalAccountPassword=password"); 
        Request request = new Request.Builder() 
          .url("https://hac.chicousd.org/LoginParent.aspx?page=GradebookSummary.aspx") 
          .post(body) 
          .addHeader("content-type", "application/x-www-form-urlencoded") 
          .addHeader("cache-control", "no-cache") 
          .build(); 

        Response response = client.newCall(request).execute(); 

我的問題是:我如何處理能夠前往新位置的響應?

回答

0

所有我需要做的就是添加持久cookie處理程序,我發現here得到新的位置。

+0

我有同樣的問題,不適合我:/ –

3

OKhttp默認遵循重定向,但由於您在這種情況下顯式禁用了它,因此您需要檢查響應的Location標題以查找重定向的url。

編輯:您可以通過

String location = response.header('Location'); 
+0

你能提供一個例子 – Jacolack

+0

刪除此:'.followRedirects(假)' –

+0

@JesseWilson我試過了,它給了我一個頁面,說:你必須輸入用戶名和密碼 – Jacolack