简体   繁体   中英

How to send text by HttpPost method?

I have no idea, how to send some text using HTTPCLIENT (java // apache) library. I need to send parameters by text to server. Any idea?

Assume you have some-remote-server as your remote server address and some-servlet as your remote servlet which accepts param1 , param2 etc.. with its respective values on request. If the remote servlet accept GET call you can use below to send the request;

  HttpClient httpClient = new HttpClient();
  GetMethod getMethod = new GetMethod(); //You could use PostMethod if servlet accept POST

  String request ="http://some-remote-server/some-servlet?param1=value1&param2=value2";
  httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
  getMethod.setURI(new URI(request, false, null));
  ...

And then recieve the response return from the remote servlet like this;

ObjectInputStream ois = new ObjectInputStream(getMethod.getResponseBodyAsStream());
ois.readObject();

If you can change the tool, try RestClient Tool for eclipse. It has great support for testing restful web-services. It has option to specify,

  1. Header Parameter,
  2. Query Parameter,
  3. Body Text
  4. Request type (GET,POST,PUT,DELETE,HEAD,OPTIONS,TRACE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM