import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import net.sf.json.JSONObject;
/**
* 设备安装连接状态-远程接口调用
* Title: DeviceRemoteUtil
* Description:
* @author learnworm
* @date 2019年11月28日
*/
public class HttpClientUtil {
private static Logger log = LoggerFactory.getLogger(HttpClientUtil.class);
private static String DEFAULT_CHARSET = "UTF-8";
private static final String CONFIG_FILE_PATH = "config.properties";
public static final String SERVICE_MONITOR_ISRUNNING_URL_KEY = "SERVICE_MONITOR_ISRUNNING_URL";
public static void main(String[] args) {
Map<String,String> pmap = new HashMap<String,String>();
pmap.put("registryCode", "30101101012006040020");
pmap.put("pageNum", "1");
pmap.put("pageSize", "1");
Gson gson=new Gson();
//传入的参数
String param=gson.toJson(pmap);
String url="http://sss.com/lift/getCurAreaRunHistoryStatus";
JSONObject jsonObject = new JSONObject();
jsonObject=sendPostRequest(url,param);
//请求回来的数据
System.out.println(jsonObject.toString());
}
/**
* 发送默认GET方式http请求
* Title: httpsRequest
* Description:
* @param requestUrl
* @return
*/
public static JSONObject httpsRequest(String requestUrl) {
return httpsRequest(requestUrl,"GET",null);
}
/**
*
* Title: 发送Http请求
* @param requestUrl
* @param requestMethod
* @param outputStr
* @return
*/
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
try {
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
//设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);
//当outputStr不为null时,向输出流写出数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
//注意编码格式
outputStream.write(outputStr.getBytes(DEFAULT_CHARSET));
outputStream.close();
}
//从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, DEFAULT_CHARSET);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
//释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream = null;
conn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
log.error("连接超时:{}", ce);
} catch (Exception e) {
log.error("https请求异常:{}", e);
}
return jsonObject;
}
/**
* @desc: 发送post请求
* @param url
* @param param
* @return
* JSONObject
*/
public static JSONObject sendPostRequest(String url,String param){
HttpURLConnection httpURLConnection = null;
OutputStream out = null; //写
InputStream in = null; //读
int responseCode = 0; //远程主机响应的HTTP状态码
String result="";
try{
URL sendUrl = new URL(url);
httpURLConnection = (HttpURLConnection)sendUrl.openConnection();
//post方式请求
httpURLConnection.setRequestMethod("POST");
//设置头部信息
httpURLConnection.setRequestProperty("headerdata", "ceshiyongde");
//一定要设置 Content-Type 要不然服务端接收不到参数
httpURLConnection.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
//指示应用程序要将数据写入URL连接,其值默认为false(是否传参)
httpURLConnection.setDoOutput(true);
//httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setConnectTimeout(30000); //30秒连接超时
httpURLConnection.setReadTimeout(30000); //30秒读取超时
//获取输出流
out = httpURLConnection.getOutputStream();
//输出流里写入POST参数
out.write(param.getBytes());
out.flush();
out.close();
responseCode = httpURLConnection.getResponseCode();
BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"UTF-8"));
result =br.readLine();
}catch(Exception e) {
e.printStackTrace();
JSONObject.fromObject("{\"code\":500,\"message\":\"服务请求异常\"}");
}
return JSONObject.fromObject(result);
}
/**
* 读取属性文件配置
* Title: readProperty
* Description:
* @param key
* @return
*/
public static String readProperty(String key) {
Properties properties = new Properties();
InputStream inputstream = Object.class.getResourceAsStream(CONFIG_FILE_PATH);
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(inputstream, "utf-8");
properties.load(inputStreamReader);
return properties.get(key).toString();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
}