package com.youchain.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpPutUtil {
    public static void main(String[] args) {
        System.out.println("HttpPutUtil.main");
        String url="http://192.168.0.201:8092/synq/orders/pick-confirmation";
        JSONObject paramsJson=new JSONObject();
        JSONArray orderLine=new JSONArray();
            JSONObject order = new JSONObject();
            order.put("orderLineNumber", "1");
            order.put("quantity", "10");
            order.put("productId", "704237988");
                JSONArray attributeValue=new JSONArray();
                    JSONObject ztBidName=new JSONObject();
                    ztBidName.put("name", "ztBidName");
                    ztBidName.put("value", "2202山东单相表");
                attributeValue.add(ztBidName);
                    JSONObject prodMtrlNo=new JSONObject();
                    prodMtrlNo.put("name", "prodMtrlNo");
                    prodMtrlNo.put("value", "704520757");
                attributeValue.add(prodMtrlNo);

            order.put("attributeValue",attributeValue);
        orderLine.add(order);
        paramsJson.put("orderLine",orderLine);
        String res = HttpPutUtil.sendPutReq(url+"/NARI/S202405071551WLDD1",paramsJson.toString());
        System.out.println("返回："+res);
    }
    public static String sendPutReq(String httpUrl, String params) {
       System.out.println("url:"+httpUrl);
       System.out.println("params:"+params);
        String result = "";
        URL url = null;
        try {
            url = new URL(httpUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        if (url != null) {
            try {
                /*HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
                urlConn.setRequestProperty("content-type", "application/json");
                urlConn.setDoInput(true);
                urlConn.setDoOutput(true);
                urlConn.setConnectTimeout(5 * 1000);
                //设置请求方式为 PUT
                urlConn.setRequestMethod("PUT");

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

                urlConn.setRequestProperty("Charset", "UTF-8");


                DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
                //写入请求参数
                //这里要注意的是，在构造JSON字符串的时候，实践证明，最好不要使用单引号，而是用“\”进行转义，否则会报错
                // 关于这一点在上面给出的参考文章里面有说明
//                String jsonParam = params;
                dos.write(params.getBytes());
                dos.flush();
                dos.close();

                if (urlConn.getResponseCode() == 200) {
                    InputStreamReader isr = new InputStreamReader(urlConn.getInputStream());
                    BufferedReader br = new BufferedReader(isr);
                    String inputLine = null;
                    while ((inputLine = br.readLine()) != null) {
                        result += inputLine;
                    }
                    isr.close();
                    urlConn.disconnect();
                }*/

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }
}
