JAVA核心知识点--使用HttpURLConnection通过Post方式访问webservice接口

本文以获取天气预报webservice接口为例,使用HttpURLConnection通过发送SOAP消息格式内容来请求webservice接口。

天气预报接口地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 

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);
	}
}

执行程序,控制台打印结果。  

直辖市特别行政区黑龙江吉林辽宁内蒙古河北河南山东山西江苏安徽陕西宁夏甘肃青海湖北湖南浙江江西福建贵州四川广东广西云南海南新疆西藏台湾亚洲欧洲非洲北美洲南美洲大洋洲

你可能感兴趣的:(JAVA核心知识点)