webservice之获得中文、英文双向翻译

来源:互联网 发布:万能搬家软件下载 编辑:程序博客网 时间:2024/05/15 20:02

webservice地址:http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx?op=getEnCnTwoWayTranslator

soap协议:

SOAP 1.1以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。POST /WebServices/TranslatorWebService.asmx HTTP/1.1Host: www.webxml.com.cnContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://WebXml.com.cn/getEnCnTwoWayTranslator"<?xml version="1.0" encoding="utf-8"?><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/">  <soap:Body>    <getEnCnTwoWayTranslator xmlns="http://WebXml.com.cn/">      <Word>string</Word>    </getEnCnTwoWayTranslator>  </soap:Body></soap:Envelope>HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><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/">  <soap:Body>    <getEnCnTwoWayTranslatorResponse xmlns="http://WebXml.com.cn/">      <getEnCnTwoWayTranslatorResult>        <string>string</string>        <string>string</string>      </getEnCnTwoWayTranslatorResult>    </getEnCnTwoWayTranslatorResponse>  </soap:Body></soap:Envelope>SOAP 1.2以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。POST /WebServices/TranslatorWebService.asmx HTTP/1.1Host: www.webxml.com.cnContent-Type: application/soap+xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">  <soap12:Body>    <getEnCnTwoWayTranslator xmlns="http://WebXml.com.cn/">      <Word>string</Word>    </getEnCnTwoWayTranslator>  </soap12:Body></soap12:Envelope>HTTP/1.1 200 OKContent-Type: application/soap+xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">  <soap12:Body>    <getEnCnTwoWayTranslatorResponse xmlns="http://WebXml.com.cn/">      <getEnCnTwoWayTranslatorResult>        <string>string</string>        <string>string</string>      </getEnCnTwoWayTranslatorResult>    </getEnCnTwoWayTranslatorResponse>  </soap12:Body></soap12:Envelope>


代码如下:

private static void getEnCnTwoWayTranslator(String word) {try {String soapBindingAddress = "http://webservice.webxml.com.cn/webservices/TranslatorWebService.asmx";ServiceClient sender = new ServiceClient();EndpointReference endpointReference = new EndpointReference(soapBindingAddress);Options options = new Options();options.setAction("http://WebXml.com.cn/getEnCnTwoWayTranslator");options.setTo(endpointReference);sender.setOptions(options);OMFactory fac = OMAbstractFactory.getOMFactory();OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/","getEnCnTwoWayTranslator");OMElement data = fac.createOMElement("getEnCnTwoWayTranslator",omNs);String[] strs = new String[] { "Word" };String[] val = new String[] { word };for (int i = 0; i < strs.length; i++) {OMElement inner = fac.createOMElement(strs[i], omNs);inner.setText(val[i]);data.addChild(inner);}OMElement result = sender.sendReceive(data);System.out.println(result.toString());} catch (AxisFault ex) {ex.printStackTrace();}}

调用:

getEnCnTwoWayTranslator("你好");
getEnCnTwoWayTranslator("hello");


结果:

<getEnCnTwoWayTranslatorResponse xmlns="http://WebXml.com.cn/"><getEnCnTwoWayTranslatorResult><string>你好: [ n&amp;#464 h&amp;#462o ]</string><string>1. hello | 
2. how are you |</string></getEnCnTwoWayTranslatorResult></getEnCnTwoWayTranslatorResponse>
<getEnCnTwoWayTranslatorResponse xmlns="http://WebXml.com.cn/"><getEnCnTwoWayTranslatorResult><string>hello: [ 'hel&amp;#601u, he'l&amp;#601u ]</string><string>int.(见面打招呼或打电话用语)喂,哈罗 | 
词形变化:异体字:hullo 名词复数:hellos 动词过去式:helloed 过去分词:helloed 现在分词:helloing 第三人称单数:helloes  |</string></getEnCnTwoWayTranslatorResult></getEnCnTwoWayTranslatorResponse>

0 0