webservice client

本文介绍了一种使用Java通过调用WebService接口实现英文到中文翻译的方法。通过构造SOAP请求并发送到指定的WebService,可以获取翻译结果。文章提供了完整的代码示例,包括如何设置HTTP请求头,构建SOAP请求体以及处理响应结果。

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

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<String, String> paramsMap = new HashMap<String, String>();
        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("</{0,1}(string)?>",""));

    }

    public static String doSoapPost(String postUrl,String soapAction,String requestMethod,Map<String, String> paramsMap) throws Exception {
        URL url = new URL(postUrl);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        //拼接请求体,此处也可以在外面写xml文件然后读取,但是为了方便一个文件搞定,而且参数也比较好传递我们直接String拼接
        String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                " <soap:Body>\n" +
                " <getEnCnTwoWayTranslator xmlns=\"http://WebXml.com.cn/\">\n";
        for(String key : paramsMap.keySet()) {
            soap = soap + "<" + key + ">" + paramsMap.get(key) + "</" + key + ">\n";
        }
        soap = soap + " </getEnCnTwoWayTranslator>\n" + " </soap:Body>\n" + "</soap:Envelope>";
        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;
    }

}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值