package com.thhy.screen.utils;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.github.pagehelper.PageInfo;
|
import com.thhy.general.common.BasicMessage;
|
import com.thhy.general.exception.BasicException;
|
|
import java.io.*;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.Map;
|
|
public class HttpUtils {
|
|
private static String BaseHost = "http://192.168.5.198:9199/api/";
|
|
private static String token = "";
|
|
public static String post(String apiPath, JSONObject paramJson, PageInfo pageInfo){
|
HttpURLConnection connection=null;
|
try {
|
URL url = new URL(BaseHost+apiPath);
|
connection = (HttpURLConnection) url.openConnection();
|
connection.setConnectTimeout(3000);
|
connection.setReadTimeout(3000);
|
connection.setRequestMethod("POST");
|
connection.setDoInput(true);
|
connection.setDoOutput(false);
|
connection.setRequestProperty("content-type","application/json;charset=UTF-8");
|
if(!apiPath.equals("/api/getToken")){
|
connection.setRequestProperty("userToken","");
|
}
|
/*if(pageInfo.isPage()){
|
connection.setRequestProperty("pageNum",pageInfo.getPageNo()+"");
|
connection.setRequestProperty("pageSize",pageInfo.getPageSize()+"");
|
}*/
|
connection.connect();
|
|
DataOutputStream dos=new DataOutputStream(connection.getOutputStream());
|
dos.write(paramJson.toJSONString().getBytes("UTF-8"));
|
//dos.writeBytes(paramJson.toJSONString());
|
int responseCode = connection.getResponseCode();
|
if (responseCode != HttpURLConnection.HTTP_OK) {
|
throw new BasicException(new BasicMessage("19901","网络连接失败"));
|
}
|
String result = getStringByStream(connection.getInputStream());
|
if (result == null) {
|
throw new BasicException(new BasicMessage("19901","网络连接失败"));
|
}else{
|
return result;
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new BasicException(new BasicMessage("19901","网络连接失败"));
|
}
|
}
|
|
public static String post(String apiPath, JSONObject paramJson){
|
HttpURLConnection connection=null;
|
try {
|
URL url = new URL(BaseHost+apiPath);
|
connection = (HttpURLConnection) url.openConnection();
|
connection.setConnectTimeout(3000);
|
connection.setReadTimeout(3000);
|
connection.setRequestMethod("POST");
|
connection.setDoInput(true);
|
connection.setDoOutput(true);
|
connection.setRequestProperty("content-type","application/json;charset=UTF-8");
|
if(!apiPath.equals("/api/getToken")){
|
connection.setRequestProperty("userToken","");
|
}
|
connection.connect();
|
|
DataOutputStream dos=new DataOutputStream(connection.getOutputStream());
|
dos.write(paramJson.toJSONString().getBytes("UTF-8"));
|
//dos.writeBytes(paramJson.toJSONString());
|
int responseCode = connection.getResponseCode();
|
if (responseCode != HttpURLConnection.HTTP_OK) {
|
throw new BasicException(new BasicMessage("19901","网络连接失败"));
|
}
|
String result = getStringByStream(connection.getInputStream());
|
if (result == null) {
|
throw new BasicException(new BasicMessage("19901","网络连接失败"));
|
}else{
|
return result;
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new BasicException(new BasicMessage("19901","网络连接失败"));
|
}
|
}
|
|
private static String getStringByStream(InputStream inputStream){
|
Reader reader;
|
try {
|
reader=new InputStreamReader(inputStream,"UTF-8");
|
char[] rawBuffer=new char[512];
|
StringBuffer buffer=new StringBuffer();
|
int length;
|
while ((length=reader.read(rawBuffer))!=-1){
|
buffer.append(rawBuffer,0,length);
|
}
|
return buffer.toString();
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
|
public static String get(String api, Map<String,Object> map){
|
HttpURLConnection connection = null;
|
try {
|
|
StringBuilder urlParam = new StringBuilder();
|
for(String key : map.keySet()){
|
urlParam.append(key+"="+map.get(key).toString()+"&");
|
}
|
String param = urlParam.toString();
|
if(param.contains("&")){
|
param = param.substring(0,param.length()-1);
|
}
|
URL url = new URL(BaseHost+api+"?"+param);
|
connection = (HttpURLConnection) url.openConnection();
|
connection.setConnectTimeout(3000);
|
connection.setReadTimeout(3000);
|
connection.setRequestMethod("GET");
|
if(!api.equals("/api/getToken")){
|
connection.setRequestProperty("authorization","");
|
}
|
|
//dos.writeBytes(paramJson.toJSONString());
|
int responseCode = connection.getResponseCode();
|
if (responseCode != HttpURLConnection.HTTP_OK) {
|
return null;
|
}
|
String result = getStringByStream(connection.getInputStream());
|
return result;
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
}
|