0

I am sending a POST request using HTMLUnit that sends keywords as parameters. An example of the URL is:

website.com/foo/bar/api?keywords=word1,word2,word3&language=en

The problem is my application is dynamically picking these words and the amount of words can go up to 10 or 20 or even more. How do you append a Set of words as values to a HTTP request. My code at the moment is:

requestSettings = new WebRequest(new URL("website.com/foo/bar/api?"),
                        HttpMethod.POST);
                Iterator<String> itr = list.iterator();
                while(itr.hasNext()) {
                    requestSettings.getRequestParameters()
                            .add(new NameValuePair("keywords[]", itr.next()));
                }
                requestSettings.getRequestParameters().add(new NameValuePair("language", "en"));
                System.out.println(requestSettings.getUrl().toString());
                response = webClient.getPage(requestSettings).getWebResponse();

This code does not return a valid respone. What am I doing wrong here?

GreenGodot
  • 6,030
  • 10
  • 37
  • 66
  • You need to use WebRequest.setRequestBody(). Other than this, other headers may be needed by the server, e.g. Referer. Please check the headers sent by HtmlUnit vs what is sent by real browsers. You can also provide specific URL case, so others can reproduce your issue – Ahmed Ashour Jul 21 '15 at 15:00

1 Answers1

1

Give this a try:

using (var client = new WebClient())
{
     var dataObject = new {
         KeyWords = "one, two, three"
     };

     var serializer = new JavaScriptSerializer();
     var json = serializer.Serialize(dataObject);

     var response = client.UploadString("yourUrl", json);
}
Stephen Brickner
  • 2,584
  • 1
  • 11
  • 19
  • Regarding your first paragraph, I realize that, putting it in the request body is what I am trying to do in my code.I am sorry if that is not clear. – GreenGodot Jul 21 '15 at 14:29
  • I'm using pure Java though for my application, not Javascript. – GreenGodot Jul 22 '15 at 09:34
  • That's C# in my example but it should give you the idea of what you need to do. Wrap your keywords into an object, serialize it and then post the data through the request body. – Stephen Brickner Jul 22 '15 at 09:42