java代码跨域通用方法
2021-06-17 01:04
标签:utf-8 write int agent lan new connect ext disco java代码跨域通用方法 标签:utf-8 write int agent lan new connect ext disco 原文地址:https://www.cnblogs.com/bin93/p/9722317.html/**
* 通用的跨越调接口方法
* @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;
}