Axis調用webservice查看天氣情況(固定地區)

來源:互聯網
上載者:User

1.步驟與查看手機號碼歸屬地部落格是一樣的。

2. 建立一個weatherAxis.java  檔案

 代碼如下:

package com.tools;import org.apache.axiom.om.OMAbstractFactory;import org.apache.axiom.om.OMElement;import org.apache.axiom.om.OMFactory;import org.apache.axiom.om.OMNamespace;import org.apache.axiom.soap.SOAP11Constants;import org.apache.axis2.Constants;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.client.ServiceClient;import org.apache.axis2.transport.http.HTTPConstants;import org.apache.axis2.transport.http.HttpTransportProperties.ProxyProperties;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class WeatherAxis {        private static EndpointReference targetEPR = new  EndpointReference(          "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");      //  private static EndpointReference targetEPR = new EndpointReference(     //   "http://210.43.64.106/Service.asmx");       public void  getResult() throws Exception    {        ServiceClient sender =  new ServiceClient();                  sender.setOptions(buildOptions());        OMElement result = sender.sendReceive(buildParam());                System.out.println(result);    }            private static OMElement buildParam()    {        OMFactory fac = OMAbstractFactory.getOMFactory();          OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");     //  OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", "");         OMElement data = fac.createOMElement("getWeatherbyCityName", omNs);     //   OMElement data = fac.createOMElement("XSCheckPassword", omNs);              OMElement inner = fac.createOMElement("theCityName", omNs);                          //  OMElement inner = fac.createOMElement("xh", omNs);        inner.setText("吉首");             //    OMElement inner2 = fac.createOMElement("xm", omNs);               //   inner.setText("2008198821");                    data.addChild(inner);      //   data.addChild(inner2);                  return data;    }            private static Options buildOptions()    {         Options options = new Options();         options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);         options.setAction("http://WebXml.com.cn/getWeatherbyCityName");           //  options.setAction("http://210.43.64.106/XSCheckPassword");        options.setTo(targetEPR);        //options.setProperty 如果不是通過代理上網,此句可省        //options.setProperty(HTTPConstants.PROXY, buildProxy());        options.setTransportInProtocol(Constants.TRANSPORT_HTTP);        return options;    }    /**     * 本機採用Proxy 伺服器上網時,需要設定代理     * @return     */    public static ProxyProperties buildProxy()    {        ProxyProperties proxyProperties = new ProxyProperties();        proxyProperties.setProxyName("代理名稱");        proxyProperties.setProxyPort(8080);        return proxyProperties;    }    public static void main(String[] args) throws Exception    {        WeatherAxis s = new WeatherAxis();        s.getResult();    }}

 

3.大功告成。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.