精华内容
下载资源
问答
  • Java发送HTTP的POST请求

    2014-04-24 13:41:42
    Java发送HTTP的POST请求 /**  *HttpUtil.java  *下午05:26:36  */ package test;   import java.io.BufferedReader; import java.io.InputStreamReader; import java.io....

    Java发送HTTP的POST请求

    /**

     *HttpUtil.java

     *下午05:26:36

     */

    package test;

     

    import java.io.BufferedReader;

    import java.io.InputStreamReader;

    import java.io.OutputStreamWriter;

    import java.net.HttpURLConnection;

    import java.net.URL;

    import java.util.Map;

    import java.util.Map.Entry;

     

    /**

     * @author yoUng

     * @description 发送http请求

     * @filename HttpUtil.java

     * @time 2011-6-15 下午05:26:36

     * @version 1.0

     */

    public class HttpUtil {

     

    public static String http(String url, Map<String, String> params) {

    URL u = null;

    HttpURLConnection con = null;

    //构建请求参数

    StringBuffer sb = new StringBuffer();

    if(params!=null){

    for (Entry<String, String> e : params.entrySet()) {

    sb.append(e.getKey());

    sb.append("=");

    sb.append(e.getValue());

    sb.append("&");

    }

    sb.substring(0, sb.length() - 1);

    }

    System.out.println("send_url:"+url);

    System.out.println("send_data:"+sb.toString());

    //尝试发送请求

    try {

    u = new URL(url);

    con = (HttpURLConnection) u.openConnection();

    con.setRequestMethod("POST");

    con.setDoOutput(true);

    con.setDoInput(true);

    con.setUseCaches(false);

    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");

    osw.write(sb.toString());

    osw.flush();

    osw.close();

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    if (con != null) {

    con.disconnect();

    }

    }

     

    //读取返回内容

    StringBuffer buffer = new StringBuffer();

    try {

    BufferedReader br = new BufferedReader(new InputStreamReader(con

    .getInputStream(), "UTF-8"));

    String temp;

    while ((temp = br.readLine()) != null) {

    buffer.append(temp);

    buffer.append("\n");

    }

    } catch (Exception e) {

    e.printStackTrace();

    }

     

    return buffer.toString();

    }

     

    }


    展开全文
  • java发送http的post请求

    2021-02-25 13:58:42
    /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * 请求参数 * @return 所代表远程资源响应结果 */ public static String sendPost(String url, Map<String, ?> paramMap) { ...

    /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * 请求参数 * @return 所代表远程资源的响应结果 */ public static String sendPost(String url, Map<String, ?> paramMap) { PrintWriter out = null; BufferedReader in = null; String result = ""; String param = ""; Iterator<String> it = paramMap.keySet().iterator(); while (it.hasNext()) { String key = it.next(); param += key + "=" + paramMap.get(key) + "&"; } try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Accept-Charset", "utf-8"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println(e); } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; }

    展开全文
  • 基于commons-httpclient-3.1.jar封装HttpClientUtil工具类 package com.utils; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io....

    基于commons-httpclient-3.1.jar封装的HttpClientUtil工具类

    package com.utils;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpConnectionManager;
    import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
    import org.apache.commons.httpclient.methods.multipart.FilePart;
    import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
    import org.apache.commons.httpclient.methods.multipart.Part;
    import org.apache.commons.httpclient.methods.multipart.StringPart;
    import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
    import org.apache.commons.httpclient.params.HttpMethodParams;
    
    import com.base.utils.MD5;
    import com.base.utils.ParaMap;
    import com.base.web.AppConfig;
    
    public class HttpClientUtil
    {
        public static final String UTF8= "GBK";
    
        private static final String VE= "2";
    
        // 请求成功
        public static final int SUCCESS= 1;
        // 请求失败
        public static final int ERROR= 2;
        // 网络错误
        public static final int NET_ERROR= 3;
    
        // 读取超时  
        private final static int SOCKET_TIMEOUT = 20000;  
        // 连接超时  
        private final static int CONNECTION_TIMEOUT = 20000;  
        // 每个HOST的最大连接数量(连接至目的主机的线程数)  
        private final static int MAX_CONN_PRE_HOST = 5;  
        // 连接池的最大连接数(相当于线程数)  
        private final static int MAX_CONN = 5;  
        // 连接池  
        private final static HttpConnectionManager HTTPCONNECTIONMANAGER;  
        
        private final static HttpClient CLIENT;
        
        static {  
            HTTPCONNECTIONMANAGER = new MultiThreadedHttpConnectionManager();  
            HttpConnectionManagerParams params = HTTPCONNECTIONMANAGER.getParams();  
            params.setConnectionTimeout(CONNECTION_TIMEOUT);  
            params.setSoTimeout(SOCKET_TIMEOUT);  
            params.setDefaultMaxConnectionsPerHost(MAX_CONN_PRE_HOST);  
            params.setMaxTotalConnections(MAX_CONN);  
            CLIENT = new HttpClient(HTTPCONNECTIONMANAGER);  
        }  
        
        /**
         * 用HttpClient发送post请求
         * 
         * @param URL
         * @param inMap
         * @author lgr
         */
    
        public static ParaMap post(String URL, ParaMap inMap)
        {
            ParaMap out= new ParaMap();
            
            PostMethod post= new PostMethod(URL);
            post.setRequestHeader("Content-Type", "text/x-www-form-urlencoded");
            
            post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
            
            List<NameValuePair> params= new ArrayList<NameValuePair>();
            NameValuePair[] arr = {};
            
            params.add(new NameValuePair("sign", sign(inMap)));
            Set<Map.Entry<String, Object>> entrys= inMap.entrySet();
            for(Map.Entry<String, Object> entry: entrys)
            {
                params.add(new NameValuePair(String.valueOf(entry.getKey()), String.valueOf(entry.getValue())));
            }
            try
            {
                post.setRequestBody(params.toArray(arr));
                int status= CLIENT.executeMethod(post);
                String result= new String(post.getResponseBody(), UTF8);
                out.put("data", result);
            }
            catch(UnsupportedEncodingException e1)
            {
            }
            catch(IOException e)
            {
            }
            finally
            {
                post.releaseConnection();
            }
            return out;
        }
        
        /**
         * 用HttpClient发送post请求
         * 
         * @param URL
         * @param inMap
         * @author lgr
         * @throws FileNotFoundException 
         */
        
        @SuppressWarnings("unchecked")
        public static ParaMap postFile(String URL, ParaMap inMap) throws FileNotFoundException
        {
            ParaMap out= new ParaMap();
            PostMethod post= new PostMethod(URL);
            post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, UTF8);
            Part[] parts={};
            List<StringPart> params= new ArrayList<StringPart>();
            Set<Map.Entry<String, Object>> entrys= inMap.entrySet();
            for(Map.Entry<String, Object> entry: entrys) {
                params.add(new StringPart(String.valueOf(entry.getKey()), String.valueOf(entry.getValue())));
            }
            try{
                post.setRequestEntity(new MultipartRequestEntity(params.toArray(parts), post.getParams()));
                int status= CLIENT.executeMethod(post);
                String result= new String(post.getResponseBody(), UTF8);
                out.put("data", result);
            }
            catch(UnsupportedEncodingException e1)
            {
            }
            catch(IOException e)
            {
            }
            finally
            {
                post.releaseConnection();
            }
            return out;
        }
        
        /**
         * get请求
         * @param URL
         * @param inMap
         * @return
         * @author lgr
         */
        public static ParaMap get(String URL, ParaMap inMap)
        {
            ParaMap out= new ParaMap();
            
            URL = getFinalURL(URL, inMap);
            GetMethod get = new GetMethod(URL);
            
    //        get.getParams().setParameter(HttpMethodParams.HTTP_URI_CHARSET, "GBK");
            
            try
            {
                int status= CLIENT.executeMethod(get);
                String result= new String(get.getResponseBody(), UTF8);
                out.put("data", result);
            }
            catch(UnsupportedEncodingException e1)
            {
            }
            catch(IOException e)
            {
            }
            finally
            {
                get.releaseConnection();
            }
            return out;
        }
        
        private static String getFinalURL(String URL, ParaMap parameter){
            StringBuilder sb = new StringBuilder(URL);
            sb.append("?");
            sb.append("sign=" + sign(parameter));
            if(!parameter.isEmpty()){
                sb.append("&");
                Set<Map.Entry<String, Object>> entrys= parameter.entrySet();
                for(Map.Entry<String, Object> entry: entrys)
                {
                    sb.append(String.valueOf(entry.getKey()))
                        .append("=")
                        .append(String.valueOf(entry.getValue()))
                        .append("&");
                }
                return sb.substring(0, sb.length()-1);
            }else
            {
            	return sb.toString();
            }
                
        }
        
        private static String getFinalURLWithoutSign(String URL, ParaMap parameter){
            StringBuilder sb = new StringBuilder(URL);
            if(!parameter.isEmpty()){
                sb.append("?");
                Set<Map.Entry<String, Object>> entrys= parameter.entrySet();
                for(Map.Entry<String, Object> entry: entrys)
                {
                    sb.append(String.valueOf(entry.getKey()))
                        .append("=")
                        .append(String.valueOf(entry.getValue()))
                        .append("&");
                }
                return sb.substring(0, sb.length()-1);
            }else{
                return URL;
            }
        }
        
        /**
         * 签名
         * 
         * @param inMap
         * @return
         * @author lgr
         */
        public static String sign(ParaMap inMap)
        {
            inMap.put("ve", VE);
            Set<Map.Entry<String, Object>> entrys= inMap.entrySet();
            StringBuilder str= new StringBuilder();
            for(Map.Entry<String, Object> entry: entrys)
            {
                String key= String.valueOf(entry.getKey());
                if("sign".equals(key))
            	{
            		continue;
            	}
                if("requestKey".equals(key))
                {
                	continue;
                }
                String value= String.valueOf(entry.getValue());
                if(str.length()== 0)
                {
                	str.append(key+ "="+ value);
                }
                else
                {
                	str.append("&"+ key+ "="+ value);
                }
            }
            str.append("&requestKey="+ AppConfig.getStringPro("md5Key"));
            return MD5.MD5Encode(str.toString());
        }
    
        /**
         * 判断请求是否成功
         * 
         * @param inMap
         * @return
         * @author lgr
         */
        public static boolean isSuccess(ParaMap inMap)
        {
            return inMap.getInt("state")== SUCCESS;
        }
        
        /**
         * 用HttpClient发送post请求
         * 
         * @param URL
         * @param inMap
         * @author lgr
         * @throws FileNotFoundException 
         */
        
        @SuppressWarnings("unchecked")
        public static ParaMap postFile(String URL, ParaMap inMap,byte [] content) throws FileNotFoundException
        {
            ParaMap out= new ParaMap();
            PostMethod post= new PostMethod(URL);
            post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, UTF8);
            Part[] parts={};
            List<Object> params= new ArrayList<Object>();
            Set<Map.Entry<String, Object>> entrys= inMap.entrySet();
            for(Map.Entry<String, Object> entry: entrys) {
                params.add(new StringPart(String.valueOf(entry.getKey()), String.valueOf(entry.getValue())));
            }
            if(content.length>0){
                //如果有文件上传
                params.add(new FilePart("content", new ByteArrayPartSource("", content)));
            }
            try{
                post.setRequestEntity(new MultipartRequestEntity(params.toArray(parts), post.getParams()));
                int status= CLIENT.executeMethod(post);
                String result= new String(post.getResponseBody(), UTF8);
                out.put("data", result);
            }
            catch(UnsupportedEncodingException e1)
            {
            }
            catch(IOException e)
            {
            }
            finally
            {
                post.releaseConnection();
            }
            return out;
        }
        
    }
    
    
    展开全文
  • 工具类utils: package co.yixiang.utils; ... import java.io.*; import java.net.HttpURLConnection;...import java.net.URL;...import java.net.URLConnection;...import java.net.URLEncoder;...import java.util.

    工具类utils:

    package co.yixiang.utils;
    
    import com.alibaba.fastjson.JSONObject;
    
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @author xyp
     * @data 2020/7/3
     */
    public class HttpUtil {
        /**
         * 向指定URL发送GET方法的请求
         *
         * @param url
         *            发送请求的URL
         * @param param
         *            请求参数
         * @param authorization
         *            请求参数(允许为空)
         * @param cookie
         *            请求参数(允许为空)
         * @return URL 所代表远程资源的响应结果
         */
        public static String sendGet(String url, Map<String, String> param, String authorization, String cookie) {
            String result = "";
            BufferedReader in = null;
            try {
                StringBuffer query = new StringBuffer();
    
                for (Map.Entry<String, String> kv : param.entrySet()) {
                    query.append(URLEncoder.encode(kv.getKey(), "UTF-8") + "=");
                    query.append(URLEncoder.encode(kv.getValue(), "UTF-8") + "&");
                }
                if (query.lastIndexOf("&") > 0) {
                    query.deleteCharAt(query.length() - 1);
                }
    
                String urlNameString = url + "?" + query.toString();
                URL realUrl = new URL(urlNameString);
                // 打开和URL之间的连接
                URLConnection connection = realUrl.openConnection();
                // 设置通用的请求属性
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                if (authorization != null){
                    connection.setRequestProperty("Authorization", authorization);
                }
                if (cookie != null){
                    connection.setRequestProperty("Cookie", cookie);
                }
                // 建立实际的连接
                connection.connect();
                // 定义 BufferedReader输入流来读取URL的响应
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 使用finally块来关闭输入流
            finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            return result;
        }
    
        /**
         * 向指定 URL 发送POST方法的请求
         *
         * @param url
         *            发送请求的 URL
         * @param param
         *            请求参数
         * @param authorization
         *            请求参数(允许为空)
         * @param cookie
         *            请求参数(允许为空)
         * @return 所代表远程资源的响应结果
         */
        public static String sendPost(String url, Map<String, String> param, String authorization, String cookie) {
            PrintWriter out = null;
            BufferedReader in = null;
            String result = "";
            try {
                String para = "";
                for (String key : param.keySet()) {
                    para += (key + "=" + param.get(key) + "&");
                }
                if (para.lastIndexOf("&") > 0) {
                    para = new String(para.substring(0, para.length() - 1));
                }
                String urlNameString = url + "?" + para;
                URL realUrl = new URL(urlNameString);
                // 打开和URL之间的连接
                HttpURLConnection  conn = (HttpURLConnection)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.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                if (authorization != null){
                    conn.setRequestProperty("Authorization", authorization);
                }
                if (cookie != null){
                    conn.setRequestProperty("Cookie", cookie);
                }
                // 发送POST请求必须设置如下两行
                conn.setDoOutput(true);
                conn.setDoInput(true);
                // 获取URLConnection对象对应的输出流
                out = new PrintWriter(conn.getOutputStream());
                // 发送请求参数
                out.print(param);
                // flush输出流的缓冲
                out.flush();
                // 定义BufferedReader输入流来读取URL的响应
                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 使用finally块来关闭输出流、输入流
            finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            return result;
        }
    
        /**
         * 发送post请求
         * @param url
         *            发送请求的 URL
         * @param param
         *            请求参数
         * @param authorization
         *            请求参数(允许为空)
         * @param cookie
         *            请求参数(允许为空)
         * @return 所代表远程资源的响应结果
         */
        public static String sendPost(String url, String param, String authorization, String cookie) {
            PrintWriter out = null;
            BufferedReader in = null;
            String result = "";
            try {
                URL realUrl = new URL(url + "?" + param);
                // 打开和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.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                if (authorization != null){
                    conn.setRequestProperty("Authorization", authorization);
                }
                if (cookie != null){
                    conn.setRequestProperty("Cookie", cookie);
                }
                // 发送POST请求必须设置如下两行
                conn.setDoOutput(true);
                conn.setDoInput(true);
                // 获取URLConnection对象对应的输出流
                out = new PrintWriter(conn.getOutputStream());
                // 发送请求参数
                out.print(param);
    
                // flush输出流的缓冲
                out.flush();
                // 定义BufferedReader输入流来读取URL的响应
                in = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //使用finally块来关闭输出流、输入流
            finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            return result;
        }
    
        public static void main(String[] args) {
            //请求参数
            String body = "{\"status\":[1],\"customerid\":[\"20000047\"],\"sdorderno\":[1580459238218],\"total_fee\":[\"100.00\"],\"paytime\":[1580459238218],\"sign\":[\"a0397aef701f13e79133566582afa903\"]}";
            //请求头,可变,根据实际来
            String authorization = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZXN0IiwiYXV0aCI6ImliRXF1aXBtZW50TGlzdDplZGl0LGliQmFua0NhcmRMaXN0Omxpc3QsaWJCYW5rRmxvdzpsaXN0LGliRGlzYnVyc2VtZW50UmVjb3JkOmxpc3QsaWJCYW5rQ2FyZExpc3Q6YWRkLGliQmFua0NhcmRMaXN0OmVkaXQsaWJEaXNidXJzZW1lbnQ6YWRkLGliUGF5bWVudE9yZGVyOmRlbCxpYkJhbmtBbGFybTpkZWwsaWJFcXVpcG1lbnRBbGFybTplZGl0LHRlc3QsaWJCYW5rQWxhcm06ZWRpdCxpYkRpc2J1cnNlbWVudDpsaXN0LGliRXF1aXBtZW50QWxhcm06ZGVsLGliUGF5bWVudE9yZGVyOmxpc3QsaWJQYXltZW50T3JkZXI6YWRkLGliRXF1aXBtZW50TGlzdDpkZWwsaWJCYW5rQWxhcm06bGlzdCxpYkJhbmtDYXJkTGlzdDpkZWwsaWJFcXVpcG1lbnRMaXN0OmFkZCxpYk1lcmNoYW50SW5mb3JtYXRpb246bGlzdCxpYkVxdWlwbWVudEFsYXJtOmxpc3QsaWJFcXVpcG1lbnRMaXN0Omxpc3QsaWJEaXNidXJzZW1lbnQ6ZGVsLGliRGlzYnVyc2VtZW50OmVkaXQsaWJQYXltZW50T3JkZXI6ZWRpdCIsImV4cCI6MTU5Mzc4MzUwMn0.p_AEZfwErkRQCJ9ctZBNgf9fniLPGogV9rxSGyZEqStOOv7YEdAcefsAJNnUxzOOhLCdwhGBBAEDDVn3x43A3A";
            //cookie值,可变,根据实际来
            String cookie = "YSHOP-TOEKN=Bearer%20eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZXN0IiwiYXV0aCI6ImliRXF1aXBtZW50TGlzdDplZGl0LGliQmFua0NhcmRMaXN0Omxpc3QsaWJCYW5rRmxvdzpsaXN0LGliRGlzYnVyc2VtZW50UmVjb3JkOmxpc3QsaWJCYW5rQ2FyZExpc3Q6YWRkLGliQmFua0NhcmRMaXN0OmVkaXQsaWJEaXNidXJzZW1lbnQ6YWRkLGliUGF5bWVudE9yZGVyOmRlbCxpYkJhbmtBbGFybTpkZWwsaWJFcXVpcG1lbnRBbGFybTplZGl0LHRlc3QsaWJCYW5rQWxhcm06ZWRpdCxpYkRpc2J1cnNlbWVudDpsaXN0LGliRXF1aXBtZW50QWxhcm06ZGVsLGliUGF5bWVudE9yZGVyOmxpc3QsaWJQYXltZW50T3JkZXI6YWRkLGliRXF1aXBtZW50TGlzdDpkZWwsaWJCYW5rQWxhcm06bGlzdCxpYkJhbmtDYXJkTGlzdDpkZWwsaWJFcXVpcG1lbnRMaXN0OmFkZCxpYk1lcmNoYW50SW5mb3JtYXRpb246bGlzdCxpYkVxdWlwbWVudEFsYXJtOmxpc3QsaWJFcXVpcG1lbnRMaXN0Omxpc3QsaWJEaXNidXJzZW1lbnQ6ZGVsLGliRGlzYnVyc2VtZW50OmVkaXQsaWJQYXltZW50T3JkZXI6ZWRpdCIsImV4cCI6MTU5Mzc4MzUwMn0.p_AEZfwErkRQCJ9ctZBNgf9fniLPGogV9rxSGyZEqStOOv7YEdAcefsAJNnUxzOOhLCdwhGBBAEDDVn3x43A3A";
            String jsionResult=HttpUtil.sendPost("http://192.168.18.1:8013/api/callback",body,authorization,cookie);
            System.out.println(jsionResult);
        }
    }
    
    

    后台接受请求的Controller类里请求对应的方法:

    	/**
    	 * 测试方法
    	 * /
    	@PostMapping(value = "/callback")
        @Log("订单回调到下游")
        @ApiOperation("订单回调到下游")
        public ResponseEntity<Object> callback(@RequestBody Map<String,Object> callbackPama){
            return new ResponseEntity<>(ibBankOrderApiService.callback(callbackPama), HttpStatus.OK);
        }
    

    这种方式类似于用Java的方式模拟postman软件里的post请求:
    在这里插入图片描述

    展开全文
  • java编写了http Post的请求代码,通过发送请求的 URL,获取远程资源响应结果,入参为json字符串。使用到httpPost,CloseableHttpClient
  • java发送httpPost请求

    万次阅读 2019-04-01 15:12:43
    最近调用外部厂商提供的Post接口,总结了几种方法: 一、项目的maven依赖: <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId>...
  • 转自:java发送http的get、post请求 Http请求类 package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java...
  • 使用Java发送HTTP发送POST、GET请求在使用Java编程时候,经验要发送POST、GET请求,但有时候还经常忘记,那好吧做个记忆备份。 具体是POST还是GET只在setRequestMethod之间。public static String sendPost...
  • java发送http请求有很多种方式和写法,下面这种方式是使用java原生代码实现,以url传递参数方式 public JSONObject invoke(Map<String, Object> params) { try { apiUrl = propertyPlaceUtil....
  • Java通过http协议发送Get和Post请求

    万次阅读 多人点赞 2019-07-02 14:21:07
    Java通过http协议发送Get和Post请求 本文是基于Windows 10系统环境,利用MyEclipse构建一个java项目,并通过http协议发送Get和Post请求 Windows 10 MyEclipse 一、准备工作 (1) 下载需要用工具jar包 二、Post...
  • 【摘要】 ...最近项目里面需要用到Java发送http请求,由于发送https请求有点复杂,暂时不考虑 HttpURLConnection HttpURLConnection是一种多用途、轻量极HTTP客户端,使用它来进行HTTP操作可以适用于
  • 下面小编就为大家带来一篇java发送http的get、post请求实现代码。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
  • http请求在所有编程语言中几乎都是支持,我们常用两种为:GET,POST请求。一般情况下,发送一个GET请求都很简单,因为参数直接放在请求URL上,所以,对于PHP这种语言,甚至只需要一行:file_get_content(...
  • java发送http/https请求(get/post)代码,java 通过发送json,post请求,返回json数据方法 java 通过发送json,post请求,返回json数据方法
  • Java发送http的get post请求

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,548
精华内容 619
关键字:

java发送http的post请求

java 订阅