发送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编码

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/11173.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论