一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - Java 調(diào)用天氣Webservice詳解及實例代碼

Java 調(diào)用天氣Webservice詳解及實例代碼

2020-07-08 13:46java教程網(wǎng) JAVA教程

這篇文章主要介紹了Java 調(diào)用天氣Webservice詳解及實例代碼的相關資料,這里附實例代碼,使用java 調(diào)用webservice 的小應用,需要的朋友可以參考下

Java調(diào)用天氣Webservice的小應用

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

 CityReq.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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對象
      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{
      
    }
  }
  
   
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: xxxxx性欧美| 国产伦精一区二区三区视频 | 日韩视频第二页 | 91大神亚洲影视在线 | 91色资源网在线观看 | 免费国产影视观看网站入口 | jizz农村野外jizz农民 | 国产区成人精品视频 | 精品在线播放 | 女bbbbxxx孕妇 | 国产午夜亚洲精品一区网站 | 日韩欧美国产一区 | 99色在线播放 | 日韩网站在线 | 色一情| 亚洲免费视频在线 | 毛片免费视频观看 | 国产成人亚洲精品一区二区在线看 | 国产视频一二三区 | 欧美日韩1区2区 | 97大香伊在人人线色 | 婚色阿花在线全文免费笔 | 青青在线香蕉国产精品 | 三星w699| 亚洲精品久久啪啪网站成年 | 国产欧美久久一区二区 | 91精品综合久久久久m3u8 | 亚洲欧美天堂 | 国产精品亚洲午夜一区二区三区 | 男人的天堂日本 | 风间由美被义子中文字幕 | 亚州在线视频 | 日本精a在线观看 | 亚洲国产精品线在线观看 | 国产欧美一区二区三区免费 | 免费港剧在线观看港剧 | 成人动漫影院 | 性色AV乱码一区二区三区视频 | 精品国语对白精品自拍视 | 九九爱这里只有精品 | 特黄特黄一级高清免费大片 |