springMVC、httpClient调用别人提供的接口。
2017-06-19 19:24
302 查看
package utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class HttpClientUtil {
private static final String ITIL_URL = "172.20.32.32";
private static final int ITIL_PORT = 9999;
private static final String ITIL_PROTOCOL = "http";
@SuppressWarnings("deprecation")
public static String callingInterface(String jsonParams, String apiUrl) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
HttpHost targetHost = new HttpHost(ITIL_URL, ITIL_PORT, ITIL_PROTOCOL);
//认证提供者
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), null);
AuthCache authCache = new BasicAuthCache();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
String url = ITIL_PROTOCOL+"://"+ITIL_URL+":"+ITIL_PORT+apiUrl;
HttpPost httpPost = new HttpPost(url);
StringEntity stringEntity = new StringEntity(jsonParams, "UTF-8");
//设置请求参数
httpPost.setEntity(stringEntity);
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try {
HttpResponse response = httpclient.execute(targetHost, httpPost, context);
System.out.println("status="+response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() == 200){ //请求和响应都成功了
//调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
HttpEntity entity = response.getEntity();
reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String buffer = "";
while((buffer = reader.readLine()) != null){
sb.append(buffer);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(reader != null){
reader.close();
}
httpPost.releaseConnection();
httpclient.getConnectionManager().shutdown();
HttpClientUtils.closeQuietly(httpclient);
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class HttpClientUtil {
private static final String ITIL_URL = "172.20.32.32";
private static final int ITIL_PORT = 9999;
private static final String ITIL_PROTOCOL = "http";
@SuppressWarnings("deprecation")
public static String callingInterface(String jsonParams, String apiUrl) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
HttpHost targetHost = new HttpHost(ITIL_URL, ITIL_PORT, ITIL_PROTOCOL);
//认证提供者
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), null);
AuthCache authCache = new BasicAuthCache();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
String url = ITIL_PROTOCOL+"://"+ITIL_URL+":"+ITIL_PORT+apiUrl;
HttpPost httpPost = new HttpPost(url);
StringEntity stringEntity = new StringEntity(jsonParams, "UTF-8");
//设置请求参数
httpPost.setEntity(stringEntity);
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try {
HttpResponse response = httpclient.execute(targetHost, httpPost, context);
System.out.println("status="+response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() == 200){ //请求和响应都成功了
//调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
HttpEntity entity = response.getEntity();
reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String buffer = "";
while((buffer = reader.readLine()) != null){
sb.append(buffer);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(reader != null){
reader.close();
}
httpPost.releaseConnection();
httpclient.getConnectionManager().shutdown();
HttpClientUtils.closeQuietly(httpclient);
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
相关文章推荐
- springMVC、httpClient调用别人提供的接口!!!(外加定时调用)
- springMVC、https、GET调用别人提供的接口!!!
- 如何调用别人提供的接口
- SpringMVC 结合HttpClient调用第三方接口实现
- 如何调用别人提供的webservice接口
- 创建接口(接口,提供其他人进行操作的一个入口就是将我们原来实现功能的程序删了写成那个接口让别人来实现功能,为他人提供操作我们这儿的一个入口)
- APP server 使用httpclient调用第三方环信的server rest接口获取token
- httpclient调用cxf开发webservice接口
- java如何写接口给别人调用
- c# 【MVC】WebApi通过HttpClient来调用Web Api接口
- 转:java如何写接口给别人调用
- springMVC框架下如何实现移动端接口调用——流程简介篇
- UE4中调用DLL并提供蓝图接口
- Unity3D 调用Android的提供的接口
- java调用平台提供的接口进行图片识别
- 关于百度APIStore提供的接口调用失败问题
- java使用httpClient调用接口
- WebApi系列~通过HttpClient来调用Web Api接口