WebService學(xué)習(xí)筆記(一)

一、基礎(chǔ)進(jìn)階

  1. httpclient調(diào)用WebService
package com.demo;

import com.midea.gls.MobileCodeWS;
import com.midea.gls.MobileCodeWSSoap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* Author: huyi
 * \* Date: 2017/2/17 22:15
 * \* Description:
 * \
 */
public class WSHttpClient {



    public static void main(String[] args) {
        String mobileCode = "18680586568";

       // get(mobileCode);
//        post(mobileCode);

        soap(mobileCode);
    }

    public static void get(String mobileCode){
        HttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://ws.webxml.com.cn//WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+mobileCode+"&userID=");
        try {
            HttpResponse response = client.execute(httpGet);
            System.out.println("executing request " + httpGet.getURI());
            // 獲取響應(yīng)實(shí)體
            HttpEntity entity = response.getEntity();
            System.out.println("--------------------------------------");
            // 打印響應(yīng)狀態(tài)
            System.out.println(response.getStatusLine());
            if (entity != null) {
                // 打印響應(yīng)內(nèi)容長(zhǎng)度
                System.out.println("Response content length: " + entity.getContentLength());
                // 打印響應(yīng)內(nèi)容
                System.out.println("Response content: " + EntityUtils.toString(entity));
            }
            System.out.println("------------------------------------");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void post(String mobileCodeVal){
        HttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://ws.webxml.com.cn//WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
        try {
            //設(shè)置參數(shù)
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            list.add(new BasicNameValuePair("mobileCode",mobileCodeVal));
            list.add(new BasicNameValuePair("userID",""));
            UrlEncodedFormEntity requestEntiry = new UrlEncodedFormEntity(list);
            httpPost.setEntity(requestEntiry);
            HttpResponse response = client.execute(httpPost);
            System.out.println("executing request " + httpPost.getURI());
            // 獲取響應(yīng)實(shí)體
            HttpEntity httpEntity = response.getEntity();
            System.out.println("--------------------------------------");
            // 打印響應(yīng)狀態(tài)
            System.out.println(response.getStatusLine());
            if (httpEntity != null) {
                // 打印響應(yīng)內(nèi)容長(zhǎng)度
                System.out.println("Response content length: " + httpEntity.getContentLength());
                // 打印響應(yīng)內(nèi)容
                System.out.println("Response content: " + EntityUtils.toString(httpEntity));
            }
            System.out.println("------------------------------------");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void soap(String mobileVal){
        MobileCodeWS mobileCodeWS = new MobileCodeWS();
        MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();
        String address = mobileCodeWSSoap.getMobileCodeInfo(mobileVal, null);
        System.out.println(address);
    }
}
  1. wsimport調(diào)用WSDL
wsimport -s . -p com.midea.gls http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL

找到WSDL中關(guān)于service的調(diào)用

    public static void soap(String mobileVal){
        MobileCodeWS mobileCodeWS = new MobileCodeWS();
        MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();
        String address = mobileCodeWSSoap.getMobileCodeInfo(mobileVal, null);
        System.out.println(address);
    }

二 、 發(fā)布自己的WSDL

發(fā)布自己的WSDL時(shí)候,需要注意,地址欄后面要自己加?WSDL

package com.demo;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

/**
 * \* Created with IntelliJ IDEA.
 * \* Author: huyi
 * \* Date: 2017/2/20 13:32
 * \* Description:
 * \
 */
@WebService
public class MyWS {

    public String sayHello(String name) {
        String reVal = name + ", 你好!";
        System.out.println(reVal);
        return reVal;
    }

    public static void main(String[] args) {
        String address = "http://10.74.96.29/ws";
        Endpoint.publish(address , new MyWS());
        System.out.println("發(fā)布的地址是:"+address);
    }
}

訪(fǎng)問(wèn)http://10.74.96.29/ws?WSDL,可以見(jiàn)到

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://demo.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="MyWSService" targetNamespace="http://demo.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://demo.com/" elementFormDefault="unqualified" targetNamespace="http://demo.com/" version="1.0">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="MyWS">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello"></wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="MyWSServiceSoapBinding" type="tns:MyWS">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="MyWSService">
<wsdl:port binding="tns:MyWSServiceSoapBinding" name="MyWSPort">
<soap:address location="http://10.74.96.29/ws"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

三 、客戶(hù)端訪(fǎng)問(wèn)

  1. 首先在CMD中調(diào)用wsimport工具生成本地WebService代碼
wsimport -s . -p com.ws http://10.74.96.29/ws?WSDL
  1. 導(dǎo)入代碼到本地項(xiàng)目后,直接調(diào)用即可
    public static void soap2(String mobileVal){
        MyWS mobileCodeWS = new MyWS();
        String name = "huyi";
        String reVal = mobileCodeWS.sayHello(name);
        System.out.println(reVal);
    }

注意:一個(gè)端口可以發(fā)布多個(gè)Webservice服務(wù)

四 、ajax訪(fǎng)問(wèn)WebService

ajax不能直接訪(fǎng)問(wèn)WebService,因?yàn)榇嬖诳缬虻膯?wèn)題,需要變通一下,通過(guò)ajax調(diào)用工程下的客戶(hù)端訪(fǎng)問(wèn)WebService,客戶(hù)端可以是一個(gè)servlet,或者spring下的controller

ws.jsp

<%--
  Created by IntelliJ IDEA.
  User: huyi
  Date: 2017/2/20
  Time: 15:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>

    <title>$Title$</title>
    <script src="js/jquery/jquery.min.js"></script>
    <script>


        $("#myWs").click(function () {
            console.info(data);
            /**/
            console.info("----2-------");
        })

        function getVal() {
            console.info("-----1---" + $("#myWs").val() + "------");
            var data = {
                name:$("#name").val()
            }

            $.ajax({
                method:'POST',
                url:'http://localhost:8080/ws/myWsServlet?name='+$("#name").val(),
                success:function(result) {
                    console.info("-----3---" + result + "------");
                }
            });
        }

    </script>
</head>
<body>
<input type="text" id="name">
<input type="button" name="調(diào)用WebService" id="myWs" value="調(diào)用WebService" onclick="getVal()">
</body>
</html>

servlet

package com.gls.ws;

import com.demo.*;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * \* Created with IntelliJ IDEA.
 * \* Author: huyi
 * \* Date: 2017/2/20 15:50
 * \* Description:
 * \
 */
public class MyWsServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        com.demo.MyWS mobileCodeWS = new com.demo.MyWS();
        String name = req.getParameter("name");
        String reVal = mobileCodeWS.sayHello(name);
        System.out.println(reVal+"------------");
        resp.getWriter().print(reVal);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

web.xml

    <servlet>
        <servlet-name>myWs</servlet-name>
        <servlet-class>com.gls.ws.MyWsServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myWs</servlet-name>
        <url-pattern>/myWsServlet</url-pattern>
    </servlet-mapping>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類(lèi)相關(guān)的語(yǔ)法,內(nèi)部類(lèi)的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線(xiàn)程的語(yǔ)...
    子非魚(yú)_t_閱讀 34,896評(píng)論 18 399
  • 一、Java基礎(chǔ) 1.寫(xiě)出下面代碼的執(zhí)行結(jié)果 2.寫(xiě)出下面代碼的執(zhí)行結(jié)果 3.寫(xiě)出下面代碼的執(zhí)行結(jié)果 (此題需寫(xiě)出...
    joshul閱讀 577評(píng)論 0 1
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,039評(píng)論 0 11
  • 前不久,一位朋友在閑聊中提到自己孩子正在某知名英語(yǔ)培訓(xùn)機(jī)構(gòu)學(xué)習(xí)英語(yǔ),目前學(xué)習(xí)自然拼讀已經(jīng)一個(gè)多月,孩子在家時(shí)不時(shí)會(huì)...
    正好學(xué)拼讀閱讀 787評(píng)論 0 7
  • 文/添一抹嵐 蘆葦,是我的同桌,在四年級(jí)時(shí)。 那時(shí),周?chē)瑢W(xué)名字里,或梅、蘭,或紅、花。當(dāng)聽(tīng)得蘆葦這名字時(shí),我便先...
    添一抹嵐閱讀 1,071評(píng)論 58 33

友情鏈接更多精彩內(nèi)容