http 和 webservice 都是基于TCP/IP协议的应用层协议
webservice是基于http的soap协议传输数据 webservice=soap=http+xml,webservice协议就是有http+xml组成的,其中xml中会用到wsdl,wsdl是描述语言xml中的一种格式。
socket是基于TCP/IP的传输协议,是对TCP/IP协议的封装
socket和TCP都是基于TCP/IP传输层协议
注:Restful是一种接口规范,而不是接口协议,restful接口规范中也会用到http协议。
以下内容转载自:https://blog.csdn.net/sunroyi666/article/details/51917991 因现在大部分会用http协议不用webservice协议,故没有实际操作,只是转载来了解理论。
一. WSDL WebService的创建:
1.创建【Web Service Project】:
WebServices Framework要选JAX-WS:
2.写一个简单的测试用例:
package com.webservice;
public class WebService{
public String printData(String printerName){
String strRet = "Welcome to use WebService, " + printerName;
System.out.println("Print from WebService:" + strRet);
return strRet;
}
}
3.发布Web Service:
点击工具栏的New Web Service:
Strategy选择第二个(Create web service from Java class):
勾选【Generate WSDL in project】:
点击【Finish】后,系统会在WEB-INF/wsdl下生成两个文件:
WebServiceService.wsdl:这个文件是用来描述Web Service内容的
WebServiceService_schema1.xsd:用来说明Web Service的命令及其参数
比如:sample里面的WebService是【printData】,有一个String类型的参数【arg0】,返回值一个String类型的值。
将WebService项目部署到Tomcat即可。
(部署方法略)
二. WSDL WebService的调用:
方法1:创建Web Service Client来调用:
1.创建【Java Project】:
2.点击工具栏的New Web Service Client:
3.选择【WSDL URL】:
4.点击【Next】完成创建后,在src/com/webservice下,自动生成相关文件。(WebServiceTest.java除外,这个是自己创建的调用文件)
5.创建【WebServiceTest.java】
代码如下:
package com.webservice;
public class WebServiceTest{
public static void main(String[] args){
WebServiceService wssPrintData = new WebServiceService();
WebServiceDelegate wsdPrintData = wssPrintData.getWebServicePort();
System.out.println(wsdPrintData.printData("sun"));
}
}
6.【WebServiceTest.java】右键→Run As→Java Application
输出结果:
Welcome to use WebService, sun
方法2:用HttpClient调用:
package com.httpclientforwsdl;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
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;
public class WebServiceHttpClientTest{
public synchronized static String accessService(String wsdl,String ns,String method,Map params,String result)throws Exception{
//拼接参数
String param = getParam(params);
String soapResponseData = "";
//拼接SOAP
StringBuffer soapRequestData = new StringBuffer("");
soapRequestData.append("");
soapRequestData.append("");
soapRequestData.append("");
soapRequestData.append(param);
soapRequestData.append(" ");
soapRequestData.append(" " + " ");
PostMethod postMethod = new PostMethod(wsdl);
// 然后把Soap请求数据添加到PostMethod中
byte[] b=null;
InputStream is=null;
try {
b = soapRequestData.toString().getBytes("utf-8");
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 status = httpClient.executeMethod(postMethod);
System.out.println("status:"+status);
if(status==200){
soapResponseData = getMesage(postMethod.getResponseBodyAsString(),result);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
if(is!=null){
is.close();
}
}
return soapResponseData;
}
public static String getParam(Map params){
String param = "";
if(params!=null){
Iterator it = params.keySet().iterator();
while(it.hasNext()){
String str = it.next();
param+="<"+str+">";
param+=params.get(str);
param+=""+str+">";
}
}
return param;
}
public static String getMesage(String soapAttachment,String result){
System.out.println("message:"+soapAttachment);
if(result==null){
return null;
}
if(soapAttachment!=null && soapAttachment.length()>0){
int begin = soapAttachment.indexOf(result);
begin = soapAttachment.indexOf(">", begin);
int end = soapAttachment.indexOf(""+result+">");
String str = soapAttachment.substring(begin+1, end);
str = str.replaceAll("<", "<");
str = str.replaceAll(">", ">");
return str;
}else{
return "";
}
}
/**
* @param args
*/
public static void main(String[] args) {
try {
Map param = new HashMap();
param.put("arg0", "sun");
String wsdl="http://localhost:8080/WebService/WebServicePort?wsdl";
String ns = "http://webservice.com/";
String method="printData";
String response =accessService(wsdl,ns,method,param,"return");
System.out.println("main:"+response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
显示结果:
status:200
七月 15, 2016 3:43:27 下午 org.apache.commons.httpclient.HttpMethodBase getResponseBody
警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
message:
main:Welcome to use WebService, sun
作者:Royi666
来源:CSDN
原文:https://blog.csdn.net/sunroyi666/article/details/51917991
版权声明:本文为博主原创文章,转载请附上博文链接!