java发送application/json格式的post请求,需要登陆

package util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class JavaPostJson2 {
    final static String url = "http://t-jzz.pcauto.com.cn:9001/admin/content/add.do";
    
    
    /**
     * 发送HttpPost请求
     * 
     * @param strURL
     *            服务地址
     * @param params
     *            json字符串,例如: "{ \"id\":\"12345\" }" ;其中属性名必须带双引号<br/>
     * @return 成功:返回json字符串<br/>
     */
    public static String post(String strURL, String params) {
        BufferedReader reader = null;
        try {
            URL url = new URL(strURL);// 创建连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod("POST"); // 设置请求方式
            // connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
、      //因为要登陆才可以执行请求,所以这里要带cookie的header connection.setRequestProperty("Cookie", "geli-session=41b6d86db1e97df5:-60813064:16d0571e3e8:-3a58377431669b192d08; c=58ydwry; u=58zqg8z; pcsuv=1482230765738.a.46958170; u4ad=4031lfgby; UM_distinctid=16be539fa796b9-099f04a8a5445d-4144032b-1fa400-16be539fa7a7d3; visitedfid=22035D16260D14152; gr_user_id=b2a6251c-dddb-4502-91f9-0b6f10ccb260; favCar=%E5%A5%A5%E8%BF%AAA3_9550%7C%E5%A5%A5%E8%BF%AAA8L_7%7C%E7%A6%8F%E5%85%8B%E6%96%AFActive_25101%7C%E8%BD%A9%E9%80%B8_3996%7C%E5%88%9B%E7%95%8C_24483; locationWap=%7B%22expires%22%3A1568884707863%2C%22city%22%3A%22%E5%93%88%E5%B0%94%E6%BB%A8%E5%B8%82%22%2C%22cityUser%22%3A%22%E5%93%88%E5%B0%94%E6%BB%A8%E5%B8%82%22%2C%22cityCode%22%3A%22230100%22%2C%22proCode%22%3A%22230000%22%2C%22cityCodeUser%22%3A%22230100%22%2C%22proCodeUser%22%3A%22230000%22%7D; AplocationWap=%7B%22regionId%22%3A187%2C%20%22regionName%22%3A%22%E5%93%88%E5%B0%94%E6%BB%A8%22%7D; pcLocate=%7B%22proCode%22%3A%22440000%22%2C%22pro%22%3A%22%E5%B9%BF%E4%B8%9C%E7%9C%81%22%2C%22cityCode%22%3A%22441900%22%2C%22city%22%3A%22%E4%B8%9C%E8%8E%9E%E5%B8%82%22%2C%22dataType%22%3A%22user%22%2C%22expires%22%3A1571888929008%7D; pcautoLocate=%7B%22proId%22%3A5%2C%22cityId%22%3A6%2C%22url%22%3A%22%2F%2Fwww.pcauto.com.cn%2Fqcbj%2Fdg%2F%22%2C%22dataTypeAuto%22%3A%22user%22%7D; PClocation=6; pcuvdata=lastAccessTime=1570869719999|visits=289; channel=9396"); connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式 connection.connect(); //一定要用BufferedReader 来接收响应, 使用字节来接收响应的方法是接收不到内容的 OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码 out.append(params); out.flush(); out.close(); // 读取响应 reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line; String res = ""; while ((line = reader.readLine()) != null) { res += line; } reader.close(); return res; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "error"; // 自定义错误信息 } public static void getparame() { String data1 = "{\"type\":1,\"aiAdId\":\""; String data3= "\",\"contents\":[{\"content_id\":\"18399180\",\"title\":\"CS55钜惠5000元 欢迎到店试乘试驾!\",\"pub_date\":\"2019-10-21 12:00:00 \",\"pub_url\":\"http://www.pcauto.com.cn/qcbj/1839/18399180.html\",\"channel_id\":\"000000185 / 000084302 / 000084320 / \",\"guide_pic\":\"https://img0.pcauto.com.cn/pcauto/1904/01/g_15446002_1554101963823.jpg\"},{\"content_id\":\"18399239\",\"title\":\"全系CS35Plus热销中 可到店品鉴!\",\"pub_date\":\"2019-10-21 12:00:00 \",\"pub_url\":\"http://www.pcauto.com.cn/qcbj/1839/18399239.html\",\"channel_id\":\"000000185 / 000084302 / 000084320 / \",\"guide_pic\":\"https://img0.pcauto.com.cn/pcauto/1905/09/g_15965356_1557385235655.jpg\"},{\"content_id\":\"18399181\",\"title\":\"逸动现优惠高达0.8万 欢迎到店品鉴!\",\"pub_date\":\"2019-10-21 12:00:00 \",\"pub_url\":\"http://www.pcauto.com.cn/qcbj/1839/18399181.html\",\"channel_id\":\"000000185 / 000084302 / 000084320 / \",\"guide_pic\":\"https://img0.pcauto.com.cn/pcauto/1905/09/g_15965315_1557385082630.jpg\"}]}\r\n" ; Tools tools=new Tools(); List<String> listall = tools.readFile02("E:\\项目\\2019年\\10月\\ad\\aid.txt"); for (String str : listall) { String params=data1+str+data3; System.out.println(params); String reString=post(url,params); System.out.println(reString); } // String pa="{\"type\":1,\"aiAdId\":\"1067\",\"contents\":[{\"content_id\":\"18399180\",\"title\":\"CS55钜惠5000元 欢迎到店试乘试驾!\",\"pub_date\":\"2019-10-21 12:00:00 \",\"pub_url\":\"http://www.pcauto.com.cn/qcbj/1839/18399180.html\",\"channel_id\":\"000000185 / 000084302 / 000084320 / \",\"guide_pic\":\"https://img0.pcauto.com.cn/pcauto/1904/01/g_15446002_1554101963823.jpg\"},{\"content_id\":\"18399239\",\"title\":\"全系CS35Plus热销中 可到店品鉴!\",\"pub_date\":\"2019-10-21 12:00:00 \",\"pub_url\":\"http://www.pcauto.com.cn/qcbj/1839/18399239.html\",\"channel_id\":\"000000185 / 000084302 / 000084320 / \",\"guide_pic\":\"https://img0.pcauto.com.cn/pcauto/1905/09/g_15965356_1557385235655.jpg\"},{\"content_id\":\"18399181\",\"title\":\"逸动现优惠高达0.8万 欢迎到店品鉴!\",\"pub_date\":\"2019-10-21 12:00:00 \",\"pub_url\":\"http://www.pcauto.com.cn/qcbj/1839/18399181.html\",\"channel_id\":\"000000185 / 000084302 / 000084320 / \",\"guide_pic\":\"https://img0.pcauto.com.cn/pcauto/1905/09/g_15965315_1557385082630.jpg\"}]}\r\n" + // ""; // post(url, pa); //System.out.println(strURL); //System.out.println(params); } public static void main(String[] args) { getparame(); } }

  

java发送application/json格式的post请求,需要登陆

上一篇:EasyRTMP实现内网摄像头RTSP拉流转码RTMP推流到RTMP服务器之EasyRTMP-iOS的extension是什么?


下一篇:axios学习和使用