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();

    }
}
相關文章
相關標籤/搜索