package com.zcloud.util;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

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

public class HttpEmisDuijieUtil {
    /**  emis 对接地址   */
    public static final String HttpUrl = "http://192.168.119.4:10001/webservice.asmx/ReviceData";

    public static String  sendPostNoAccept(String strUrl) throws Exception{
//        InputStream is = null;
        BufferedInputStream bufferedInputStream = null;
        // 创建远程url连接对象
        URL url = new URL(HttpUrl);
        // 通过远程url连接对象打开一个连接，强转成httpURLConnection类
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setDoInput(true);
        //post请求必须设置此项setDoOutput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//        SOAPAction: "http://tempuri.org/ReviceData"
//        httpURLConnection.setRequestProperty("SOAPAction", "\"http://tempuri.org/ReviceData\"");
        //设置连接主机服务器的超时时间：15000毫秒
        httpURLConnection.setConnectTimeout(15000);
        // 设置读取远程返回的数据时间：60000毫秒
        httpURLConnection.setReadTimeout(60000);
        //发送请求
        httpURLConnection.connect();
        OutputStream out = httpURLConnection.getOutputStream();
        out.write(strUrl.getBytes());//此处将参数传过去
        //通过connection获取输入流

        StringBuilder sb = new StringBuilder();

        if (httpURLConnection.getResponseCode() == 200) {
            //获取输入流
            InputStream is = httpURLConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String temp = null;
            while (null != (temp = br.readLine())) {
                sb.append(temp);
            }
            is.close();
            isr.close();
            br.close();
        }
        Document document = DocumentHelper.parseText(sb.toString());
        Element root = document.getRootElement();
        return root.getData().toString();
    }

}
