zoukankan      html  css  js  c++  java
  • 发送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编码
  • 相关阅读:
    windows10关闭更新,windowsUpdate禁用无效 windows无限重启 一分钟无限重启 win10无法连接到SENS服务
    Visual Studio项目/解决方案重命名
    关于Geometry中的一些简单形状
    无法打开http://localhost:6080/arcgis/manager/
    centOS无法联网
    FTP服务器搭建
    iFrame中dateGrid中数据不显示
    关于python的基础知识
    python中int str bool list dict数据操作方法汇总
    关于int str bool的讨论
  • 原文地址:https://www.cnblogs.com/atomicbomb/p/6678002.html
Copyright © 2011-2022 走看看