Java 調(diào)用天氣Webservice詳解及實(shí)例代碼
Java調(diào)用天氣Webservice的小應(yīng)用
廢話不多說(shuō),直接貼代碼:
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");
//發(fā)送數(shù)據(jù)
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);
//創(chuàng)建soapmessage對(duì)象
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");
//發(fā)送數(shù)據(jù)
soapMessage.writeTo(ous);
// soapMessage.writeTo(System.out);
System.out.println(urlconn.getResponseCode());
System.out.println(urlconn.getResponseMessage());
//接收數(shù)據(jù)
ins=urlconn.getInputStream();
//接收的數(shù)據(jù)需要解組?
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{
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Java編程中的防轉(zhuǎn)義和轉(zhuǎn)義技巧匯總
在編程過(guò)程中,我們常常需要處理特殊字符和特定上下文,以確保生成的內(nèi)容在正確的環(huán)境中能夠被解析和顯示,本文將介紹一些常見的防轉(zhuǎn)義或者轉(zhuǎn)義處理的編程技巧,需要的可以參考一下2023-07-07
Springboot整合Flowable6.x導(dǎo)出bpmn20的步驟詳解
這篇文章主要介紹了Springboot整合Flowable6.x導(dǎo)出bpmn20,Flowable流程引擎可用于部署B(yǎng)PMN 2.0流程定義,可以十分靈活地加入你的應(yīng)用/服務(wù)/構(gòu)架,本文給出兩種從flowable導(dǎo)出流程定義bpmn20.xml的方式,需要的朋友可以參考下2023-04-04
SpringBoot配置SSL同時(shí)支持http和https訪問(wèn)實(shí)現(xiàn)
本文主要介紹了SpringBoot配置SSL同時(shí)支持http和https訪問(wèn)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Spring整合Mybatis具體代碼實(shí)現(xiàn)流程
這篇文章主要介紹了Spring整合Mybatis實(shí)操分享,文章首先通過(guò)介紹Mybatis的工作原理展開Spring整合Mybatis的詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-05-05
SpringBoot 插件化開發(fā)模式詳細(xì)總結(jié)
插件化開發(fā)模式正在很多編程語(yǔ)言或技術(shù)框架中得以廣泛的應(yīng)用實(shí)踐,大大提升了系統(tǒng)的擴(kuò)展性和伸縮性,也拓展了系統(tǒng)整體的使用價(jià)值,那么為什么要使用插件呢,本文就詳細(xì)介紹SpringBoot 插件化開發(fā)模式,感興趣的同學(xué)可以參考下2023-06-06
java發(fā)送get請(qǐng)求和post請(qǐng)求示例
這篇文章主要介紹了java發(fā)送get請(qǐng)求和post請(qǐng)求示例,需要的朋友可以參考下2014-03-03
詳解Java中Dijkstra(迪杰斯特拉)算法的圖解與實(shí)現(xiàn)
Dijkstra(迪杰斯特拉)算法是典型的單源最短路徑算法,用于計(jì)算一個(gè)節(jié)點(diǎn)到其他所有節(jié)點(diǎn)的最短路徑。本文將詳解該算法的圖解與實(shí)現(xiàn),需要的可以參考一下2022-05-05
關(guān)于protected修飾符詳解-源于Cloneable接口
這篇文章主要介紹了protected修飾符詳解-源于Cloneable接口,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11

