import com.alibaba.fastjson.JSON;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* @Author : JCccc
* @CreateTime : 2019/5/11
* @Description :
**/
public class HttpURLConnectionUtil {
/**
* get請求
*
* @param path
* @param param
* @return
*/
public static String get(String path,Map<String, Object> param) {
try {
if (param != null) {
StringBuffer paramBuffer = new StringBuffer();
int i = 0;
for (String key : param.keySet()) {
if (i == 0)
paramBuffer.append("?");
else
paramBuffer.append("&");
paramBuffer.append(key).append("=").append(param.get(key));
i++;
}
path += paramBuffer;
}
URL url = new URL(path); // 把字元串轉換為URL請求位址
HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打開連接配接
connection.connect();// 連接配接會話
// 擷取輸入流
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {// 循環讀取流
sb.append(line);
}
br.close();// 關閉流
connection.disconnect();// 斷開連接配接
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
System.out.println("失敗!");
return null;
}
}
/**
* post 請求
*
* @param path
* @param jsonStr
* @return
*/
public static String post(String path,String jsonStr) {
try {
URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 送出模式
httpURLConnection.setConnectTimeout(10000);//連接配接逾時 機關毫秒
httpURLConnection.setReadTimeout(10000);//讀取逾時 機關毫秒
// 發送POST請求必須設定如下兩行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
// PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// printWriter.write(postContent);
// printWriter.flush();
httpURLConnection.connect();
OutputStream os=httpURLConnection.getOutputStream();
os.write(jsonStr.getBytes("UTF-8"));
os.flush();
StringBuilder sb = new StringBuilder();
int httpRspCode = httpURLConnection.getResponseCode();
if (httpRspCode == HttpURLConnection.HTTP_OK) {
// 開始擷取資料
BufferedReader br = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
Map<String,Object> map=new HashMap<>();
map.put("A","a");
map.put("B",100);
String getResult = get("http://192.xxx.xx.xx:8665/testGet", map);
System.out.println(getResult);
String postResult = post("http://192.xxx.xx.xx:8665/testPost", JSON.toJSONString(map));
System.out.println(postResult);
}
}