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 ...
随机推荐
- JS相关链接
给开发者提供的 35 款 JavaScript 图形图表库: http://news.cnblogs.com/n/201518/ 主题:[前端必看]JavaScript推荐资料合集: http://w ...
- sublime开启php自动代码补全
配置文件添加 "auto_complete_selector": "source, text", 配置文件打开方式:preferences->settin ...
- uva 484 - The Department of Redundancy Department
已有的数据结构装不下数据,或者不能处理现有的数据,那就必须要思考其他的辅助手段,辅助结构: #include <cstdio> #include <map> #include ...
- javascript模块
//使用模块 template = {}; template.createObj = (function(){ //私有成员 var _age = ""; var _name = ...
- C#操作Excel开发报表系列整理(转)
C#操作Excel进行报表开发系列共写了七篇,也已经有很久没有新东西了,现在整理一下,方便以后查阅,如果有写新的,会同时更新.需要注意的是因为Office的版本不同,实际的代码可能会有所不同,但是都是 ...
- JS-商品图片点击轮换
//小图预览区域图片轮换键const LIWIDTH=62;var moveCount=0;document.getElementById("btForward").onclick ...
- ServerInfo.INI解密
[GlobalInfo]LastServerName=000781ED2D127FBA074D97444DC82F216443034E66BB341A428B14E326A656B9LastServe ...
- python 基础,包括列表,元组,字典,字符串,set集合,while循环,for循环,运算符。
1.continue 的作用:跳出一次循环,进行下一次循环 2.break 跳出不再循环 3.常量 (全是大写)NAME = cjk 一般改了会出错 4.py ...
- Moving Acerage
http://zh.wikipedia.org/zh/%E7%A7%BB%E5%8B%95%E5%B9%B3%E5%9D%87
- 一道考验你设计能力的C++编程题
http://www.cppblog.com/weiym/archive/2012/06/12/178472.html