场景:后台Http请求其它接口获取token
GET请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| private String HttpGetUrlJump(String getUrl, Map<String, Object> HeaderParams, Map<String, Object> params) { String result = ""; try { if (params != null) { Iterator<String> it = params.keySet().iterator(); StringBuffer sb = null; while (it.hasNext()) { String key = it.next(); String value = (String) params.get(key); if (sb == null) { sb = new StringBuffer(); sb.append("?"); } else { sb.append("&"); } sb.append(key); sb.append("="); sb.append(value); } getUrl += sb.toString(); }
URL url = new URL(getUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setRequestMethod("GET"); connection.setUseCaches(false); connection.setConnectTimeout(60000); connection.setReadTimeout(60000); connection.setConnectTimeout(60000); connection.setInstanceFollowRedirects(false);
if (HeaderParams.size() > 0) { Set<String> set = HeaderParams.keySet(); for (String str : set) { connection.setRequestProperty(str, (String) HeaderParams.get(str)); } } connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { result += line; } reader.close(); connection.disconnect(); return result; } catch (Exception e) { e.printStackTrace(); return result; } }
|
上面拼url方法来自网络,非常巧妙,使用一个外接变量,初始设为null判断为null为第一次的方式巧妙地确定了第一次———————good,good,good,机智,机智,机智
POST请求
需要传入url,请求头header,请求body信息(都以map的形式传入)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| private String HttpPostUrlJump(String postUrl, Map<String,Object> HeaderParams, Map<String,Object> params){ String result=""; try { URL url = new URL(postUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setConnectTimeout(60000); connection.setReadTimeout(60000); connection.setConnectTimeout(60000); connection.setInstanceFollowRedirects(false); connection.setRequestProperty("Content-Type","application/json");
if(HeaderParams.size()>0){ Set<String> set = HeaderParams.keySet(); for (String str : set) { connection.setRequestProperty(str, (String) HeaderParams.get(str)); } }
connection.connect(); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8"); if(params.size()>0){ JSONObject json = new JSONObject(params); writer.write(String.valueOf(json)); }else{ writer.write("{}"); } writer.flush(); writer.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null){ result += line; } reader.close(); connection.disconnect(); return result; } catch (Exception e) { e.printStackTrace(); return result; }
|
注:其中有些值还是写死,具体情况具体修改
Http的两种提交方式跳转:GET,POST比较