package com.huimv.manage.webservice;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.Iterator;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Component
public class Soap {
    
    /**
     * @Method      : callSoap
     * @Description : 
     * @Params      : [text, url]
     * @Return      : java.lang.String
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/10/28       
     * @Time        : 16:29
     */
    public String callSoap(String text,String url) throws UnsupportedEncodingException {
        String str = new String(text.getBytes(), "UTF-8");
        System.out.println(str);
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 3000);
        httpClient.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 1000);
        PostMethod post = new PostMethod(url);
        post.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
        RequestEntity re = new InputStreamRequestEntity(new ByteArrayInputStream(str.getBytes()));
        post.setRequestEntity(re);
        BufferedReader in = null;
        try {
            int status = httpClient.executeMethod(post);
            System.out.println("返回状态码>>"+status);
            if (status == 200) {
                System.out.println("返回数据成功.");
                InputStream io = post.getResponseBodyAsStream();
                InputStreamReader is = new InputStreamReader(io, "UTF-8");
                in = new BufferedReader(is);
                String inputLine;
                StringBuilder result = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    result.append(inputLine);
                }
                String a = result.toString().replace("&lt;", "<").replace("&gt;", ">");
                System.out.println("返回下面的数据>>");
                System.out.println(a);
                return a;
            } else {
                System.out.println("调用接口失败");
                System.out.println(post.getResponseBodyAsString());
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return null;
                }
            }
            post.releaseConnection();
            httpClient.getHttpConnectionManager().closeIdleConnections(0);
        }
    }


}
