时间:2021-05-20
Java调用天气Webservice的小应用
废话不多说,直接贴代码:
CityReq.java
package com.weather;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name="getWeatherbyCityName",namespace="http://WebXml.com.cn/")public class CityReq { private String theCityName; public String getTheCityName() { return theCityName; } @XmlElement(name="theCityName",namespace="http://WebXml.com.cn/") public void setTheCityName(String theCityName) { this.theCityName = theCityName; } }WeatherWebServiceTest.java
package com.weather;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import javax.xml.bind.JAXBContext;import javax.xml.bind.Marshaller;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.soap.MessageFactory;import javax.xml.soap.SOAPBody;import javax.xml.soap.SOAPConstants;import javax.xml.soap.SOAPEnvelope;import javax.xml.soap.SOAPMessage;import org.w3c.dom.Document;public class WeatherWebServiceTest { public static void main(String[] args) { // TODO Auto-generated method stub weather(); } static void weather(){ System.out.println("开始登陆..."); String wsdl="http://.cn/WebServices/WeatherWebService.asmx?wsdl"; System.out.println("wsdl:"+wsdl); HttpURLConnection urlconn=null; InputStream ins=null; OutputStream ous=null; try { URL u=new URL(wsdl); urlconn=(HttpURLConnection)u.openConnection(); urlconn.setDoOutput(true); urlconn.setRequestMethod("POST"); urlconn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8"); //urlconn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); //发送数据 ous=urlconn.getOutputStream(); Document document=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); //编组 Marshaller marsh=JAXBContext.newInstance(CityReq.class).createMarshaller(); CityReq xmlf=new CityReq(); xmlf.setTheCityName("北京"); //JAXB.marshal(xmlf, new PrintWriter(System.out)); marsh.marshal(xmlf, document); //创建soapmessage对象 SOAPMessage soapMessage=MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(); SOAPBody soapBody=soapMessage.getSOAPBody(); soapBody.addDocument(document); SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope(); soapEnvelope.removeNamespaceDeclaration("env"); soapEnvelope.addNamespaceDeclaration("soap12", "http://www.w3.org/2003/05/soap-envelope"); soapEnvelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance"); soapEnvelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema"); soapEnvelope.setPrefix("soap12"); soapEnvelope.removeChild(soapEnvelope.getHeader()); soapBody.setPrefix("soap12"); //发送数据 soapMessage.writeTo(ous); // soapMessage.writeTo(System.out); System.out.println(urlconn.getResponseCode()); System.out.println(urlconn.getResponseMessage()); //接收数据 ins=urlconn.getInputStream(); //接收的数据需要解组? StringBuffer respMsg=new StringBuffer(); byte[] bytes=new byte[1024*1024]; int a=-1; while ((a=ins.read(bytes))!=-1) { respMsg.append(new String(bytes,0,a)); } System.out.println(respMsg.length()); System.out.println(respMsg); //解组的方式 ous.close(); ins.close(); urlconn.disconnect(); } catch (Exception e) { e.printStackTrace(); }finally{ } } }感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
jax-wshandler的详解及简单实例aop技术一般用于某个对象的函数调用的日志,认证等。webservice是远程的函数调用,也需要类似的aop方法,举例
本文实例讲述了Java调用WebService接口的方法。分享给大家供大家参考。具体如下:这里讲述有参方法Add,代码如下:复制代码代码如下:publicsta
Python调用Java实例详解前言:Python对服务器端编程不如Java所以这方面可能要调用Java代码前提:Linux环境1安装jpype1安装后测试代码
一、WebService.asmx 处理业务数据,在GetWhether方法中产生天气情况数据,供JqueryRequest.aspx调用,代码如下:复制代码
Androidksoap调用webservice批量上传多张图片详解这几天一直在开发app,哎呀,什么都是第一接触,想想自己自学Java,然后自学Android