啥也不说了,看代码吧,都封装好了
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;
}
}
本文介绍了一种使用Java通过调用WebService接口实现英文到中文翻译的方法。通过构造SOAP请求并发送到指定的WebService,可以获取翻译结果。文章提供了完整的代码示例,包括如何设置HTTP请求头,构建SOAP请求体以及处理响应结果。
760

被折叠的 条评论
为什么被折叠?



