package HttpRequestTest; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.SocketException; import java.net.URL; import java.net.URLEncoder; public class HttpRequestUtil { /** * @param urlAll * :请求接口 * @param httpArg * :参数 * @return 返回结果 */ public static String request(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 = null; connection = (HttpURLConnection) url.openConnection();// 正常访问 connection.setConnectTimeout(5000); connection.setRequestMethod("GET"); connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append(" "); } reader.close(); result = sbf.toString(); } catch (SocketException e) { System.out.println("Connection timed out: connect"); } catch (Exception e) { e.printStackTrace(); } System.out.println("result:" + result); return result; } public static void main(String[] args) { String httpUrl = "http://192.168.100.47:8081/scooper-core-rest/data/system/authManage/getAccountStatus"; String messageCnt = "你好,这是一条测试短信"; String messageContent = URLEncoder.encode(messageCnt,"GBK");//message 以GBK编码 String httpArg = "message="+messageContent; HttpRequestUtil.request(httpUrl, httpArg); } }