您的位置:首页 > 理论基础 > 计算机网络

HttpClient方式调用接口的实例

2017-05-27 15:00 260 查看
  使用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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: