转自 https://blog.csdn.net/ls_man/article/details/78133950
-
/**
-
* http调用
-
*
-
* @param httpUrl :请求接口
-
* @param httpArg :参数
-
* @return 返回结果
-
*/
-
public static String httpRequest(String httpUrl, String httpArg) {
-
BufferedReader reader =
null;
-
String result =
null;
-
StringBuffer sbf =
new StringBuffer();
-
httpUrl = httpUrl +
"?" + httpArg;
-
-
try {
-
URL url =
new URL(httpUrl);
-
HttpURLConnection connection = (HttpURLConnection) url
-
.openConnection();
-
connection.setConnectTimeout(
3000);
//超时时间,单位毫秒,3000是3秒
-
connection.setReadTimeout(
3000);
-
connection.setRequestMethod(
"GET");
-
// 填入apikey到HTTP header(百度接口)
-
//connection.setRequestProperty("apikey", "11111");
-
connection.connect();
-
InputStream is = connection.getInputStream();
-
reader =
new BufferedReader(
new InputStreamReader(is,
"UTF-8"));
//UTF-8
-
String strRead =
null;
-
while ((strRead = reader.readLine()) !=
null) {
-
sbf.append(strRead);
-
sbf.append(
"\r\n");
-
}
-
reader.close();
-
result = sbf.toString();
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
return result;
-
}