使用OKHTTP,Android帖子请求失败

时间:2016-08-29 18:31:44

标签: java android okhttp

我是Android开发新手,我正在尝试使用OKHTTP3

登录https://m.colorado.edu/mycuinfo/

以下是我的代码

        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网址为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>

&#39; 有什么想法吗?

我个人认为它应该是关于会话和cookie但我不知道如何使用OKHTTP

2 个答案:

答案 0 :(得分:1)

我认为您不应该尝试传递查询参数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();

答案 1 :(得分:0)

要添加Rafael Cardoso的评论,您肯定不应使用查询参数来记录用户。这被视为安全风险。通过安全连接使用RequestBody是优选的。

相关问题