加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice

发布时间:2020-12-16 21:36:00 所属栏目:安全 来源:网络整理
导读:服务端 代码如下,控制台打
  • 服务端
    代码如下,控制台打印”ServiceTestStart”,浏览器访问http://localhost:8080/MyService?wsdl,出现xml标志成功。
package webservce;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;


@WebService
public class ServceTest { public String getMessage(String name) { return name+"你好"; } public static void main(String[] args) { Endpoint.publish("http://localhost:8080/MyService",new ServceTest());//发布服务 System.out.println("ServiceTestStart"); } } 
  • 客户端
    E:eclipseworkspaceAdvancesrc 为项目路径,webservceclient 为项目下的包。cmd执行命令后,客户端相关java代码生成在E:eclipseworkspaceAdvancesrcwebservceclient目录下。特别注意执行该命令把服务起来,否则会报 failed.noservice=在提供的 WSDL 中找不到 wsdl:service:。出现正在生成代码…正在编译代码…成功。
wsimport -s E:eclipseworkspaceAdvancesrc -p webservceclient -keep http://localhost:8080/MyService?wsdl
  • 测试
    我测试和服务端写同一目录下了,注意ServceTest是接口。打印出”sinaihalo你好”,测试成功。
package webservce;

import webservceclient.ServceTestService;

public class ClientTest { public static void main(String[] args) { webservceclient.ServceTest serviceTest = new ServceTestService().getServceTestPort();//初始化对象 String name = serviceTest.getMessage("sinaihalo");//调用服务端方法 System.out.println(name);//打印返回结果 } } 
  • httpclient方式测试
    首先导httpclient的jar包,这种方式自己拼请求,运行结果:
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:getMessageResponse xmlns:ns2="http://webservce/"><return>sinaihalo你好</return></ns2:getMessageResponse></S:Body></S:Envelope>
package com.ultrapower.nettech.obm.server;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class TestWebService { public static void main(String[] args) throws Exception { Map<String,String> map = new HashMap<String,String>(); //拼接xml请求,带有请求头 String soapRequestData = "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservce/">"
                +"<soapenv:Header/>"
                +"<soapenv:Body>"
                +"<web:getMessage>"
                +" <!--Optional:-->"
                +"<arg0>sinaihalo</arg0>"
                +"</web:getMessage>"
                +"</soapenv:Body>"
                +"</soapenv:Envelope>";

        try {
            String method = "http://localhost:8080/MyService";//比如http://192.177.222.222:8888/services/Service_Name/Function_Name
            PostMethod postMethod = new PostMethod(method);
            byte[] b = soapRequestData.getBytes("utf-8");
            InputStream is = new ByteArrayInputStream(b,0,b.length);
            RequestEntity re = new InputStreamRequestEntity(is,b.length,"text/xml; charset=utf-8");
            postMethod.setRequestEntity(re);

            HttpClient httpClient = new HttpClient();
            int statusCode = httpClient.executeMethod(postMethod);
            //200说明正常返回数据
            if (statusCode != 200) {
                //internet error
                System.out.println(statusCode);
            }
            soapRequestData = postMethod.getResponseBodyAsString();
            System.out.println(soapRequestData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读