1. 程式人生 > >Java HTTP請求URL後面的引數作為對方接收流

Java HTTP請求URL後面的引數作為對方接收流

package com.test.gov.supervision.processor.gd.gz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.test.gov.supervision.processor.js.nj.Properties;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
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.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zhuqinghua on 2018/10/16.
 */
public class GDGZOrderProcessor {

    private static  final String  url="http://xxxx:8083/Management/xxx.ashx";
    private static final RestTemplate restTemplate =new RestTemplate();
     public  static   String processorStream(String url, String param){
         System.out.println("========");
         PrintWriter out = null;
         BufferedReader in = null;
         String result = "";
         try {
             URL realUrl = new URL(url);
             // 開啟和URL之間的連線
             URLConnection conn = realUrl.openConnection();
             // 設定通用的請求屬性
             conn.setRequestProperty("accept", "*/*");
             conn.setRequestProperty("connection", "Keep-Alive");
//             conn.setRequestProperty("user-agent",
//                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
             // 傳送POST請求必須設定如下兩行
             conn.setDoOutput(true);
             conn.setDoInput(true);
             // 獲取URLConnection物件對應的輸出流
             out = new PrintWriter(conn.getOutputStream());
             // 傳送請求引數
             out.print(param);
             // flush輸出流的緩衝
             out.flush();
             // 定義BufferedReader輸入流來讀取URL的響應
             in = new BufferedReader(
                     new InputStreamReader(conn.getInputStream()));
             String line;
             while ((line = in.readLine()) != null) {
                 result += line;
             }
             System.out.println(result);
         } catch (Exception e) {
             System.out.println("傳送 POST 請求出現異常!"+e);
             e.printStackTrace();
         }
         //使用finally塊來關閉輸出流、輸入流
         finally{
             try{
                 if(out!=null){
                     out.close();
                 }
                 if(in!=null){
                     in.close();
                 }
             }
             catch(IOException ex){
                 ex.printStackTrace();
             }
         }
         return result;

}

    public static void main(String[] args) {
        String time=new SimpleDateFormat("yyyyMMddHHmmSS").format(new Date());

        JSONArray body = new JSONArray();
        JSONObject com = new JSONObject();
        com.put("symbol","mobike0220122001");
        com.put("companyId","mobike");
        com.put("address","1");
        com.put("bicycleNo","0220122001");
        com.put("bicycleColor","橙色");
        com.put("putInArea","1");
        com.put("putInDate","1");
        com.put("usedCycle",72);
        com.put("operateState",0);
        com.put("checkDate","20181001");
        com.put("callBackTime","20201001");
        com.put("putBatchNo","xx201810");
        com.put("operationName","mobiker");
        com.put("operationContact","010xxxxxx");
        com.put("state","1");
        com.put("flag","1");
        com.put("updateTime",time);
        body.add(com);
       String par="cmd=CLJB&fromSource=BASIC&body="+body;

        GDGZOrderProcessor.processorStream("http://xxxx:8083/Management/xxx.ashx",par);
    }
}