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 ...
随机推荐
- (转)用eclipse创建一个j2ee的web工程后,左面projects窗口中的项目如何没有显示webRoot文件夹,除了src的文件夹,其他都不显示
左边目录窗口的右上方,有个向下的箭头,点里面的filters,把所有的勾都去掉看看
- 增加Android模拟器的内存
1,在window中,打开'C:\Users\Administrator\.android\avd\4.4.2.avd\config.ini'文件(我的是win7,xp的貌似不是'Users',是'D ...
- Swift - 35 - 使用闭包简化语法
//: Playground - noun: a place where people can play import UIKit // 初始化一个整数数组 var arr = [1, 3, 5, 7 ...
- 使用 pm2 来守护 NoderCMS
pm2 是一个带有负载均衡功能的Node应用的进程管理器,使用 pm2 可以帮助你守护和监控 NoderCMS 的正常运行, 基于Node.js+MongoDB的轻量级内容管理系统NoderCMS ...
- 单点登录CAS使用记(八):使用maven的overlay实现无侵入的改造CAS
前期在学习CAS部署的过程中,都是网上各种教程,各种方案不停的尝试. 期间各种侵入改源码,时间久了,改了哪个文件,改了哪段配置,增加了哪段代码,都有可能混淆不清了. 而且最大的问题是,万一换个人来维护 ...
- storm-kafka教程
一.原理介绍 本文内容参考:https://github.com/apache/storm/tree/master/external/storm-kafka#brokerhosts (一)使用st ...
- 【回忆1314】第一次用AngularJS
1.创建指令的4种方式(ECMA) var appModule = angular.module('app', []); appModule.directive('hello', function() ...
- 浮点数比较问题(float x 与 '零值'比较)
今天在牛客网上看到一道面试题,看完之后着实吃了一惊,自己平常都没有在意,看似简单的问题,实则考验了语言的基本功. 据说这是腾讯的面试题: float x 与“零值”比较的if语句为? if (x == ...
- ng-class的使用
例如: td(ng-class="{0:'text-warning',1:'text-primary'}[bj.flag]") {{bj.flag | bjFlagfilter}} ...
- 浅析Android中的消息机制
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...