Giorgos Myrianthous
Giorgos Myrianthous

Reputation: 39950

Retrieve cookie and send it in subsequent POST requests

I want to read two numbers (randomly generated) from a website which are then used in order to compute a result and then submit the result using a POST request. To do so, I will also need to submit the cookie of that session so that the system is aware of the random numbers which have been produced in that particular session.

In order to read the numbers I am using Jsoup:

Document document = Jsoup.parse(Jsoup.connect("http://website.com/getNumbers").get().select("strong").text());
String[] numbers = document.text().split(" ");
String answer = methodThatComputesTheDesiredOutput(numbers);

Now I want to send a POST request that includes answer and cookies of that session. Here's a partially implemented POST request that includes only the one parameter (answer):

HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://website.com/submitAnswer");
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
params.add(new BasicNameValuePair("answer", answer);
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

How can I obtain the cookie when reading the document and then use it as a parameter of the POST request?

Upvotes: 1

Views: 759

Answers (2)

codewizard
codewizard

Reputation: 1

Send your first request like below :-

Response res = Jsoup.connect("login Site URL")
        .method(Method.GET)
        .execute();

Now get the cookies and send new request with the cookies something like below :-

 CookieStore cookieStore = new BasicCookieStore();
     for (String key : cookies.keySet()) {
        Cookie cookie = new Cookie(key, cookies.get(key));
        cookieStore.addCookie((org.apache.http.cookie.Cookie) cookie);
    }
       HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://website.com/submitAnswer");
        httpClient.execute(httpPost,context);

Upvotes: 0

Ranjith
Ranjith

Reputation: 1739

Extract cookies using jsoup in the following way:

    Response response = Jsoup.connect("http://website.com/getNumbers").execute();
    Map<String, String> cookies = response.cookies();
    Document document = Jsoup.parse(response.body());

Create a BasicCookieStore using the cookies extracted using jsoup. Create a HttpContext containing the cookie store and pass it while executing your next request.

    BasicCookieStore cookieStore = new BasicCookieStore();
    for (Entry<String, String> cookieEntry : cookies.entrySet()) {
        BasicClientCookie cookie = new BasicClientCookie(cookieEntry.getKey(), cookieEntry.getValue());
        cookie.setDomain(".example.com");
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
    }


    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

    HttpPost httpPost = new HttpPost("http://website.com/submitAnswer");
    List<NameValuePair> params = new ArrayList<NameValuePair>(1);
    params.add(new BasicNameValuePair("answer", answer);
    httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

    httpClient.execute(httpPost, localContext);

Upvotes: 2

Related Questions