本文以获取天气预报webservice接口为例,使用HttpURLConnection通过发送SOAP消息格式内容来请求webservice接口。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUtil {
public static String sendSoapPost(String url, String xml,
String contentType, String soapAction) {
String urlString = url;
HttpURLConnection httpConn = null;
OutputStream out = null;
String returnXml = '';
try {
httpConn = (HttpURLConnection) new URL(urlString).openConnection();
httpConn.setRequestProperty('Content-Type', contentType);
if (null != soapAction) {
httpConn.setRequestProperty('SOAPAction', soapAction);
}
httpConn.setRequestMethod('POST');
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
out = httpConn.getOutputStream(); // 获取输出流对象
httpConn.getOutputStream().write(xml.getBytes('UTF-8')); // 将要提交服务器的SOAP请求字符流写入输出流
out.flush();
out.close();
int code = httpConn.getResponseCode(); // 用来获取服务器响应状态
String tempString = null;
StringBuffer sb1 = new StringBuffer();
if (code == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(httpConn.getInputStream(),
'UTF-8'));
while ((tempString = reader.readLine()) != null) {
sb1.append(tempString);
}
if (null != reader) {
reader.close();
}
} else {
BufferedReader reader = new BufferedReader(
new InputStreamReader(httpConn.getErrorStream(),
'UTF-8'));
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
sb1.append(tempString);
}
if (null != reader) {
reader.close();
}
}
// 响应报文
returnXml = sb1.toString();
} catch (Exception e) {
e.printStackTrace();
}
return returnXml;
}
public static void main(String[] args) {
String url = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx';
StringBuilder sb = new StringBuilder('');
sb.append(
'')
.append(' ');
/*
* sb.append(
* ''
* ) .append(
* '河北 '
* );
*/
String dataXml = sb.toString();
String contentType = 'text/xml; charset=utf-8';
String soapAction = 'http://WebXml.com.cn/getSupportProvince';
// String soapAction =
// ''document/http://pengjunnlee.com/CustomUI:GetWeatherById'';
String resultXml = HttpUtil.sendSoapPost(url, dataXml, contentType,
soapAction);
System.out.println(resultXml);
}
}
执行程序,控制台打印结果。
直辖市 特别行政区 黑龙江 吉林 辽宁 内蒙古 河北 河南 山东 山西 江苏 安徽 陕西 宁夏 甘肃 青海 湖北 湖南 浙江 江西 福建 贵州 四川 广东 广西 云南 海南 新疆 西藏 台湾 亚洲 欧洲 非洲 北美洲 南美洲 大洋洲
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点