[英]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,该服务器接受param1
, param2
等。 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¶m2=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, 它可以选择指定
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.