Java implementation and invocation of Web Service

Source: Internet
Author: User
Tags soap wsdl

A. Web Service

1. Web Service

is a cross-language call between applications

For example, the weather forecast web Service:http://www.webxml.com.cn/webservices/weatherwebservice.asmx

2. Wsdl:web Service Description Language (Web Service Description Language)

The XML format describes how the called address method is called, and can be seen as a webservice instruction

For example, the weather forecast for: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

3. Soap:simple Object Access Protocol (Simple Object Accessing protocol)

SOAP transmits XML data based on HTTP (because there is a request body, so it must be a POST request)
The format of the request and response XML is as follows:

<Envelop>
<body>
//....
</body>
</Envelop>

Two. Java implementation and invocation of Web Service

1. At present, the more well-known WebService framework has roughly four jws,axis,xfire and CXF. (Server-side publishing, client invocation)

For specific reference: fast implementation of Jws-webservice and Axis2-webservice

2. Call WebService via URL connection (this method is too cumbersome for client calls)

Import Java.io.inputstream;import java.io.outputstream;import java.net.httpurlconnection;import java.net.URL;/** * Call WebService service via URLConnection * */public class App {public static void main (string[] args) throws Exception {/                /service address URL wsurl = new URL ("Http://192.168.1.100:6789/hello");                HttpURLConnection conn = (httpurlconnection) wsurl.openconnection ();        Conn.setdoinput (TRUE);        Conn.setdooutput (TRUE);        Conn.setrequestmethod ("POST");                Conn.setrequestproperty ("Content-type", "text/xml;charset=utf-8");                OutputStream OS = Conn.getoutputstream (); Request Body String soap = "<soapenv:envelope xmlns:soapenv=\" http://schemas.xmlsoap.org/soap/envelope/\ "xmlns:q0=\" HT Tp://ws.itcast.cn/\ "xmlns:xsd=\" http://www.w3.org/2001/xmlschema\ "xmlns:xsi=\" http://www.w3.org/2001/ Xmlschema-instance\ ">" + "<soapenv:Body> <q0:sayHello><arg0>aaa</arg0> & Lt;/q0:sayhello> </soapenv:Body> </soapenv:Envelope> ";                Os.write (Soap.getbytes ());                InputStream is = Conn.getinputstream ();        Byte[] B = new byte[1024];        int len = 0;        String s = "";            while (len = Is.read (b))! =-1) {string ss = new String (B,0,len, "UTF-8");        s + = SS;                } System.out.println (s);        Is.close ();        Os.close ();    Conn.disconnect (); }}

3. Call WebService with KSOAP2 (client call)

Used primarily for resource-constrained Java environments such as applets or J2ME applications, as well as Android, and so on (CLDC/CDC/MIDP).

Refer to: Use KSOAP2 call WebService (instance: Invoke weather forecast service)

Java implementation and invocation of Web Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.