小编在这里讲述在java中两种常见调用接口的方式,同时提供多种类型选择
/**
* HttpURLConnection 适用于安卓,在java中io流容易报错
* path (路径)
* post (json.tostring 数据源)
* post提交方式
* POST请求获取数据
*/
public static String postDownloadJson(String path,String post){
URL url = null;
try {
url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
httpURLConnection.setReadTimeout(2000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
printWriter.write(post);//post的参数 xx=xx&yy=yy
// flush输出流的缓冲
printWriter.flush();
System.out.println(httpURLConnection.getResponseCode());
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
byte[] arr = new byte[1024];
while((len=bis.read(arr))!= -1){
bos.write(arr,0,len);
bos.flush();
}
bos.close();
return bos.toString("utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* HttpURLConnection 适用于安卓,在java中io流容易报错
* path (路径)
* post (json.tostring 数据源)
* header(装载头部信息)
* POST请求获取数据
*/
public static String postDownloadJsonHeader(String path,String post,String header){
URL url = null;
try {
url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
httpURLConnection.setRequestProperty("Content-Type", "application/json");
// httpURLConnection.setRequestProperty("Authorization", header);
// conn.setConnectTimeout(10000);//连接超时 单位毫秒
// conn.setReadTimeout(2000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
printWriter.write(post);//post的参数 xx=xx&yy=yy
// flush输出流的缓冲
printWriter.flush();
System.out.println(httpURLConnection.getResponseCode());
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
byte[] arr = new byte[1024];
while((len=bis.read(arr))!= -1){
bos.write(arr,0,len);
bos.flush();
}
bos.close();
return bos.toString("utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* path 路径
* get 提交方式
* Authorization 传头部信息
*/
public static String memberCodeClientget(String path,String Authorization) {
CloseableHttpClient httpCilent2 = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000) //设置连接超时时间
.setConnectionRequestTimeout(5000) // 设置请求超时时间
.setSocketTimeout(5000)
.setRedirectsEnabled(true)//默认允许自动重定向
.build();
HttpGet httpGet2 = new HttpGet(path);
httpGet2.addHeader("Content-Type","application/json");
httpGet2.setHeader("Authorization", Authorization);
httpGet2.setConfig(requestConfig);
String srtResult = "";
try {
HttpResponse httpResponse = httpCilent2.execute(httpGet2);
System.out.println(httpResponse.getStatusLine().getStatusCode());
String outstr = new String(EntityUtils.toString(httpResponse.getEntity()).getBytes("ISO-8859-1"),"UTF-8");
srtResult = outstr;
return srtResult;
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpCilent2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return srtResult;
}
/*
* path 路径
* post提交方式
* Authorization 传头部信息
*/
public static String doPost(String url,String jsonstr,String charset){
CloseableHttpClient httpCilent =null ;
HttpPost httpPost = null;
String result = null;
try{
httpCilent = HttpClients.createDefault();
httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
StringEntity se = new StringEntity(jsonstr);
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(se);
HttpResponse response = httpCilent.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
/*
* path 路径
* post提交方式
* Header 传头部信息
* charset UTF-8
*/
public static String doPostaddHeader(String url,String jsonstr,String Header,String charset){
CloseableHttpClient httpCilent =null ;
HttpPost httpPost = null;
String result = null;
try{
httpCilent = HttpClients.createDefault();
httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
httpPost.setHeader("Authorization",Header);
StringEntity se = new StringEntity(jsonstr);
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(se);
HttpResponse response = httpCilent.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
这里提供httpCilent 需要的两个jar包 百度云免费下载
链接:https://pan.baidu.com/s/1hqW5Lfbj-yqg405vFC-Omw 密码:ka5k