使用ajax和urlconnection方式調用webservice服務

來源:互聯網
上載者:User

標籤:nec   element   exception   lips   author   ring   round   myeclips   state   

<html>    <head>        <title>使用ajax方式調用webservice服務</title>        <script>            var xhr = new XMLHttpRequest();            function sendAjax(){                var url = "http://192.168.13.66:8080/hello";//webservice服務的地址                var requestBody = ‘<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ‘ + ‘xmlns:q0="http://service.demo.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ‘ + ‘xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">‘+                ‘<soapenv:Body><q0:sayHi> <arg0>xiaoming</arg0></q0:sayHi></soapenv:Body></soapenv:Envelope>‘;//構造請求體,符合soap協議規範                //開啟串連                xhr.open("POST",url,true);                //重新佈建要求頭                xhr.setRequestHeader("content-type","text/xml;charset=UTF-8");                //指定回呼函數                xhr.onreadystatechange = _back;                //發送請求                xhr.send(requestBody);            }            //定義回呼函數            function _back(){                if(xhr.readyState == 4){                    if(xhr.status == 200){                        //擷取伺服器的響應資料                        var ret = xhr.responseXML;                        var ele = ret.getElementsByTagName("return")[0];                        alert(ele.textContent);                    }                }            }        </script>    </head>    <body>        <input type="button" value="使用ajax方式調用webservice服務" onclick="sendAjax();">    </body></html>
requestBody 請求體,可以通過myeclipse內建外掛程式,點擊go以後,下邊的請求體就是我們想要的格式。


二,使用urlconnection方式
package cn.demo.client.urlconnection;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;/** * 使用urlconnection方式調用webservice服務 * @author zhaoqx * */public class App {    public static void main(String[] args) throws Exception {        URL wsUrl = new URL("http://192.168.13.66:8080/hello");        HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();                //構造請求體        String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +                                 "xmlns:q0=\"http://service.demo.cn/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "+                                "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"+                                "<soapenv:Body><q0:sayHi><arg0>xiaoming</arg0></q0:sayHi></soapenv:Body></soapenv:Envelope>";        //佈建要求的參數        conn.setDoInput(true);        conn.setDoOutput(true);        conn.setRequestMethod("POST");        conn.setRequestProperty("content-type", "text/xml;charset=UTF-8");                //向服務端寫資料        conn.getOutputStream().write(requestBody.getBytes());                int responseCode = conn.getResponseCode();        if(responseCode == 200){            //使用輸入資料流擷取服務端響應資料            InputStream in = conn.getInputStream();            byte[] b = new byte[1024];            int len = 0;            StringBuffer sb = new StringBuffer();            while((len = in.read(b)) != -1){                String s = new String(b, 0, len,"UTF-8");                sb.append(s);            }            System.out.println(sb.toString());            in.close();        }            }}

 

使用ajax和urlconnection方式調用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.