如何使用HttpClient进行高效接口调用?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1230个文字,预计阅读时间需要5分钟。
javaimport java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;
package com.eshore.expert.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.eshore.expert.vo.ResultExper;
/**
* HTTP Client ��ʵ��
* @author eric
*
*/
public class Httplient {
/**
* 对象转json
* @param obj
* @return
*/
public static String Object2Json(Object obj){
JSONObject json = JSONObject.fromObject(obj);//将java对象转换为json对象
String str = json.toString();//将json对象转换为字符串
return str;
}
/**
* 对象转json 数组 ["":""]
* @param obj
* @return
*/
public static String objecttoJson(Object obj){
JSONArray array=JSONArray.fromObject(obj);
String strArray=array.toString();
return strArray;
}
/**
* 发送请求(专家抽取)
* @param url 请求路径
* @param token 密匙 (可为空)
* @param data 请求数据(json)
* @return 返回true或者false
*/
public static Map ExperExtraction(String url,String token,String data) throws KeyManagementException, NoSuchAlgorithmException, IOException{
Map
package com.eshore.expert.utils; import java.io.InputStream; public interface HTTPResponse { int getStatus(); InputStream getInputStream(); void close(); } package com.eshore.expert.utils; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; public class Response implements HTTPResponse { InputStream in ; int status=200; HttpURLConnection conn; public Response(int status ,InputStream in,HttpURLConnection conn){ this.in=in; this.status=status; this.conn=conn; } @Override public int getStatus() { // TODO Auto-generated method stub return status; } @Override public InputStream getInputStream() { // TODO Auto-generated method stub return in; } @Override public void close() { // TODO Auto-generated method stub conn.disconnect(); } } 调用函数发送请求并接受实体数据
String object2Json = Httplient.Object2Json(queryProect); //对象转json
logger.info("发送去门禁系统" + object2Json);
Boolean experExtraction = this.ExperExtraction(url, object2Json); //调用函数发送请求
/**
* url 地址
* data 为json数据
*/
public Boolean ExperExtraction(String url, String data)
throws KeyManagementException, NoSuchAlgorithmException,
IOException {
Boolean flag = false;
Map
本文共计1230个文字,预计阅读时间需要5分钟。
javaimport java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;
package com.eshore.expert.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.eshore.expert.vo.ResultExper;
/**
* HTTP Client ��ʵ��
* @author eric
*
*/
public class Httplient {
/**
* 对象转json
* @param obj
* @return
*/
public static String Object2Json(Object obj){
JSONObject json = JSONObject.fromObject(obj);//将java对象转换为json对象
String str = json.toString();//将json对象转换为字符串
return str;
}
/**
* 对象转json 数组 ["":""]
* @param obj
* @return
*/
public static String objecttoJson(Object obj){
JSONArray array=JSONArray.fromObject(obj);
String strArray=array.toString();
return strArray;
}
/**
* 发送请求(专家抽取)
* @param url 请求路径
* @param token 密匙 (可为空)
* @param data 请求数据(json)
* @return 返回true或者false
*/
public static Map ExperExtraction(String url,String token,String data) throws KeyManagementException, NoSuchAlgorithmException, IOException{
Map
package com.eshore.expert.utils; import java.io.InputStream; public interface HTTPResponse { int getStatus(); InputStream getInputStream(); void close(); } package com.eshore.expert.utils; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; public class Response implements HTTPResponse { InputStream in ; int status=200; HttpURLConnection conn; public Response(int status ,InputStream in,HttpURLConnection conn){ this.in=in; this.status=status; this.conn=conn; } @Override public int getStatus() { // TODO Auto-generated method stub return status; } @Override public InputStream getInputStream() { // TODO Auto-generated method stub return in; } @Override public void close() { // TODO Auto-generated method stub conn.disconnect(); } } 调用函数发送请求并接受实体数据
String object2Json = Httplient.Object2Json(queryProect); //对象转json
logger.info("发送去门禁系统" + object2Json);
Boolean experExtraction = this.ExperExtraction(url, object2Json); //调用函数发送请求
/**
* url 地址
* data 为json数据
*/
public Boolean ExperExtraction(String url, String data)
throws KeyManagementException, NoSuchAlgorithmException,
IOException {
Boolean flag = false;
Map

