webservice client

啥也不说了,看代码吧,都封装好了

package cloud.data.service.service;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class WebService {

    public static void main(String[] args) throws Exception {

        //webservicve接口地址
        String postUrl="http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx";
        // soapAction地址
        String soapAction="http://WebXml.com.cn/getEnCnTwoWayTranslator";
        //请求方式 post//get
        String requestMethod = "POST";
        //参数组
        Map paramsMap = new HashMap();
        paramsMap.put("Word","sea");
        String result = doSoapPost(postUrl,soapAction,requestMethod,paramsMap);//访问接口
        System.out.println("result:   " + result);
        System.out.println("result2:   " + result.substring(result.indexOf("string") - 1,result.lastIndexOf("string") + 7));
        System.out.println("result3:   " + result.substring(result.indexOf("string") - 1,result.lastIndexOf("string") + 7).replaceAll("",""));

    }

    public static String doSoapPost(String postUrl,String soapAction,String requestMethod,Map paramsMap) throws Exception {
        URL url = new URL(postUrl);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        //拼接请求体,此处也可以在外面写xml文件然后读取,但是为了方便一个文件搞定,而且参数也比较好传递我们直接String拼接
        String soap = "\n" +
                "\n" +
                " \n" +
                " \n";
        for(String key : paramsMap.keySet()) {
            soap = soap + "<" + key + ">" + paramsMap.get(key) + "\n";
        }
        soap = soap + " \n" + " \n" + "";
        byte[] buf = soap.getBytes();
        //设置一些头参数
        httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("soapActionString", soapAction);
        httpConn.setRequestMethod(requestMethod);
        //输入参数和输出结果
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        OutputStream out = httpConn.getOutputStream();
        out.write(buf);
        out.close();

        //打印出解析结果
        byte[] datas = readInputStream(httpConn.getInputStream());
        String result = new String(datas);
       return result;
    }

    /**
     * 从输入流中读取数据
     *
     * @param inStream
     * @return
     * @throws Exception
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        inStream.close();
        return data;
    }

}  

你可能感兴趣的:(java,webService)