废话不多说,直接贴代码:

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的小应用的更多相关文章

  1. Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法 转

    Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法   同事遇到一个很囧的问题,java调,netwebservice的时候,调用无参数方法成功,调用有参 ...

  2. java调用C# webService发布的接口

    java调用C# webService发布的接口 java调用C# webService方式有很多种我这里只介绍一种 首先需要引入axis的jar包 axis的maven坐标如下 <depend ...

  3. Java调用.NET webservice方法的几种方式

    最近做项目,涉及到web-service调用,现学了一个星期,现简单的做一个小结.下面实现的是对传喜物流系统(http://vip.cxcod.com/PodApi/GetPodStr.asmx?ws ...

  4. JAVA调用.NET WebService终极方案(包含对SoapHeader的处理)

    一.前言:      今日部门的产品需要用到短信功能,需要走公司统一的接口,而该短信接口是由.net开发的,利用两天时间彻底搞定了用java来调用.net 的web service,包括对soap h ...

  5. java调用 C# webservice接口

    java调用webservice接口方式有多种,本人很懒,测试一种满足我的需求,故为试验其他方法,仅供参考 一:工具 MyEclipse,C#编码发布的webservice接口 二:步骤 1.打开my ...

  6. java调用CXF WebService接口的两种方式

    通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...

  7. Axis2 java调用.net webservice接口的问题(郑州就维)

    这是一个古老的问题,古老到从我若干年前遇到这样的问题就是一个解决之道:反复尝试.其实标准是什么,标准就是一个束缚,一种按既定规则的束缚,错点点,你的调用就可能不成功,不成功后你要花费大量的力气查找原因 ...

  8. Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法

    同事遇到一个很囧的问题,java调,netwebservice的时候,调用无参数方法成功,调用有参数的方法每次我这边的webservice日志都记录参数为空,而我自己.Net程序调用完全没有问题,后面 ...

  9. java 调用 .net webservice

    1.首先下载Axis2工具包 2.解压之后用cmd命令进入bin目录WSDL2Java.bat -uri http://192.168.20.42:9999/LoginService.asmx?wsd ...

随机推荐

  1. ASP.NET 页面间数据传递的方法

    在做WEB开发时,很多地方会涉及到页面间的数据传递.这几天在完善教务基础系统,遇到了这个问题,上网查了一些资料,现总结如下: 说到页面间数据传递,很多人都会想到通过像Session这样的全局变量,但是 ...

  2. IIS应用程序池数目

    今天突然遇到一个问题,访问线上站点,数据处理过之后,访问页面有时是404,有时不是404,而404页面刷新几次之后就正常了. 经过大神们指点之后,发现竟然是应用程序池惹的祸. 分析下原因,在代码里面数 ...

  3. C#之重定向输入输出

    当我们写完程序,想要在另一个平台上跑我们所写的程序的时候,就需要用到重定向输入输出. 重定向有两中方式,即同步和异步. 下面来讲讲同步 代码: Process process = new Proces ...

  4. 武汉科技大学ACM :1003: A+B for Input-Output Practice (III)

    Problem Description Your task is to Calculate a + b. Input Input contains multiple test cases. Each ...

  5. sql 当重复的数据有多条时,保留一条,删除其他重复

    delete from proj_info where   newcode in (select newcode    from proj_info group by newcode      hav ...

  6. 数据库和Doctrine(转载自http://www.111cn.net/phper/332/85987.htm)

    对于任何应用程序来说最为普遍最具挑战性的任务,就是从数据库中 读取和持久化数据信息.尽管symfony完整的框架没有默认集成ORM,但是symfony标准版,集成了很多程序,还自带集成了Doctrin ...

  7. php中magic_quotes_gpc函数详解

    magic_quotes_gpc函数在php中的作用是判断解析用户提示的数据,如包括有:post.get.cookie过来的数据增加转义字符“\”,以确保这些数据不会引起程序,特别是数据库语句因为特殊 ...

  8. Swagger 生成 ASP.NET Web API

    使用 Swagger 生成 ASP.NET Web API 在线帮助测试文档 原文:ASP.NET Web API Help Pages using Swagger作者:Shayne Boyer翻译: ...

  9. START WITH CONNECT BY PRIOR 链表查询

    使用场景:排序 设计思路,id为主键,index为顺序,index存前一个节点的id,当然,按照这个思路可以实现双向链表的(preindex存前一个节点,nextindex存下一个节点) 这样的话排序 ...

  10. ubuntu设置系统时间与网络时间同步

    ubuntu设置系统时间与网络时间同步   Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC).   系统时间:指当前Linux Ker ...