webservice
发布时间:2020-12-16 21:36:00 所属栏目:安全 来源:网络整理
导读:服务端 代码如下,控制台打
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"); } }
wsimport -s E:eclipseworkspaceAdvancesrc -p webservceclient -keep http://localhost:8080/MyService?wsdl
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);//打印返回结果 } }
<?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();
}
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读