一、GET请求
public static String doGet(String url) { -> 参数拼接在URL中
String result = "";
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
}
httpResponse.close();
httpClient.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return result;
}
二、POST请求
public static String doPost(String url, String json) { -> 参数拼接在URL中
String result = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
try {
StringEntity stringEntity = new StringEntity(json, "utf-8"); -> JSON对象
stringEntity.setContentType("text/json");
Header header = new BasicHeader(HTTP.CONTENT_TYPE, "application/json");
stringEntity.setContentEncoding(header);
httpPost.setEntity(stringEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
response.close();
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@RequestMapping("/post")
@ResponseBody
public void post(HttpServletRequest request) { -> 获取StringEntity的参数
try{
Reader reader = new InputStreamReader(request.getInputStream());
BufferedReader bufferedReader = new BufferedReader(reader);
String line = null;
StringBuilder stringBuilder = new StringBuilder();
while((line = bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
String json = stringBuilder.toString();
JSONObject object = JSON.parseObject(json);
}catch (Exception e){
e.printStackTrace();
}
}
三、请求来源
public boolean JudgeIsMoblie(HttpServletRequest request) {
boolean moblie = false;
String[] agents = {"iphone", "android", "phone", "mobile", "wap", "netfront", "java",
"opera mobi", "opera mini", "ucweb", "windows ce", "symbian", "series", "webos",
"sony", "blackberry", "dopod", "nokia", "samsung", "palmsource", "pieplus",
"meizu", "midp", "cldc", "motorola", "foma", "docomo", "up.browser", "up.link",
"blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos", "techfaith",
"palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem",
"wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-",
"longcos", "pantech", "gionee", "xda", "xda-", "Googlebot-Mobile", "portalmmm",
"jig browser", "hiptop", "benq", "haier", "^lct", "320x320", "240x320", "176x220",
"w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac",
"blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno",
"ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-",
"maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-",
"newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port", "prox",
"qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar",
"sie-", "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-",
"tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp",
"wapr", "webc", "winw"};
if (request.getHeader("User-Agent") != null) {
for (String agent : agents) {
if (request.getHeader("User-Agent").toLowerCase().contains(agent)) {
moblie = true;
break;
}
}
}
return moblie;
}
网友评论