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://www.webxml.com.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); //解组的方式
/* SOAPMessage responseMessage=MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null, ins);
Unmarshaller unmarsh=JAXBContext.newInstance(CityResp.class).createUnmarshaller();
JAXBElement<CityResp> reponse= unmarsh.unmarshal(responseMessage.getSOAPBody().extractContentAsDocument(), CityResp.class);
CityResp uresp= reponse.getValue();
System.out.println(uresp.getResult());*/ ous.close();
ins.close();
urlconn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}finally{ }
} }
Java调用天气Webservice的小应用的更多相关文章
- Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法 转
Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法 同事遇到一个很囧的问题,java调,netwebservice的时候,调用无参数方法成功,调用有参 ...
- java调用C# webService发布的接口
java调用C# webService发布的接口 java调用C# webService方式有很多种我这里只介绍一种 首先需要引入axis的jar包 axis的maven坐标如下 <depend ...
- Java调用.NET webservice方法的几种方式
最近做项目,涉及到web-service调用,现学了一个星期,现简单的做一个小结.下面实现的是对传喜物流系统(http://vip.cxcod.com/PodApi/GetPodStr.asmx?ws ...
- JAVA调用.NET WebService终极方案(包含对SoapHeader的处理)
一.前言: 今日部门的产品需要用到短信功能,需要走公司统一的接口,而该短信接口是由.net开发的,利用两天时间彻底搞定了用java来调用.net 的web service,包括对soap h ...
- java调用 C# webservice接口
java调用webservice接口方式有多种,本人很懒,测试一种满足我的需求,故为试验其他方法,仅供参考 一:工具 MyEclipse,C#编码发布的webservice接口 二:步骤 1.打开my ...
- java调用CXF WebService接口的两种方式
通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...
- Axis2 java调用.net webservice接口的问题(郑州就维)
这是一个古老的问题,古老到从我若干年前遇到这样的问题就是一个解决之道:反复尝试.其实标准是什么,标准就是一个束缚,一种按既定规则的束缚,错点点,你的调用就可能不成功,不成功后你要花费大量的力气查找原因 ...
- Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法
同事遇到一个很囧的问题,java调,netwebservice的时候,调用无参数方法成功,调用有参数的方法每次我这边的webservice日志都记录参数为空,而我自己.Net程序调用完全没有问题,后面 ...
- java 调用 .net webservice
1.首先下载Axis2工具包 2.解压之后用cmd命令进入bin目录WSDL2Java.bat -uri http://192.168.20.42:9999/LoginService.asmx?wsd ...
随机推荐
- pointer-events属性
pointer-events的风格更像JavaScript,它能够: 1.阻止用户的点击动作产生任何效果.阻止缺省鼠标指针的显示3.阻止CSS里的hover和active状态的变化触发事件4.阻止Ja ...
- Centos 5.2安装配置DNS服务器
BIND安装配置(主从)我的系统环境:centos 5.2 作者:哈密瓜 主:我采用的是yum安装[root@linux src]#yum -y install bind* 生成rndc控制命令的ke ...
- uva10020 贪心
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Bencode编码解析的C++实现
Ben编码的基本规则 B编码中有4种类型:字符串.整型.列表.字典. 字符串 字符串的编码格式为:<字符串的长度>:<字符串>,其中<>括号中的内容为必需.例如,有 ...
- rtmp协议介绍
概述: •tcp建立连接. •rtmp握手. •客户端与服务器对建立rtmp连接达成一致. •创建rtmp流 •客户端与服务器对play或者Publish达成一致. •客户端开始传送数据到服务器. • ...
- Sql Server索引(转载)
官方说法: 聚集索引 一种索引,该索引中键值的逻辑顺序决定了表中相应行的物理顺序. 聚集索引确定表中数据的物理顺序.聚集索引类似于电话簿,后者按姓氏排列数据.由于聚集索引规定数据在表中的物理存储顺序, ...
- 按钮点击效果jquery
<html><head> <meta charset="UTF-8"> <title>QQ</title> <me ...
- Nginx源码研究三:Epoll在NGINX中的使用
Web服务器在面对高并发的情况下,网络的IO一般选择IO复用,像apache选择的Select/poll.Nginx在linux 2.6后选择Epoll做网路IO,提高了WEB服务的并发能力. 在本章 ...
- 《python基础教程》笔记之 抽象
创建函数 记录函数,在函数的开头写下字符串,它就会作为函数的一部分进行存储,这称为文档字符串,如 def square(x): 'Caculates the square of the number ...
- linux awk 中 RS,ORS,FS,OFS 区别与联系【转】
linux awk 中 RS,ORS,FS,OFS 区别与联系 http://blog.csdn.net/jesseen/article/details/7992929