两种方式实现公网webservice的调用

第一种方式:

使用wsimport -p cn.hct.mobile -s . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl 生产客户端代码

编写代码调用:

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.hct.webservice;
 
/**
 * @作者: HCT
 * @时间:2017年3月3日上午10:41:59
 * @描述:
 *
 */
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
 
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
 
import cn.hct.mobile.MobileCodeWSSoap;
 
public class ServiceQuery {
     
        public static void main(String[] args) throws IOException {
            //创建WSDL的URL,注意不是服务地址
            URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
             
            //创建服务名称
            //1.namespaceURI - 命名空间地址
            //2.localPart - 服务视图名
            QName qname = new QName("http://WebXml.com.cn/", "MobileCodeWS");
             
            //创建服务视图
            //参数解释:
            //1.wsdlDocumentLocation - wsdl地址
            //2.serviceName - 服务名称
            Service service = Service.create(url, qname);
            //获取服务实现类
            MobileCodeWSSoap mobileCodeWSSoap = service.getPort(MobileCodeWSSoap.class);
            //调用查询方法
            String result = mobileCodeWSSoap.getMobileCodeInfo("18221137382", "");
            System.out.println(result);
        }
     
 
}

  

第二中方式:

使用HttpURLConnection方式调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.hct.webservice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
 
public class UrlHttp {
 
    public static void main(String[] args) throws IOException {
        //第一步:创建服务地址,不是WSDL地址
        URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
        //第二步:打开一个通向服务地址的连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //第三步:设置参数
        //3.1发送方式设置:POST必须大写
        connection.setRequestMethod("POST");
        //3.2设置数据格式:content-type
        connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
        //3.3设置输入输出,因为默认新创建的connection没有读写权限,
        connection.setDoInput(true);
        connection.setDoOutput(true);
 
        //第四步:组织SOAP数据,发送请求
        String soapXML = getXML("18221137382");
        OutputStream os = connection.getOutputStream();
        os.write(soapXML.getBytes());
        //第五步:接收服务端响应,打印
        int responseCode = connection.getResponseCode();
        if(200 == responseCode){//表示服务端响应成功
            InputStream is = connection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
             
            StringBuilder sb = new StringBuilder();
            String temp = null;
            while(null != (temp = br.readLine())){
                sb.append(temp);
            }
            System.out.println(sb.toString());
             
            is.close();
            isr.close();
            br.close();
        }
 
        os.close();
    }
    public static String getXML(String phoneNum){
        String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
        +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            +"<soap:Body>"
            +"<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"
                +"<mobileCode>"+phoneNum+"</mobileCode>"
              +"<userID></userID>"
            +"</getMobileCodeInfo>"
          +"</soap:Body>"
        +"</soap:Envelope>";
        return soapXML;
    }
}

  

 

posted @   hct118  阅读(366)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示