Answer the question
In order to leave comments, you need to log in
How to add form-data request to post?
How to fix the code to correctly submit "form-data":
private static Request request = null;
private static OkHttpClient client= new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.put(url, cookies);
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url);
return cookies != null ? cookies : new ArrayList<Cookie>();
}
}).build();
RequestBody formBody = new FormBody.Builder()
.add("user_name", "значение-1")//urlHttp[1]
.add("user_pass", "значение-2")//urlHttp[2]
.build();
request = new Request.Builder()
.url("https://сайт.ru/")
.post(formBody)
.build();
System.out.println(client
.newCall(request)
.execute().body().string());
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question