java 调用接口的get方法//接口调用 public String get(String httpUrl) { // httpurl为所调用接口的url String result = ""; try { URL url = new URL(httpUrl.toString()); StringBuffer document = new StringBuffer();......
java 调用接口的get方法
//接口调用
public String get(String httpUrl) { // httpurl为所调用接口的url
String result = "";
try {
URL url = new URL(httpUrl.toString());
StringBuffer document = new StringBuffer();
HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
httpconn.setRequestProperty("accept", "*/*");
httpconn.setRequestProperty("connection", "Keep-Alive");
httpconn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
//建立实际的连接
httpconn.connect();
InputStream input = null;
try {
httpconn.connect();
input = httpconn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String Result = "";
while ((Result = reader.readLine()) != null) {
document.append(Result);
}
result = document.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
|
本文地址:https://blog.csdn.net/jiandan7410/article/details/107493369