Search code examples
javastruts2http-postapache-commons-httpclientstruts-action

How to send strings to server using Java?


I need to send a lot of strings to a web server using Java. I have a List<String> with huge amount of strings and I need to send it via POST request to the Struts2 action on the server side.

I have tried something starting with

HttpPost httppost = new HttpPost(urlStr);

but don't know how to use it.

On other side I have a Struts2 action, and getting the POST request is easy to me.

I think this solution is too close, but it doesn't solve my problem because it's using just one string : HTTP POST using JSON in Java

So, how to send many strings to a server using Java?


Solution

  • You should do somthing

    HttpPost httppost = new HttpPost(url);
    List<NameValuePair> params = new ArrayList<>();
    for(String s : list) 
      params.add(new BasicNameValuePair("param", s));
    httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    HttpResponse response = httpclient.execute(httppost);
    

    on the other side is an action mapped to the url has setter for param. It should be List<String> or String[]. The action when intercepted will populate that param property.