码迷,mamicode.com
首页 > Web开发 > 详细

最简单易懂的webService客户端之soap请求

时间:2016-10-16 13:52:31      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

代码准备:

  1.网络上有提供一些免费的服务器测试地址,可以上这里找一找:https://my.oschina.net/CraneHe/blog/183471

  2.我选择了一个翻译地址:http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx

    2.1打开之后看到该地址下有一个方法:

技术分享

    2.2点击进入,网站会提供该方法的客户端请求xml格式:

技术分享

    2.3,这个红框部分就是我们要的,将它写入代码,就可以完成请求了.

    注意:以上还是获取soap请求xml的方法,也是比较入门的方式,有经验的筒子直接上wsdl利用解释文档也可以自己写xml...

然后是代码,我直接附上代码,大家直接复制即可运行,附注释.

Translate.class
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * Created by garfield on 2016/10/16.
 */



public class Translate {
    public static void translate(String word ) throws Exception {
        //地址
        String urlString = "http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx ";
        //方法
        String soapActionString = "http://WebXml.com.cn/getEnCnTwoWayTranslator";
        URL url = new URL(urlString);
        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" +
                "            <Word>" + word + "</Word>\n" +
                "        </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", soapActionString);
        httpConn.setRequestMethod("POST");
        //输入参数和输出结果
        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);
        System.out.println("result:" + result);
        System.out.println(result.substring(result.indexOf("string") - 1,result.lastIndexOf("string") + 7));
        System.out.println(result.substring(result.indexOf("string") - 1,result.lastIndexOf("string") + 7).replaceAll("</{0,1}(string)?>",""));
    }

    /**
     * 从输入流中读取数据
     *
     * @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;
    }

    public static void main(String[] args) throws Exception {
        translate("sea");
    }

运行结果:

result:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getEnCnTwoWayTranslatorResponse xmlns="http://WebXml.com.cn/"><getEnCnTwoWayTranslatorResult><string>sea: [ si: ]</string><string>n. 海,海洋 |</string></getEnCnTwoWayTranslatorResult></getEnCnTwoWayTranslatorResponse></soap:Body></soap:Envelope>
<string>sea: [ si: ]</string><string>n. 海,海洋 |</string>
sea: [ si: ]n. 海,海洋 |

  第一行是直接返回的结果,下面两行帮助理解解析,最后得到sea单词的解释,是不是简单清楚...

 

最简单易懂的webService客户端之soap请求

标签:

原文地址:http://www.cnblogs.com/garfieldcgf/p/5966317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!