package cn.net.test;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.net.test.bean.WarehouseReqBean;

public class TestA {
	
	public static void main(String[] args) {
		String url = "http://172.16.1.54:8980/API/JPDeliveryOrder/DeliveryOrderList";
		WarehouseReqBean b = new WarehouseReqBean();
//		b.setCWhCode("JP60");
		b.setCWhCode("JP63");
		Date d = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String date = format.format(d);
		b.setStartDate(date +" 00:00:00");
		b.setEndDate(date +" 23:59:59");
		b.setDataSource("ZS");
		b.setToken("000224af7242462087d2853a91dc009d");
		Gson gson = new Gson();
		
//		doJsonPost(url, gson.toJson(b, WarehouseReqBean.class));
		System.out.println("发送的数据："  + gson.toJson(b));
		doJsonPost(url, gson.toJson(b));
	}
	
	public static String doJsonPost(String urlPath, String Json) {
        // HttpClient 6.0被抛弃了
        String result = "";
        BufferedReader reader = null;
        try {
//        	Log.i(TConfig.Echo, "开始请求Test数据");
            URL url = new URL(urlPath);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Charset", "UTF-8");
            // 设置文件类型:
            conn.setRequestProperty("Content-Type","application/json; charset=UTF-8");
            // 设置接收类型否则返回415错误
            //conn.setRequestProperty("accept","*/*")此处为暴力方法设置接受所有类型，以此来防范返回415;
          conn.setRequestProperty("accept","application/json");
            // 往服务器里面发送数据
//            if (Json != null && !TextUtils.isEmpty(Json)) { }
                byte[] writebytes = Json.getBytes();
                // 设置文件长度
                conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
                OutputStream outwritestream = conn.getOutputStream();
                outwritestream.write(Json.getBytes());
                outwritestream.flush();
                outwritestream.close();
//                Log.i(TConfig.Echo, "doJsonPost: conn"+conn.getResponseCode());
           
            if (conn.getResponseCode() == 200) {
                reader = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));
                result = reader.readLine();
//                LogUtil.i(TConfig.Echo, "获取接受到的数据：" + result);
                System.out.println("result:" +result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

}
