Java調用天氣Webservice的小應用

來源:互聯網
上載者:User

標籤:

廢話不多說,直接貼代碼:

 

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的小應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.