- 场景
上一章中的webservice接口,因为现场正式环境的项目与外部单位网络不通,是通过前置机与外部进行数据交换,所以我们将webservice部署在前置机,在使用HttpURLConnection与正式服务器上进行数 据传递。这里可以使用的技术挺多的,如:java的rmi,Hessian,spring的HttpInvoker等,下面是使用HttpURLConnection的代码(实际情况是最后选择了spring的HttpInvoker):
- HttpURLConnection客户端调用方法
1 package http.rpc.util; 2 3 import java.io.BufferedOutputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 import java.io.OutputStreamWriter; 9 import java.net.HttpURLConnection; 10 import java.net.MalformedURLException; 11 import java.net.URL; 12 import java.net.URLEncoder; 13 import java.util.Map; 14 15 public class ClientUtils { 16 /* 17 * @param url 服务端url地址 18 * @param beanid 访问远程方法所在的类名 19 * @param method 调用服务端的方法名 20 * @param param 服务端的方法的参数 21 */ 22 public static Object remoteService(String url, String beanid, String method, String param) { 23 HttpURLConnection urlcon = null; 24 String result = null; 25 try { 26 if (param == null) { 27 param = ""; 28 } 29 URL u = new URL(url + "/webservice.rpc"); 30 31 byte[] databuffer = new byte[1024]; 32 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 33 int len = 0; 34 try { 35 urlcon = (HttpURLConnection) u.openConnection(); 36 urlcon.setDoOutput(true); 37 urlcon.setRequestMethod("POST"); 38 OutputStream buf = new BufferedOutputStream(urlcon.getOutputStream()); 39 OutputStreamWriter out = new OutputStreamWriter(buf, "UTF-8"); 40 /* 向webservice.rpc传递3个参数 */ 41 out.write("serverid=" + beanid + "&method=" + method + param); 42 out.flush(); 43 out.close(); 44 45 urlcon.connect(); 46 if (urlcon.getResponseCode() == 200) { 47 InputStream in = urlcon.getInputStream(); 48 while ((len = in.read(databuffer)) != -1) { 49 outStream.write(databuffer, 0, len); 50 } 51 in.close(); 52 result = new String(outStream.toByteArray(), "utf-8"); 53 System.out.println(result); 54 urlcon.disconnect(); 55 } 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 } catch (MalformedURLException e) { 60 e.printStackTrace(); 61 } 62 urlcon.disconnect(); 63 return result; 64 } 65 }
- 服务端web.xml配置(简单的servlet配置)
1 <servlet-mapping> 2 <servlet-name>testserver</servlet-name> 3 <url-pattern>/webservice.rpc</url-pattern> 4 </servlet-mapping> 5 6 <servlet> 7 <servlet-name>testserver</servlet-name> 8 <servlet-class>http.rpc.Server</servlet-class>
- 服务端http请求类
1 package http.rpc; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.ServletOutputStream; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class Server extends HttpServlet{ 11 12 @Override 13 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 14 throws ServletException, IOException { 15 System.out.println("doPost"); 16 String serverid = req.getParameter("serverid").toString(); 17 String method = req.getParameter("method").toString(); 18 String methodParam = req.getParameter("param").toString(); 19 ServletOutputStream sos = resp.getOutputStream(); 20 String result = "远程调用返回"; 21 sos.write(result.getBytes("utf-8")); 22 } 23 }
- 最后说说
Server类得到远程方法信息后,可以使用反射去调用具体方法。返回的数据如果是对象类型,可以实现序列化和反序列化进行数据传递。因为使用spring的HttpInvoker,就没写得很完善,这里只是做个记录,有时间再完善吧。