使用HttpClient的方式调用接口的实例。
public class TestHttpClient { public static void main(String[] args) {
// 请求接口地址
String url = "";
// 请求参数
String userid = ""; HttpClient httpclient = null;
PostMethod post = null;
try {
//创建连接
httpclient = new HttpClient();
post = new PostMethod(url);
// 设置编码方式
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
// 添加参数
post.addParameter("userid", userid);
// 执行请求
httpclient.executeMethod(post);
// 接口返回信息
String info = new String(post.getResponseBody(), "UTF-8");
System.out.println(info);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
post.releaseConnection();
((SimpleHttpConnectionManager) httpclient.getHttpConnectionManager()).shutdown();
}
}
}