A
A
artshelom2018-02-08 21:30:05
Java
artshelom, 2018-02-08 21:30:05

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());

I make exactly the same request through postman, but the site's responses are different. Maybe I'm not writing correctly in androyd or I'm missing something ??
post man:
5a7c96ce6a5f3128962144.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question