我是新的Android开发,我试图登录https://m.colorado.edu/mycuinfo/与OKHTTP3
以下是我的代码
OkHttpClient client = new OkHttpClient();
HttpUrl url = HttpUrl.parse(urlString).newBuilder()
.addQueryParameter("j_username", username)
.addQueryParameter("j_password", password)
.build();
Request request = new Request.Builder()
.url(url)
.build();
try {
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
} catch (IOException e){
e.printStackTrace();
}
POST url为https://m.colorado.edu/mycuinfo/j_security_check
但是响应只是登录页面的源代码。
<form data-ajax="false" id="LoginForm" name="lForm" method="POST" action="j_security_check" autocomplete="off">
<p></p>
<b>CU Login Name:</b><br> <input tabindex="1" type="text" name="j_username" size="20" data-theme="c" />
<p></p>
<b>IdentiKey Password:</b><br> <input tabindex="2" type="password" name="j_password" size="20" data-theme="c" />
<p></p>
<button data-ajax="false" data-theme="a" value="Log in" tabindex="3">Login</button>
<p></p>
'任何想法吗?
我个人认为应该是关于会话和cookie但是我不知道如何使用OKHTTP
我不认为你应该尝试做一个POST
请求传递查询参数。QueryParameters
通常用于GET
请求。尝试构建一个RequestBody
并使用它执行一个请求,如下所示:
RequestBody formBody = new FormEncodingBuilder()
.add("j_username", username)
.add("j_password", password)
.build();
Request request = new Request.Builder()
.url(urlString)
.post(formBody)
.build();
Response response = client.newCall(request).execute();
添加到Rafael Cardoso的评论中,您绝对不应该使用查询参数来登录用户。这被认为是一种安全风险。通过安全连接使用RequestBody
是可取的。