transmitting java application / json post request format required login

util Package; 

Import java.io.IOException; 
Import a java.io.InputStream; 
Import java.io.OutputStreamWriter; 
Import java.net.HttpURLConnection The; 
Import the java.net.URL; 
Import java.util.List; 
Import the java.io. the BufferedReader; 
Import the java.io.InputStreamReader; 

public class JavaPostJson2 { 
    Final static String URL = "http://t-jzz.pcauto.com.cn:9001/admin/content/add.do"; 
    
    
    / ** 
     * send HttpPost request 
     * 
     * @param strURL 
     * service address 
     * @param the params 
     * JSON string, for example: "{\" id \ " : \" 12345 \ "}"; wherein the attribute names must quotation marks a 
     * @return success: returns a string json <br/> 
     * /
    POST static String public (strURL String, String the params) { 
        the BufferedReader Reader = null; 
        the try { 
            the URL = new new URL the URL (strURL); // create a connection 
            the HttpURLConnection Connection = (the HttpURLConnection) url.openConnection (); 
            connection.setDoOutput (to true) ; 
            connection.setDoInput (to true); 
            connection.setUseCaches (to false); 
            connection.setInstanceFollowRedirects (to true); 
            connection.setRequestMethod ( "the POST"); // set request mode 
            // connection.setRequestProperty ( "Accept", " application / json "); // set the format of the received data 
, // signed in to perform due to the request, so here we are going to take a cookie header 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 "); 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 "); 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"); // Set the format of transmission data connection.connect (); // use certain to receive the response BufferedReader, using the method to receive the response byte is received less content OutputStreamWriter out = new OutputStreamWriter (connection.getOutputStream ( ), "UTF-8"); // utf-8 encoded out.append (the params); out.flush (); the out.close (); // read response Reader the BufferedReader new new = (the InputStreamReader new new (connection.getInputStream (), "UTF-. 8")); String Line; String RES = ""; the while (! (= reader.readLine Line ()) = null) { res + = line; } reader.close(); RES return; } the catch (IOException E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); } return "error"; // custom error message } public static void getparame () { String data1 = "{\"type\":1,\"aiAdId\":\""; pcauto.com.cn/pcauto/1905/09/g_15965356_1557385235655.jpg \ "}, {\" content_id \ ": \" 18399181 \ ", \" title \ ": \" Yi moving now discounts of up to 08,000 welcome to shop tasting! \ ", \" 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(); } }

  

Guess you like

Origin www.cnblogs.com/sincoolvip/p/11715021.html