爱玺玺

爱玺玺的生活日记本。wx:lb87626

java post带素组的json数据 本质还是传递的是json字符串

package test;



import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.ContentType;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import org.json.JSONArray;


import com.alibaba.fastjson.JSONObject;


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.HashMap;

import java.util.Map;



public class HttpRequest2 {


      public static String sendPost(String url,String param){

          OutputStreamWriter out =null;

          BufferedReader reader = null;

          String response = "";


          //创建连接

          try {

              URL httpUrl = null; //HTTP URL类 用这个类来创建连接

              //创建URL

              httpUrl = new URL(url);

              //建立连接

              HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();

              conn.setRequestMethod("POST");

              conn.setRequestProperty("Content-Type", "application/json");

              conn.setRequestProperty("connection", "keep-alive");

              conn.setRequestProperty("cookie", "_lxsdk_cuid=16e8b8bc1d1c8-0b40b630af97cf-454c092b-1fa400-16e8b8bc1d129; _lxsdk=16e8b8bc1d1c8-0b40b630af97cf-454c092b-1fa400-16e8b8bc1d129; _hc.v=b4852671-4dd5-982d-01ce-2e3d6de0b616.1574299223; __guid=87664844.792314117330453800.1574299222975.8333; yellow-tips-Fri Jan 29 17:21:29 CST 2016=shown; __utma=1.1502615047.1574299223.1574299223.1574299223.1; __utmc=1; __utmz=1.1574299223.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); edper=xO0x7ct0ZD6oIAp7S7KD-GJeBMS9jE4f2JWfJyNMUQXXAt31N2f2YqM6CvOXLmVkealu0IFHmcRvgZmI6TastQ; JSESSIONID=5D78BD46EDAB684DE706BF89F36CD3E5; mpmerchant_portal_shopid=129383121; merchantBookShopID=129383121; merchantCategoryID=183; logan_session_token=qx65ctx03fqu0s4i1bc3; logan_custom_report=; monitor_count=5; _lxsdk_s=16e8b8bc1d2-4cd-d-cd7%7C%7C123");

              conn.setRequestProperty("Referer", "Referer:https://e.dianping.com/e-beauty/node/custommanage?display=2&accountId=23889459&shopId=72448927");

              conn.setUseCaches(false);//设置不要缓存

              conn.setInstanceFollowRedirects(true);

              conn.setDoOutput(true);

              conn.setDoInput(true);

              conn.connect();

              //POST请求

              out = new OutputStreamWriter(

                      conn.getOutputStream());

              out.write(param);

              out.flush();

              //读取响应

              reader = new BufferedReader(new InputStreamReader(

                      conn.getInputStream()));

              String lines;

              while ((lines = reader.readLine()) != null) {

                  lines = new String(lines.getBytes(), "utf-8");

                  response+=lines;

              }

              reader.close();

              // 断开连接

              conn.disconnect();


          } catch (Exception e) {

              System.out.println("发送 POST 请求出现异常!"+e);

              e.printStackTrace();

          }

          //使用finally块来关闭输出流、输入流

          finally{

              try{

                  if(out!=null){

                      out.close();

                  }

                  if(reader!=null){

                      reader.close();

                  }

              }

              catch(IOException ex){

                  ex.printStackTrace();

              }

          }


          return response;

      }




    public static void main(String[] args) {


        

        String param ="{\"industryName\":\"medicalbeauty\",\"promoChannel\":1,\"personIds\":[\"91612092\"],\"textContent\":\"你好\",\"shopID\":\"129383121\",\"shopAccountID\":\"\"}";

        String url="https://e.dianping.com/omnichannel/lightsaas/promo/promoaction?shopID=129383121&shopAccountID=&promoChannel=1&textContent=%E4%BD%A0%E5%A5%BD&personIds%5B0%5D=91612092&industryName=medicalbeauty";

        

        


        

        //这里是发送过去的

        String sendPost = sendPost(url, param);

        System.out.println(sendPost);



    }


}


发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5