package com.jojubanking.boot.module.system.util.xytj;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import static com.jojubanking.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jojubanking.boot.module.system.enums.ErrorCodeConstants.TJ_SYSTEM_ERROR;

/**
 * 访问远程SOAP Web Service 协议接口
 */
public class SoapUtil {
    //外网测试地址
//    private static String url = "http://222.128.90.115:8010/BtGhYytWebService.asmx";
    //测试库地址
//    private static String url = "http://192.168.150.17:8002/BtGhYytWebService.asmx";

    //生成库地址
    private static String url = "http://192.168.150.15:8001/BtGhYytWebService.asmx";

    /**
     * @param xml
     * @return
     * @throws Exception
     */
    public static String soapMethod(String xml) throws Exception {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        // 设置请求头头
        method.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        // 设置请求体
        method.setRequestBody(xml);
        // 获取响应状态码
        int code = client.executeMethod(method);
        if (code == 200) {
            return method.getResponseBodyAsString();
        } else {
            throw exception(TJ_SYSTEM_ERROR);
        }
    }

    /**
     * 定时任务发送请求
     *
     * @param xml
     * @return
     * @throws Exception
     */
    public static String taskSoapMethod(String xml) throws Exception {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        // 设置请求头头
        method.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        // 设置请求体
        method.setRequestBody(xml);
        // 获取响应状态码
        int code = client.executeMethod(method);
        if (code == 200) {
            return method.getResponseBodyAsString();
        } else {
            return "";
        }
    }

}
