简体   繁体   English

如何通过HttpPost方法发送文本?

[英]How to send text by HttpPost method?

I have no idea, how to send some text using HTTPCLIENT (java // apache) library. 我不知道如何使用HTTPCLIENT(java // apache)库发送一些文本。 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. 假设您有some-remote-server作为您的远程服务器地址, some-servlet作为您的远程servlet,该服务器接受param1param2等。 If the remote servlet accept GET call you can use below to send the request; 如果远程servlet接受GET调用,则可以使用下面的方法发送请求;

  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; 然后像这样从远程servlet接收响应返回;

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

If you can change the tool, try RestClient Tool for eclipse. 如果可以更改工具,请尝试使用RestClient工具进行蚀。 It has great support for testing restful web-services. 它为测试静态Web服务提供了强大的支持。 It has option to specify, 它可以选择指定

  1. Header Parameter, 标头参数,
  2. Query Parameter, 查询参数
  3. Body Text 文章主体
  4. Request type (GET,POST,PUT,DELETE,HEAD,OPTIONS,TRACE) 请求类型(GET,POST,PUT,DELETE,HEAD,OPTIONS,TRACE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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