/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.utils;

import com.crunii.micro.common.http.HttpClientUtil;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 田平 create 2018年10月11日下午7:14:04
 */
public class SoapUtil {
	protected static final Logger log = LoggerFactory.getLogger(SoapUtil.class);

	/**
	 * @param reqXml
	 * @param url
	 * @param readTimeout 单位是秒
	 * @return
	 */
	public static String callSoap(String reqXml, String url, int readTimeout) {
		List<Header> headers = new ArrayList<Header>();
		Header header1 = new BasicHeader("soapAction", "");
		headers.add(header1);
		ContentType contentType = ContentType.create("text/html", Consts.UTF_8);
		return callSoap(reqXml, headers, contentType, url, readTimeout);
	}

	/**
	 * @param reqXml
	 * @param headers
	 * @param contentType
	 * @param url
	 * @param readTimeout 单位是秒
	 * @return
	 */
	public static String callSoap(String reqXml, List<Header> headers, ContentType contentType, String url, int readTimeout) {
		String rspXml = "";
		log.info("reqXml:\n" + reqXml);
		StringEntity entity = null;
		try {
			entity = new StringEntity(reqXml, contentType);
		} catch (Exception e) {
			log.error("callSoap error!", e);
			throw new RuntimeException(e);
		}
		rspXml = HttpClientUtil.reqStrByEntity(url, entity, "POST", headers, HttpClientUtil.getSockConf(readTimeout * 1000));
		log.info("rspXml:\n" + rspXml);
		return rspXml;
	}

	/**
	 * 默认超时时间60秒
	 *
	 * @param reqXml
	 * @param url
	 * @return
	 */
	public static String callSoap(String reqXml, String url) {
		return callSoap(reqXml, url, 60000);
	}

	public static void main(String[] args) {
		String callSoap = callSoap("<a>1231</a>", "http://81.68.133.219:7355/services/BsnServicePort?wsdl");
		System.out.println(callSoap);
	}
}
