WebService learning summary (thirteen) - the way to call webservice (other ways)

1. Use ajax call
var xhr;
function invoke(){
    if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        xhr = new XMLHttpRequest();
    }
    //Specify the request address
    var url = "http://127.0.0.1:7777/hello?wsdl";
    //Define request type and address and asynchronous
    xhr.open("POST", url, true);
    //Set Content-Type
    xhr.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
    // Specify the callback method
    xhr.onreadystatechange = back;


    var textVal = document.getElementById("mytext").value;
    //Assemble the data of the message body
    var data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://server.hm.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
    +'<soapenv:Body>'
    +'<q0:sayHello>'
    +'<arg0>'+textVal+'</arg0>'
    +'</q0:sayHello>'
    +'</soapenv:Body>'
    +'</soapenv:Envelope>';
    xhr.send(data);


}
function back(){
    if(xhr.readyState == 4){
        if(xhr.status == 200){
            var doc = xhr.responseXML;
            alert(doc);
            alert(xhr.responseText);
            var tag = doc.getElementsByTagName("return")[0];
            alert(day)


        }
    }
}
2. Called by URLConnection
//create url address
URL url = new URL("http://192.168.1.104:8080/hello");
//open connection
URLConnection conn = url.openConnection();
//Convert to HttpURL
HttpURLConnection httpConn = (HttpURLConnection) conn;
//Turn on the input and output switch
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
//set the request method
httpConn.setRequestMethod("POST");
//Set the header information of the request
httpConn.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
//splicing request message
String data = "<soapenv:Envelope xmlns:soapenv=" +
        "\"http://schemas.xmlsoap.org/soap/envelope/\" " +
        "xmlns:q0=\"http://server.rl.com/\" " +
        "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
        "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
        +"<soapenv:Body>"
        +"<q0:sayHello>"
        +"<arg0>renliang</arg0> "
      +"</q0:sayHello>"
      +"</soapenv:Body>"
      +"</soapenv:Envelope>";
//get the output stream
OutputStream out = httpConn.getOutputStream();
//send data
out.write(data.getBytes());
// judge the request is successful
if(httpConn.getResponseCode() == 200){
    //get the input stream
    InputStream in = httpConn.getInputStream();
    //Use the buffer of the input stream
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuffer sb = new StringBuffer();
    String line = null;
    //read input stream
    while((line = reader.readLine()) != null){
        sb.append(line);
    }
    //Create a reader for sax
    SAXReader saxReader = new SAXReader();
    //create document object
    Document doc = saxReader.read(new StringReader(sb.toString()));
    //Get the request response return element
    List<Element> eles = doc.selectNodes("//return");
    for(Element he : they){
        System.out.println(ele.getText());
    }
}
3. Use jquery to call cxf
$(function(){
        $("#mybutton").click(function(){
            var data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://server.web.cxf.rl.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
                  +'<soapenv:Body>'
                  +'<q0:sayHello>'
                  +'   <arg0>sss</arg0>'
                  +' </q0:sayHello>'
                  +'</soapenv:Body>'
                  +'</soapenv:Envelope>';


                $.ajax({
                    url:'http://localhost:8080/cxf-web-server/services/hello',
                    type:'post',
                    dataType:'xml',
                    contentType:'text/xml;charset=UTF-8',
                    date: date,
                    success:function(responseText){
                        alert($(responseText).find('return').text());
                    },
                    error:function(){
                        alert("error");
                    }
                })
        })
    })


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324732173&siteId=291194637