zoukankan      html  css  js  c++  java
  • 极光客户互动云java post请求

    package com.common.utils;
    
    import java.io.IOException;
    import java.net.URLDecoder;
    import java.util.Map;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    
    import com.alibaba.fastjson.JSONObject;
    
    public class HttpRequestUtils {
    //    private static Logger logger = LoggerFactory.getLogger(HttpRequestUtils.class);    //日志记录
         
        /**
         * httpPost
         * @param url  路径
         * @param jsonParam 参数
         * @return
         */
        public static JSONObject httpPost(String url,JSONObject jsonParam){
            return httpPost(url, jsonParam, false);
        }
        
        /**
         * httpPost
         * @param url  路径
         * @param jsonParam 参数
         * @return
         */
        public static JSONObject httpPost(String url,JSONObject jsonParam, JSONObject header){
            return httpPost(url, jsonParam, header, false);
        }
     
        /**
         * post请求
         * @param url         url地址
         * @param jsonParam     参数
         * @param noNeedResponse    不需要返回结果
         * @return
         */
        public static JSONObject httpPost(String url,JSONObject jsonParam, boolean noNeedResponse){
            //post请求返回结果
            HttpClient httpClient = HttpClientBuilder.create().build();
            JSONObject jsonResult = null;
            HttpPost method = new HttpPost(url);
            try {
                if (null != jsonParam) {
                    //解决中文乱码问题
                    StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
                    entity.setContentEncoding("UTF-8");
                    entity.setContentType("application/json");
                    method.setEntity(entity);
                }
                HttpResponse result = httpClient.execute(method);
                url = URLDecoder.decode(url, "UTF-8");
                /**请求发送成功,并得到响应**/
                if (result.getStatusLine().getStatusCode() == 200) {
                    String str = "";
                    try {
                        /**读取服务器返回过来的json字符串数据**/
                        str = EntityUtils.toString(result.getEntity());
                        if (noNeedResponse) {
                            return null;
                        }
                        /**把json字符串转换成json对象**/
                        jsonResult = JSONObject.parseObject(str);
                    } catch (Exception e) {
    //                    logger.error("post请求提交失败:" + url, e);
                    }
                }
            } catch (IOException e) {
    //            logger.error("post请求提交失败:" + url, e);
            }
            return jsonResult;
        }
        
        /**
         * post请求
         * @param url         url地址
         * @param jsonParam     参数
         * @param noNeedResponse    不需要返回结果
         * @return
         */
        public static JSONObject httpPost(String url,JSONObject jsonParam, JSONObject header, boolean noNeedResponse){
            //post请求返回结果
            HttpClient httpClient = HttpClientBuilder.create().build();
            JSONObject jsonResult = null;
            HttpPost method = new HttpPost(url);
            
            for (Map.Entry<String, Object> entry : header.entrySet()) {
                method.setHeader(entry.getKey(), entry.getValue().toString());
            }
            try {
                if (null != jsonParam) {
                    //解决中文乱码问题
                    StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
                    entity.setContentEncoding("UTF-8");
                    entity.setContentType("application/json");
                    method.setEntity(entity);
                }
                HttpResponse result = httpClient.execute(method);
                url = URLDecoder.decode(url, "UTF-8");
                /**请求发送成功,并得到响应**/
                if (result.getStatusLine().getStatusCode() == 200) {
                    String str = "";
                    try {
                        /**读取服务器返回过来的json字符串数据**/
                        str = EntityUtils.toString(result.getEntity());
                        if (noNeedResponse) {
                            return null;
                        }
                        /**把json字符串转换成json对象**/
                        jsonResult = JSONObject.parseObject(str);
                    } catch (Exception e) {
    //                    logger.error("post请求提交失败:" + url, e);
                    }
                }
            } catch (IOException e) {
    //            logger.error("post请求提交失败:" + url, e);
            }
            return jsonResult;
        }
     
     
        /**
         * 发送get请求
         * @param url    路径
         * @return
         */
        public static JSONObject httpGet(String url){
            //get请求返回结果
            JSONObject jsonResult = null;
            try {
                HttpClient client = HttpClientBuilder.create().build();
                //发送get请求
                HttpGet request = new HttpGet(url);
                HttpResponse response = client.execute(request);
     
                /**请求发送成功,并得到响应**/
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    /**读取服务器返回过来的json字符串数据**/
                    String strResult = EntityUtils.toString(response.getEntity());
                    /**把json字符串转换成json对象**/
                    jsonResult = JSONObject.parseObject(strResult);
                    url = URLDecoder.decode(url, "UTF-8");
                } else {
    //                logger.error("get请求提交失败:" + url);
                }
            } catch (IOException e) {
    //            logger.error("get请求提交失败:" + url, e);
            }
            return jsonResult;
        }
    }
  • 相关阅读:
    随堂练习 shell脚本(五)
    随堂练习 shell脚本(四)
    随堂练习 shell脚本(三)
    马哥博客作业第五周
    JavaScript连载25-正则表达式的匹配分割替换以及贪婪模式
    Java连载130-JDBC编程初步
    C连载15-练习一波转换模式
    Android连载25-强制下线具体实现
    JavaScript连载24-正则表达式
    Java连载129-广播数据包、网络编程总结
  • 原文地址:https://www.cnblogs.com/phpfangzhan/p/10102011.html
Copyright © 2011-2022 走看看