Java代码:调用外部接口(使用Json格式传递参数)的方法
/guoshijie8023/article/details/81634278
代码如下:
String url="所给外部接口的url";
//创建连接对象
HttpClient httpClient = new HttpClient();
//创建请求
PostMethod method = new PostMethod(url);
//设置请求头格式为json格式
RequestEntity entity=new StringRequestEntity(json,"application/json","UTF-8");
//设置请求体信息
method.setRequestEntity(entity);
//设置请求头信息
method.setRequestHeader("APPKEY", "C6C6E17AC153ED8DF8D61207");
//创建连接
httpClient.executeMethod(method);
//获取返回信息
InputStream in = method.getResponseBodyAsStream();
InputStreamReader isr = new InputStreamReader(in, "UTF-8");
char[] b = new char[4096];
for(int n; (n = isr.read(b)) != -1;) {
sb.append(new String(b, 0, n));
}
String returnStr = sb.toString();