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编码
  • 相关阅读:
    wpf之依赖属性
    wpf之布局控件
    WPF之绑定
    wpf之触发器
    wpf之样式
    wpf之TreeView
    wpf(五)
    【Javaweb】poi实现通过上传excel表格批量导入数据到数据库
    Java读取批量Excel文件
    Centos上通过yum命令删除有关MySQL
  • 原文地址:https://www.cnblogs.com/atomicbomb/p/6678002.html
Copyright © 2011-2022 走看看