目录
3.将sendMap,sendUrl 传入到 doPost() 方法中
注:本文中的 doPost() / doGet() 方法可以直接复制使用,按需传入对应参数即可。
一. Post发送方式
1. 封装请求参数:
{
"key ":" 123",
"appId":"0a42b68f-edca-489a-b2b8",
"pch":"88",
}
将json数据封装成sendMap对象
Map<String, Object> sendMap = new HashMap<>(); sendMap.put("key", "123"); sendMap.put("appId","0a42b68f-edca-489a-b2b8"); sendMap.put("pch","88");
2. 填写发送路径:
sendUrl: http://127.0.0.1:8080/send/sendApp
3.将sendMap,sendUrl 传入到 doPost() 方法中
/** * 发送post请求 * @param sendUrl 发送路径 * @param sendMap 发送参数 * @return StringBuilder */ public StringBuilder doPost(String sendUrl,Map<String, Object> sendMap){ StringBuilder response = new StringBuilder(); try { URL url = new URL(sendUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); connection.setDoInput(true); JSONObject jsonObject = new JSONObject(sendMap); String s1 = jsonObject.toString(); byte[] bytes = s1.getBytes(); DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.write(bytes); wr.close(); int statusCode = connection.getResponseCode(); InputStream inputStream = statusCode == 200 ? connection.getInputStream() : connection.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); inputStream.close(); connection.disconnect(); } catch (Exception e) { log.error("发送post请求异常:",e); e.printStackTrace(); } return response; }
4. 解析stringBulider返回值
(a. json格式返回值方式 )
返回值以json形式举例 : {"code":"200","data":"123"},转成JSONObject方式,方便用key-value形式获取
JSONObject resultObject = JSONObject.parseObject(stringBulider.toString());
resultObject.get("code");
resultObject.get("data");
(b. 直接返回状态码方式:比如200)
String code = stringBulider.toString 即可。
二. Get发送方式
1.将所有参数封装在sendUrl路径上
sendUrl : http://127.0.0.1:8080/send/sendApp?username=aa&password=bb&id=abc
2. 将sendUrl参数传入doGet() 方法
/** * 发送post请求 * @param sendUrl 发送路径 * @return stringBuffer */ public StringBuffer doGet(String sendUrl){ StringBuffer stringBuffer = new StringBuffer(); try{ URL url = new URL(sendUrl); HttpURLConnection huc = (HttpURLConnection) url.openConnection(); huc.setRequestMethod("GET"); huc.setRequestProperty("Accept", "*/*"); huc.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0"); huc.setRequestProperty("Content-Language", "en-US"); huc.setRequestProperty("Accept-Language","zh-CN"); huc.setRequestProperty("Content-type","text/html"); huc.connect(); InputStreamReader ir = new InputStreamReader(huc.getInputStream()); BufferedReader br = new BufferedReader(ir) ; String line; while ((line = br.readLine()) != null) { stringBuffer.append(line); } br.close(); ir.close(); huc.disconnect(); } catch (Exception e) { log.error("发送Get请求异常:",e); e.printStackTrace(); } return stringBuffer; }
3. 解析stringBuffer返回值
(a. json格式返回值方式 )
返回值以json形式举例 : {"code":"200","data":"123"},转成JSONObject方式,方便用key-value形式获取
JSONObject resultObject = JSONObject.parseObject(stringBuffer.toString());
resultObject.get("code");
resultObject.get("data");
(b. 直接返回状态码方式:比如200)
String code = stringBulider.toString 即可。