• java HTTP请求工具


    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);
            
        }
    
    }
  • 相关阅读:
    PIE SDK介绍
    PIE软件介绍
    PIE SDK与Python结合说明文档
    转载博客(Django2.0集成xadmin管理后台遇到的错误)
    python+django学习二
    python+django学习一
    HTML练习二--动态加载轮播图片
    HTML练习一
    LeetCode 999. 车的可用捕获量
    LeetCode 892. 三维形体的表面积
  • 原文地址:https://www.cnblogs.com/thiaoqueen/p/6840773.html
Copyright © 2020-2023  润新知