/** * 通用的跨越调接口方法 * @param path 请求的url路径 * @return */public static String getHttpResponse(String path) { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); PrintWriter out = null; conn.setRequestMethod("POST"); conn.setRequestProperty("Accept-Language", UserContext.getLanguage()); 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.setDoOutput(true); conn.setDoInput(true); out = new PrintWriter(conn.getOutputStream()); out.flush(); InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); String responseString = ""; String str = ""; while ((str = br.readLine()) != null) { responseString += str; } is.close(); conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } return responseString;}
java代码跨域通用方法
点赞
收藏