java代码跨域通用方法
Posted bin93
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java代码跨域通用方法相关的知识,希望对你有一定的参考价值。
/**
* 通用的跨越调接口方法
* @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代码跨域通用方法的主要内容,如果未能解决你的问题,请参考以下文章