每次在this站点上执行OKHttp Post请求时,响应代码为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();我的问题是:我如何处理响应才能到达新的位置?
发布于 2017-01-09 11:47:58
我所需要做的就是添加一个持久化cookie处理程序,我找到了here。
发布于 2017-01-09 09:41:33
默认情况下,OKhttp遵循重定向,但由于您在本例中显式禁用了它,因此需要检查响应的Location头来查找重定向的url。
编辑:您可以通过以下方式获取新位置
String location = response.header('Location');https://stackoverflow.com/questions/41539750
复制相似问题