Apache 的HttpClient 提供很多工具让开发者使用,其中常用的一种是调用http的请求,下面代码就是模拟了http的post和get请求。
package cn.tang.test.demo.httpclient; import java.io.IOException; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; public class TestInvoke { private static final String host ="10.1.1.225"; // private static final String host ="localhost"; public static void main(String[] args) { try { get(); post(); get2(); } catch (Exception e) { e.printStackTrace(); } } public static void post() { String url = "http://"+host+":8080/method/invoke"; PostMethod method = new PostMethod(url); NameValuePair[] data = { new NameValuePair("key", "39d7e330-d271-4fe8-904a-aaf872962431"), new NameValuePair("invokeMethod", "messageReceive"), new NameValuePair("jsonData", "messageReceive") }; method.setRequestBody(data); execute(method); } public static void get2() { String url = "http://"+host+":8080/method/invoke?key=39d7e330-d271-4fe8-904a-aaf872962431&invokeMethod=messageReceive&jsonData=data"; GetMethod method = new GetMethod(url); execute(method); } public static void get() { String url = "http://"+host+":8080/method/list"; GetMethod method = new GetMethod(url); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); execute(method); } private static void execute(HttpMethod method) { try { HttpClient client = new HttpClient(); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } byte[] responseBody = method.getResponseBody(); System.out.println(new String(responseBody)); } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // Release the connection. method.releaseConnection(); } } }
代码很简单,就不加注释和解释了。
相关推荐
httpclient使用教程
使用httpClient进行代理
httpclient使用详解共8页.pdf.zip
Http协议使用封装jar包(commons-codec-1.3.jar、commons-httpclient-3.1.jar、commons-logging-1.1.jar) 简单使用方法: public static void main(String[] args) { // String str1 = "...
URLConnection和HttpClient使用入门
httpClient 教程 使用事例 单向认证
httpclient是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,本文档提供使用httpclient的使用方法
HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。 适用人群:JavaWeb开发者 使用场景:在Java后端程序中,模拟浏览器端进行Web请求。 其它:...
Httpclient,一个我们平时开发时经常用到的组件
在.Net Core应用开发中,调用第三方接口也是常有的事情,HttpClient使用人数、使用频率算是最高的一种了,在.Net Core中,HttpClient的使用方式随着版本的升级也发生了一些变化,本次就讲解一下Asp.Net Core2.1前后...
完整的上传实例包括图片和参数配置,可以通过该样例进行扩展使用
NULL 博文链接:https://zhcheng.iteye.com/blog/1292350