I’m trying to go to my university’s website and parse the necessary information (such as a schedule and so on). I checked the work on reqres.in and everything works, but on my site there is no answer (onFailure is called). I wrote all the same in Python, everything worked and the site started up. Maybe I’m missing something, I ask for help.
Python code that works
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
Code in Android Studio that does not work
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", "username@gmail.com")
.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());
}
});
}
I hope now attached the right (stack trace)
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)
I can’t understand how to transfer the code from Python to the android application. I understand that maybe I'm missing the start of a web session, but I don’t know how to implement this in Java.
The answer began to come, but I can’t show it. Added these lines
ClearableCookieJar cookieJar =
new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext()));
OkHttpClient client = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.followRedirects(FALSE)
.followSslRedirects(FALSE)
.build();
I'm trying to get the answer like this, and I get only 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);
}