HttpURLConnection
String listPolicyApiUrl = "请求的api地址";
Map map = new HashMap();
map.put("chanelCode",channelId);
map.put("IsInsureArray",1);
map.put("appId",APPID);
String body = JSONObject.toJSONString(map);
OutputStreamWriter out = null;
BufferedReader br = null;
String result = "";
URL url = new URL(listPolicyApiUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
conn.setRequestProperty("eliteSign",sign);
conn.setDoOutput(true);
conn.setDoInput(true);
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(body);
out.flush();
InputStream is = conn.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String str = "";
while ((str = br.readLine()) != null){
result += str;
}
System.out.println("结果");
System.out.println(result);
is.close();
conn.disconnect();
HttpClient
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(listPolicyApiUrl);
JSONObject jsonParam = new JSONObject();
jsonParam.put("appId", 2);
jsonParam.put("IsInsureArray", 1);
jsonParam.put("chanelCode", 3);
httpPost.setHeader("eliteSign", sign);
httpPost.setHeader("Content-type", "application/json;charset=utf-8");
StringEntity s = new StringEntity(body, "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(s);
HttpResponse response = httpClient.execute(httpPost);
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 200) {
HttpEntity entity = response.getEntity();
InputStream input = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(input));
String str1 = br.readLine();
String result = new String(str1.getBytes("utf-8"), "utf-8");
System.out.println(result);
br.close();
input.close();
}
|