使用HttpURLConnection通过Post方式访问webservice接口
2018-01-26 22:27
477 查看
本文以获取天气预报webservice接口为例,使用HttpURLConnection通过发送SOAP消息格式内容来请求webservice接口。
天气预报接口地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
天气预报接口地址: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( "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" ") .append("xmlns:web=\"http://WebXml.com.cn/\"><soapenv:Header/><soapenv:Body>") .append("<web:getSupportProvince/></soapenv:Body></soapenv:Envelope>"); /* * sb.append( * "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " * ) .append( * "xmlns:web=\"http://WebXml.com.cn/\"><soapenv:Header/><soapenv:Body><web:getSupportCity>" * ) .append( * "<web:byProvinceName>河北</web:byProvinceName></web:getSupportCity></soapenv:Body></soapenv:Envelope>" * ); */ 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); } }执行程序,控制台打印结果。
<?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><getSupportProvinceResponse xmlns="http://WebXml.com.cn/"><getSupportProvinceResult><string>直辖市</string><string>特别行政区</string><string>黑龙江</string><string>吉林</string><string>辽宁</string><string>内蒙古</string><string>河北</string><string>河南</string><string>山东</string><string>山西</string><string>江苏</string><string>安徽</string><string>陕西</string><string>宁夏</string><string>甘肃</string><string>青海</string><string>湖北</string><string>湖南</string><string>浙江</string><string>江西</string><string>福建</string><string>贵州</string><string>四川</string><string>广东</string><string>广西</string><string>云南</string><string>海南</string><string>新疆</string><string>西藏</string><string>台湾</string><string>亚洲</string><string>欧洲</string><string>非洲</string><string>北美洲</string><string>南美洲</string><string>大洋洲</string></getSupportProvinceResult></getSupportProvinceResponse></soap:Body></soap:Envelope>
相关文章推荐
- 使用HttpURLConnection通过POST方式提交请求数据,并上传文件
- android 使用httpURLConnection post方式访问接口的简单工具包
- java 通过Ajax前台传参数 并用 HttpURLConnection Post方式访问对外的接口
- Java 通过HttpURLConnection Post方式提交json,并从服务端返回json数据
- 使用HttpURLConnection进行POST方式提交
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- HttpURLconnection使用POST方式提交JSON数据给服务器
- 使用HttpURLConnection通过GET、POST请求数据
- 使用HttpUrlConnection的POST方式返回响应信息
- Java 通过HttpURLConnection Post方式提交json,并从服务端返回json数据
- Java 通过HttpURLConnection Post方式提交xml,并从服务端返回数据
- 使用HttpURLConnection采用get方式或post方式请求数据
- 使用HttpURLConnection采用Post方式请求数据
- HttpUrlConnection使用Get和Post访问服务器的工具类(一)
- 使用 HTTP 协议访问网络的两种方式:HttpURLConnection 和 HttpClient
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- Java 通过HttpURLConnection Post方式提交xml,并从服务端返回数据
- 做一个程序来查询手机号码的归属地(使用的是HttpURLConnection的post提交方式)
- 使用HttpURLConnection调用url方式访问spring mvc中的controller并传json值与返回
- 使用HttpURLConnection进行POST方式提交