使用OkHttp在网站上进行授权

问题描述 投票:3回答:3

我正在尝试访问我大学的网站并分析必要的信息(例如时间表等)。我在reqres.in上检查了工作,一切正常,但是在我的网站上没有答案(称为onFailure)。我用Python编写了相同的代码,一切正常,并且网站启动了。也许我缺少一些东西,我寻求帮助。

有效的Python代码

import requests
import re

s = requests.Session()
s.get("https://lk.sut.ru/cabinet/")

def login_bonch(login, password):
    data = {'users': login,
            'parole': password}

    r = s.post("https://lk.sut.ru/cabinet/lib/autentificationok.php", data = data)
    return r.text // Function returns 1

Android Studio中的代码无效

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();

        OkHttpClient client = new OkHttpClient();

        RequestBody formBody = new FormBody.Builder()
                .add("users", "[email protected]")
                .add("parole", "password123")
                .build();

        Request request = new Request.Builder()
                .url("https://lk.sut.ru/cabinet/lib/autentificationok.php")
                .post(formBody)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.d("SendLog", "Error");
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                Log.d("SendLog", " " + response.body().string());
            }
        });
    }

我希望现在附上正确的(堆栈跟踪)

2020-06-11 03:47:46.560 2287-2315/com.foxartstd.lkbonchapp W/System.err: java.net.ProtocolException: Too many follow-up requests: 21
2020-06-11 03:47:46.560 2287-2315/com.foxartstd.lkbonchapp W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:127)
2020-06-11 03:47:46.560 2287-2315/com.foxartstd.lkbonchapp W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
2020-06-11 03:47:46.560 2287-2315/com.foxartstd.lkbonchapp W/System.err:     at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197)
2020-06-11 03:47:46.560 2287-2315/com.foxartstd.lkbonchapp W/System.err:     at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:502)
2020-06-11 03:47:46.560 2287-2315/com.foxartstd.lkbonchapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
2020-06-11 03:47:46.561 2287-2315/com.foxartstd.lkbonchapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
2020-06-11 03:47:46.561 2287-2315/com.foxartstd.lkbonchapp W/System.err:     at java.lang.Thread.run(Thread.java:761)

我不明白如何将代码从Python传输到android应用程序。我知道也许我错过了网络会话的开始,但是我不知道如何用Java实现。

答案开始出现,但我无法显示。添加了这些行

ClearableCookieJar cookieJar =
                new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext()));

OkHttpClient client = new OkHttpClient.Builder()
                .cookieJar(cookieJar)
                .followRedirects(FALSE)
                .followSslRedirects(FALSE)
                .build();

我试图得到这样的答案,而我只得到Ok]

 client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.d("SendLog", "Error");
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String MyResult = response.body().string();
                Log.d("SendLog", "Ok " + MyResult);
            }
android authentication authorization okhttp
3个回答
0
投票

您应该使用RequestBody.create而不是FormBody

RequestBody formBody = RequestBody.create("application/json; charset=utf-8", "{\"users\":\"[email protected]\",\"parole\":\"password123\"}")
Request request = new Request.Builder()
          .url("https://lk.sut.ru/cabinet/lib/autentificationok.php")
          .post(formBody)
          .build();

0
投票

我怀疑您将需要一个CookieJar来保持第一次呼叫时的会话设置

https://square.github.io/okhttp/4.x/okhttp/okhttp3/-cookie-jar/

https://github.com/franmontiel/PersistentCookieJar

ClearableCookieJar cookieJar =
                new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context));
OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .cookieJar(cookieJar)
                .build();

但是您的代码示例似乎并不完整。


0
投票

添加这些行解决了我的问题

ClearableCookieJar cookieJar =
                new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext()));

OkHttpClient client = new OkHttpClient.Builder()
                .cookieJar(cookieJar)
                .followRedirects(FALSE)
                .followSslRedirects(FALSE)
                .build();

正是这些截止日期没有让我犯错

.followRedirects(FALSE)
.followSslRedirects(FALSE)

关于答案,这些已经是网站的问题。如果成功则返回1,否则返回1。授权仍然没有出来,但是主要问题已经解决。

© www.soinside.com 2019 - 2024. All rights reserved.