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;
        }
    }
  • 相关阅读:
    iOS 8 界面设计 PSD 模板(iPhone 6),免费下载
    20个华丽的时间轴例子,寻找网页设计灵感
    Hello.js – Web 服务授权的 JavaScript SDK
    Web 开发人员和设计师必读文章推荐【系列三十】
    Arctext.js
    30套免费的响应式 HTML5 & CSS3 模板下载
    Midnight.js – 实现奇妙的固定头部切换效果
    推荐20个很有帮助的 Web 前端开发教程
    SweetAlert – 替代 Alert 的漂亮的提示效果
    其实每个行业都有各自的辛苦,好的程序员并不累,他们乐此不疲(见过太多在职位事业、人生方向上随转如流的人,累了疲乏了就去做别的事情了。必须有自己的坚守和立足的点,自我驱动,否则沦为在别人的体制制度中被驱赶一生)good
  • 原文地址:https://www.cnblogs.com/phpfangzhan/p/10102011.html
Copyright © 2011-2022 走看看