• 发送Post的请求代码


    通过浏览器访问的URL请求,都是GET请求,接下来代码是模拟POST发送请求

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class HttpMethod {
        
        public static String executeRule(String url,String id,String analyzeModelType,String paramJson){
            String params =generateParams(id,analyzeModelType,paramJson);
            //System.out.println(params);
            try {
                sendPost(url,params);
                return "Execute Sucessfully!";
            } catch (Exception e) {
                e.printStackTrace();
                return "Execute Failed!";
            }
        }
        
        /**
         * 获取和组装参数
         */
        private static String generateParams(String id, String analyzeModelType, String paramJson){
            StringBuffer sb =new StringBuffer();
            sb.append("id=");
            sb.append(id);
            sb.append("&analyzeModelType=");
            sb.append(analyzeModelType);
            sb.append("&paramJson=");
            sb.append(paramJson);
            return sb.toString();
        }
        
        private static String sendPost(String url, String param) {
            PrintWriter out = null;
            BufferedReader in = null;
            String result = "";
            try {
                URL realUrl = new URL(url);
                URLConnection conn = realUrl.openConnection();
                // 设置通用的请求属性
                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.print(param);
                out.flush();
                try {
                    in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                } catch (FileNotFoundException exception) {
                    java.io.InputStream err = ((HttpURLConnection) conn).getErrorStream();
                    if (err == null) throw exception;
                    in = new BufferedReader(new InputStreamReader(err));
                }
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            } catch (Exception e) {
                System.out.println("发送 POST 请求出现异常!"+e);
                e.printStackTrace();
            }
            finally{
                try{
                    if(out!=null){
                        out.close();
                    }
                    if(in!=null){
                        in.close();
                    }
                }
                catch(IOException ex){
                    ex.printStackTrace();
                }
            }
            return result;
        }  
        
    }

    //调用代码
    HttpMethod.executeRule(Constant.ANALYZEURL, id, analyzeModelType, URLEncoder.encode(paramJson,"UTF-8"));
    //若果参数中有json串,则需要将其用URLEncoder编码
  • 相关阅读:
    20100822 动漫店 员工卡缓存出现问题。
    性能报告——使用AOP与DYNAMICProxy的orm性能测试
    性能报告——反射创建对象
    抓虾 老板体会。
    Pixysoft.framework.configuration 性能提升报告
    今天你抛弃了ASP.NET了吗?问题篇
    20100922 项目重构计划
    经过一年时间的沉淀 再次回首 TCP Socket服务器编程
    对Google Me的社会化网络 “The Real Life Social Network” 翻译
    20100718 项目重构计划
  • 原文地址:https://www.cnblogs.com/atomicbomb/p/6678002.html
Copyright © 2020-2023  润新知