java 发起post请求,自定义header和body

11Java 专栏收录该内容
264 篇文章 0 订阅
package io.renren.modules.websocket.test;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @date 2019/10/1017:12
 */
public class HttpUtils {


    //post请求
    /**
     * @param url
     * @param headerMap  header 参数
     * @param contentMap body 参数
     * @return
     */
    public static String postMap(String url, Map<String, String> headerMap, Map<String, String> contentMap) {
        String result = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        List<NameValuePair> content = new ArrayList<NameValuePair>();
        Iterator iterator = contentMap.entrySet().iterator();           //将content生成entity
        while (iterator.hasNext()) {
            Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
            content.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
        }
        CloseableHttpResponse response = null;
        try {
            Iterator headerIterator = headerMap.entrySet().iterator();          //循环增加header
            while (headerIterator.hasNext()) {
                Map.Entry<String, String> elem = (Map.Entry<String, String>) headerIterator.next();
                post.addHeader(elem.getKey(), elem.getValue());
            }
            if (content.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(content, "UTF-8");
                post.setEntity(entity);
            }
            response = httpClient.execute(post);            //发送请求并接收返回数据
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();       //获取response的body部分
                result = EntityUtils.toString(entity);          //读取reponse的body部分并转化成字符串
            }
            return result;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }


    // url格式转map
    public static Map<String, String> paramToMap(String paramStr) {
        String[] params = paramStr.split("&");
        Map<String, String> resMap = new HashMap<String, String>();
        for (int i = 0; i < params.length; i++) {
            String[] param = params[i].split("=");
            if (param.length >= 2) {
                String key = param[0];
                String value = param[1];
                for (int j = 2; j < param.length; j++) {
                    value += "=" + param[j];
                }
                resMap.put(key, value);
            }
        }
        return resMap;
    }

    //Header字符串转map
    public static Map<String, String> splid(String zz) {
        String[] stepOne = zz.split("\n");
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < stepOne.length; i++) {
            String[] stepTwo = stepOne[i].split(": ");
            if (map.get(stepTwo[0]) == null)
                map.put(stepTwo[0], stepTwo[1]);
            else
                map.put(stepTwo[0], stepTwo[1] + "," + map.get(stepTwo[0]));
        }
        return map;
    }

    public static void main(String[] args) {
        String header =
                "Host: r.cnews.qq.com\n" +
                        "Accept-Encoding: gzip,deflate\n" +
                        "Referer: http://cnews.qq.com/cnews/android/\n" +
                        "User-Agent: %E5%A4%A9%E5%A4%A9%E5%BF%AB%E6%8A%A56080(android)\n" +
                        "Cookie: lskey=; luin=; skey=; uin=; logintype=0;\n" +
                        "snqn: isrlo83QeV0HUKSXBDEC2KU0i6dA6NJ+cJ6DfolV5BUaHTHKTIU0WQXRMHW9bo+0oWH2Uep5SZ6wOU2ufjuBOl9TYQKJaDModIprMcdjEIFHr32o8/mB9Da/apEitNzMj/o+zfCmWj7zQZTmMNvZaQ==\n" +
                        "svqn: 1_4\n" +
                        "qn-sig: 8da32c703ef980b76fc3343118e1a9f8\n" +
                        "qn-rid: 16e0d706-a1e4-465b-9a39-ce0caea0e03f\n" +
                        "Content-Type: application/x-www-form-urlencoded\n" +
                        "Connection: Keep-Alive";
        //"Content-Length: 2267\n";
        Map splid = splid(header);
        System.out.println(splid);
        Map<String, String> bodyMap = paramToMap("adcode=440111&last_id=20191008A046A900&lon=113.268162&cityList=%E5%B9%BF%E5%B7%9E&loc_streetNo=&forward=1&refreshType=normal&provinceId=19&last_time=1570521327&userCity=%E5%B9%BF%E5%B7%9E&bottom_id=20191007A0DZI400&loc_name=%E7%99%BD%E4%BA%91%E7%BB%BF%E5%9C%B0%E4%B8%AD%E5%BF%83&top_time=1570495721&manualRefresh=1&top_id=20191008A046A900&loc_addr=%E5%B9%BF%E4%B8%9C%E7%9C%81%E5%B9%BF%E5%B7%9E%E5%B8%82%E7%99%BD%E4%BA%91%E5%8C%BA%E4%BA%91%E5%9F%8E%E8%A5%BF%E8%B7%AF%E4%B8%8E%E9%BD%90%E5%BF%83%E8%B7%AF%E4%BA%A4%E5%8F%89%E5%8F%A3%E5%8D%97150%E7%B1%B3&page=8&cityId=198&lastRefreshTime=1570521327&loc_street=%E4%BA%91%E5%9F%8E%E8%A5%BF%E8%B7%AF&preload=1&loc_catalog=%E6%88%BF%E4%BA%A7%E5%B0%8F%E5%8C%BA%3A%E5%95%86%E5%8A%A1%E6%A5%BC%E5%AE%87&refresh_from=refresh_footer&direction=1&sessionid=undefined&chlid=kb_news_hotnews&bottom_time=1570435632&is_viola=1&loc_accuracy=40.0&lat=23.180721&REQBuildTime=1570521551393&adcode=440111&lon=113.268162&ssid=YR-TECH&omgid=f4287f3ea730a249015bd9d06a73f6dc23c00010212406&REQExecTime=1570521551412&qqnetwork=wifi&commonsid=7b7615ee4c8e412b9ba95ca7ebbca7eb&kingCardType=0&picSizeMode=0&adCookie=&commonGray=1_3%7C2_1%7C12_0%7C49_1%7C14_1%7C17_1%7C30_1%7C99_1&currentTab=kuaibao&proxy_addr=192.168.1.99%3A8888&is_wap=0&lastCheckCardType=0&omgbizid=a323e236f72cee4c10e96ec642cb73943d3a0080214908&imsi=460005292210411&commonIsFirstLaunch=0&bssid=22%3Abc%3A5a%3A74%3Aa0%3A88&taid=0101869FFED6E4A84F898262CADBA96EC2521FB86438AEE01A238B538F2A8F90BA0F96B93C4F0770FD1F0AED&activefrom=icon&unixtimesign=1570521551414&qimei=92a420ebc78a0356&Cookie=%26lskey%3D%26luin%3D%26skey%3D%26uin%3D%26logintype%3D0&qaid=0171E6660E55174412AC482013E10C8E&imsi_history=460005292210411&qn-sig=8da32c703ef980b76fc3343118e1a9f8&qn-rid=16e0d706-a1e4-465b-9a39-ce0caea0e03f&lat=23.180721&hw_fp=Coolpad%2FCool1_CN%2Fcool_c1%3A6.0.1%2FZAXCNFN5902606201S%2F0001820%3Auser%2Frelease-keys&gpu=Qualcomm%20Adreno%20%28TM%29%20510&mid=ca57d1f0e21794164956b43da41d7fea68de70fa&devid=861795038752864&mac=54%3ADC%3A1D%3A2A%3A0A%3A2A&store=9002096&screen_height=1920&apptype=android&origin_imei=861795038752864&codeclevel=5.1&rover=1&hw=Coolpad_C106&appversion=6.0.80&appver=23_areading_6.0.80&uid=92a420ebc78a0356&screen_width=1080&sceneid=&android_id=92a420ebc78a0356");

        String s = postMap("https://r.cnews.qq.com/getVerticalChannel?devid=861795038752864", splid, bodyMap);
        System.out.println(s);
    }

}

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
<p> <span style="color:#333333;">本课程详细讲解了以下内容:<br />     1.jsp环境搭建及入门、虚拟路径虚拟主机、JSP执行流程<br /><br />     2.使用Eclipse快速开发JSP、编码问题、JSP页面元素以及request对象、使用request对象实现注册示例<br /><br />     3.请求方式的编码问题、response、请求转发重定向、cookie、session执行机制、session共享问题<br /><br />      4.session与cookie问题及application、cookie补充说明及四种范围对象作用域<br /><br />      5.JDBC原理及使用Statement访问数据库、使用JDBC切换数据库以及PreparedStatement的使用、Statement与PreparedStatement的区别<br /><br />      6.JDBC调用存储过程存储函数、JDBC处理大文本CLOB及二进制BLOB类型数据<br /><br />      7.JSP访问数据库、JavaBean(封装数据封装业务逻辑)<br /><br />      8.MVC模式与Servlet执行流程、Servlet25与Servlet30的使用、ServletAPI详解与源码分析<br /><br />      9.MVC案例、三层架构详解、乱码问题以及三层代码流程解析、完善ServiceDao、完善View、优化用户体验、优化三层(加入接口DBUtil)<br /><br />     1 0.Web调试及bug修复、分页SQL(Oracle、MySQL、SQLSERVER)<br /><br />      11.分页业务逻辑层数据访问层Service、Dao、分页表示层Jsp、Servlet<br /><br />      12.文件上传及注意问题、控制文件上传类型大小、下载、各浏览器下载乱码问题<br /><br />      13.EL表达式语法、点操作符中括号操作符、EL运算、隐式对象、JSTL基础及set、out、remove<br /><br />      14.过滤器、过滤器通配符、过滤器链、监听器<br /><br />      15.session绑定解绑、钝化活化<br /><br />      16.以及Ajax的各种应用<br /></span> </p> <p> <span style="color:#333333;"><br /></span> </p> <p> <span style="color:#333333;">     17. Idea环境下的Java Web开发</span> </p>
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值