Java - Sending a post request with HtmlUnit
Asked Answered
J

2

14

Can't really find any help on this but I've been trying to send a post request with HtmlUnit. The code I have is:

final WebClient webClient = new WebClient();

// Instead of requesting the page directly we create a WebRequestSettings object
WebRequest requestSettings = new WebRequest(
  new URL("www.URLHERE.com"), HttpMethod.POST);

// Then we set the request parameters
requestSettings.setRequestParameters(new ArrayList());
requestSettings.getRequestParameters().add(new NameValuePair("name", "value"));
// Finally, we can get the page
HtmlPage page = webClient.getPage(requestSettings);

Is there an easier way I could carry out a POST request?

Jordanson answered 6/6, 2015 at 21:13 Comment(2)
Not using the Web Client.Riflery
Hello. You want an easier way. Ok but can you explain what you found hard or complex in your snippet ? Sorry, i don't see.Krissie
D
21

This is how it's done

public void post() throws Exception
{

    URL url = new URL("YOURURL");
    WebRequest requestSettings = new WebRequest(url, HttpMethod.POST);

    requestSettings.setAdditionalHeader("Accept", "*/*");
    requestSettings.setAdditionalHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    requestSettings.setAdditionalHeader("Referer", "REFURLHERE");
    requestSettings.setAdditionalHeader("Accept-Language", "en-US,en;q=0.8");
    requestSettings.setAdditionalHeader("Accept-Encoding", "gzip,deflate,sdch");
    requestSettings.setAdditionalHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
    requestSettings.setAdditionalHeader("X-Requested-With", "XMLHttpRequest");
    requestSettings.setAdditionalHeader("Cache-Control", "no-cache");
    requestSettings.setAdditionalHeader("Pragma", "no-cache");
    requestSettings.setAdditionalHeader("Origin", "https://YOURHOST");

    requestSettings.setRequestBody("REQUESTBODY");

    Page redirectPage = webClient.getPage(requestSettings);
}

You can customize it however you want. Add/remove headers, add/remove request body, etc ...

Designedly answered 9/6, 2015 at 19:53 Comment(0)
C
-2

There are n numbers of possible libraries using which you can call rest web services.

1) Apache Http client 2) Retrofit from Square 3) Volley from google

I have used Http Apache client and Retrofit both. Both are awesome.

Here is code example of Apache HTTP client to send Post request

String token = null;

    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost postRequest = new HttpPost(LOGIN_URL);
    StringBuilder sb = new StringBuilder();
    sb.append("{\"userName\":\"").append(user).append("\",").append("\"password\":\"").append(password).append("\"}");
    String content = sb.toString();
    StringEntity input = new StringEntity(content);
    input.setContentType("application/json");
    postRequest.setHeader("Content-Type", "application/json");
    postRequest.setHeader("Accept", "application/json");

    postRequest.setEntity(input);

    HttpResponse response = httpClient.execute(postRequest);

    if (response.getStatusLine().getStatusCode() != 201)
    {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
    }

    Header[] headers = response.getHeaders("X-Auth-Token");

    if (headers != null && headers.length > 0)
    {
        token = headers[0].getValue();
    }

    return token;
Claimant answered 6/6, 2015 at 21:29 Comment(2)
Thanks for the reply. I have used Apache httpclient but with the site that I have to access requires you to wait 5 seconds for the ddos protection so when I use httpclient I just get the source code from the ddos protection. It important I can get the source code of the actual page as I need to parse the response after the post has been sentJordanson
aviundefined, I don't see in which way, using apache http client and other api you propose are easier as htmlunit. Can you argument please ?Krissie

© 2022 - 2024 — McMap. All rights reserved.