GET请求:
public void testGetAnotherSystem(){
String urlAddress = "http://localhost:8091/api/reservoir/getAllWarehouse";
try {
URL url = new URL(urlAddress);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("encoding","UTF-8");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("GET");
//connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
//注意GET请求参数是直接写在请求URL里的,因此设置content-type也没用
OutputStream outputStream = connection.getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
String params = "warehouseBH=B"; //a=1&b=2
bufferedWriter.write(params);
bufferedWriter.flush();
InputStream inputStream = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
StringBuilder stringBuilder = new StringBuilder();
while((line=bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
String result = stringBuilder.toString();
JSONObject jsonObject = (JSONObject) JSON.parse(result);
JSONObject data = jsonObject.getJSONObject("data");
Map<String,Object> map = JSONObject.toJavaObject(data,Map.class);
for(Map.Entry<String,Object> entry:map.entrySet()){
System.out.println(entry.getKey()+"-"+entry.getValue());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
POST请求:
public void testPostAnotherSystem(){
String urlAddress = "http://localhost:8080/getData";
try {
URL url = new URL(urlAddress);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("encoding","UTF-8");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
//注意请求类型应与接口要求参数类型相对应
//普通参数类型(@RequestParm String str) application/x-www-form-urlencoded
//对象类型(@RequestBody Object obj) application/json
//上传文件(@RequestParam MultipartFile file) multipart/form-data
OutputStream outputStream = connection.getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
String content = "str="+ URLEncoder.encode("hahaha","UTF-8");
bufferedWriter.write(content);
bufferedWriter.flush();
InputStream inputStream = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
StringBuilder stringBuilder = new StringBuilder();
while((line=bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
String result = stringBuilder.toString();
System.out.println(result);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
有关JSON数据解析用到 fastjson依赖包。