webservice开发(jdk发布服务端,wsimport和URL两种方式调用)


//服务端的程序
package com.webservice.sym;

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

@WebService
public class HelloService {

	/**
	 * @param args
	 * 采用的JDK在1.6.25版本之上
         * 总结webservice:webservice内部是使用Socket实现,通过http post
	 * 方式接受客户的请求,webservice与客户之间一般使用SOAP协议传输XML数据,它本身是为了跨平台或者跨语言而设计的
	 * 运行本project(为server),在浏览器中输入:http://136.10.7.151:808/HelloS?wsdl查看是否成功,
	 * 则在命令行窗口中输入命令:wsimport -s . http://136.10.7.151:808/HelloS?wsdl,在所在的目录下生成文件,
	 * 保留.java 文件,去除.class文件。建立另一个project(为client),把以上生成的.java文件copy至client的project中,并创建如下client程序(HelloClient)
	 * 
	 */
	
	public static void main(String[] args) {
		HelloService hello = new HelloService();
		/*
		 * 136.10.7.15必须写自己的地址,必须保证能发布服务,不能乱写
		 */
		Endpoint endpoint = Endpoint.publish("http://136.10.7.151:808/HelloS",
				hello);
	}

	/*
	 * 必须定义一个方法,且此方法不能是static的,不能是final
	 */
	@WebMethod
	public String sayhello(String name) {
		return "Hello, " + name + "\n";
	}
//不发布此方法
	@WebMethod(exclude=true)
	public String sayhello2(String name) {
		return "Hello, " +name + "\n";
	}
}
//采用wsimport进行调用
客户端的新创建的java文件为:
class HelloClient{   
	public static void main(String args[]) {   
	  HelloServiceService service = new HelloServiceService();   
	 HelloService helloProxy = service.getHelloServicePort();   
	 String hello = helloProxy.sayhello("你好");          
	 System.out.println(hello);   
	 }   
	}
//采用URL进行调用
package com.url.sym;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class UrlConn {

	/**
	 *本次采用的是通过SOAP连接,并获取所需要调用的信息,获取方式为:在MyEclipse中找到SOAP webservice launch expl
	 *打开并找到WSDL main,打开,输入所需服务的WSDl地址,点击GO之后输入服务函数的参数,可得到explorer给生成的SOAP字符串
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// 服务的地址
		URL urlconn = new URL("http://136.10.7.151:808/HelloS");
		HttpURLConnection conn = (HttpURLConnection) urlconn.openConnection();
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
		OutputStream oStream = conn.getOutputStream();
		String soap = "<soapenv:Envelope xmlns:soapenv=\""
				+ "http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\""
				+ "http://sym.webservice.com/\" xmlns:xsd=\""
				+ "http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\""
				+ "http://www.w3.org/2001/XMLSchema-instance\">"
				+ "<soapenv:Body>" + "<q0:sayhello>" + "<arg0>666</arg0>"
				+ "</q0:sayhello>" + "</soapenv:Body>" + "</soapenv:Envelope>";
		oStream.write(soap.getBytes());
		InputStream iStream = conn.getInputStream();
		Reader reader = new InputStreamReader(iStream);
	
		
          //方法一:
		int tempchar;
//不能用String str=null;否则会打印出“null”
		String str =new String();
		while ((tempchar = reader.read()) != -1) {
			str += (char) tempchar;

		}
          //方法二:
          // BufferedReader bReader = new BufferedReader(reader); 
          //String str = bReader.readLine();
          //while ((tempchar = bReader.read()) != -1) {
		//str += bReader.readLine();
		//}
		System.out.print("str:"+str);
		/*
		 * 打印结果:str:<?xml version="1.0" ?><S:Envelope xmlns:S=
		 * "http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayhelloResponse xmlns:ns2=
		 * "http://sym.webservice.com/"><return>Hello, 666
         *</return></ns2:sayhelloResponse></S:Body></S:Envelope>
		 */
//		bReader.close();
		reader.close();
		iStream.close();
		oStream.close();
		conn.disconnect();
	}

}


猜你喜欢

转载自vvsongsunny.iteye.com/blog/2034038