码迷,mamicode.com
首页 > 编程语言 > 详细

【转-整理】Java 对外接口开发(http)服务端-客户端

时间:2016-07-29 17:08:58      阅读:1618      评论:0      收藏:0      [点我收藏+]

标签:

java开发接口利用http协议传输数据

这个接口主要用来登录,java服务器提供一个接口,移动设备客户端(android和ios)能通过这个接口把用户名和密码之类的东东传过来到服务器验证,然后服务器返回数据告诉客户端是否登录成功。比如0是成功,1是失败。中间数据的传递都是通过http协议完成.这个接口该怎么写?

 

代码1.2为服务器接口,接收到客户端的信息解析验证,并返回需要的信息。

 

服务端代码:

 

  1 public class TestTransfers extends HttpServlet {
  2 
  3     private static final long serialVersionUID = 1L;
  4 
  5     //HttpServletRequest 和 HttpServletResponse 用于接收请求和返回接口值
  6     //doGet用于接收get方法
  7     protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
  8 
  9     }
 10 
 11     //doPost用于接收post方法
 12     protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
 13 
 14         // 判断请求报文是否来自代维系统的ip地址
 15         String ip = request.getRemoteHost();
 16         // 获取收到的报文
 17         BufferedReader reader = request.getReader();
 18         
 19         String line = "";
 20 
 21         StringBuffer inputString = new StringBuffer();
 22 
 23         while ((line = reader.readLine()) != null) {
 24 
 25             inputString.append(line);
 26 
 27         }
 28         // 如有必要,可以在报文中增加其他验证和加密的参数
 29         // 解析获取到的报文,根据ip地址、其他验证、加密等等来判断请求报文的服务器是否有权限
 30         // 如果请求验证合格,则根据请求的参数装配返回的报文
 31         // 要返回的报文
 32         StringBuffer resultBuffer = new StringBuffer();
 33 
 34         resultBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
 35 
 36         resultBuffer.append("<report_data>");
 37 
 38         resultBuffer.append("<respon_req>953947334</respon_req>");
 39 
 40         resultBuffer.append("<respon_time>20120402113943</respon_time>");
 41 
 42         resultBuffer.append("<result>");
 43 
 44         resultBuffer.append("<id>0000</id>");
 45 
 46         resultBuffer.append("<comment>成功</comment>");
 47 
 48         resultBuffer.append("</result>");
 49 
 50         resultBuffer.append("<items>");
 51 
 52         resultBuffer.append("<item>");
 53 
 54         resultBuffer.append("<county>长治县</county>");
 55 
 56         resultBuffer.append("<company>铁通</company>");
 57 
 58         resultBuffer.append("<speciality>线路</speciality>");
 59 
 60         resultBuffer.append("<personnel>王加和</personnel>");
 61 
 62         resultBuffer.append("<begin_time>20120301000000</begin_time>");
 63 
 64         resultBuffer.append("<end_time>20120331235959</end_time>");
 65 
 66         resultBuffer.append("<plan_quantity>50</plan_quantity>");
 67 
 68         resultBuffer.append("<checkout_quantity>40</checkout_quantity>");
 69 
 70         resultBuffer.append("<patrol_rate>0.80</patrol_rate>");
 71 
 72         resultBuffer.append("</item>");
 73 
 74         // ......
 75 
 76         // ......
 77 
 78         // ......
 79 
 80         // 循环组装响应的报文
 81 
 82         resultBuffer.append("</items>");
 83 
 84         resultBuffer.append("</report_data>");
 85 
 86         // 设置发送报文的格式
 87 
 88         response.setContentType("text/xml");
 89 
 90         response.setCharacterEncoding("UTF-8");
 91 
 92         PrintWriter out = response.getWriter();
 93 
 94         out.println(resultBuffer.toString());
 95 
 96         out.flush();
 97 
 98         out.close();
 99 
100     }
101 
102 }


客户端调用代码:

 

 1 public void sendMessage() throws Exception {
 2     System.out.println("调用servlet开始=================");
 3     StringBuffer sendStr = new StringBuffer();
 4     sendStr.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
 5     sendStr.append("<report_data>");
 6     sendStr.append("<request_req>953943547334</request_req>");
 7     sendStr.append("<request_time>2012040211394324</request_time>");
 8     sendStr.append("<request_param>");
 9     sendStr.append("<query_month>201203</query_month>");
10     sendStr.append("</request_param>");
11     sendStr.append("</report_data>");
12     BufferedReader reader = null;
13     try {
14         String strMessage = "";
15         StringBuffer buffer = new StringBuffer();
16         // 接报文的地址
17         URL uploadServlet = new URL(
18                "http://localhost:9090/TestTransfers");
19         HttpURLConnection servletConnection = (HttpURLConnection) uploadServlet
20                .openConnection();
21         // 设置连接参数
22         servletConnection.setRequestMethod("POST");
23         servletConnection.setDoOutput(true);
24         servletConnection.setDoInput(true);
25         servletConnection.setAllowUserInteraction(true);
26         // 开启流,写入XML数据
27         OutputStream output = servletConnection.getOutputStream();
28         System.out.println("发送的报文:");
29         System.out.println(sendStr.toString());
30         output.write(sendStr.toString().getBytes());
31         output.flush();
32         output.close();
33         // 获取返回的数据
34         InputStream inputStream = servletConnection.getInputStream();
35         reader = new BufferedReader(new InputStreamReader(inputStream));
36         while ((strMessage = reader.readLine()) != null) {
37            buffer.append(strMessage);
38         }
39         System.out.println("接收返回值:" + buffer);
40     } catch (java.net.ConnectException e) {
41         throw new Exception();
42     } finally {
43         if (reader != null) {
44            reader.close();
45         }
46     }
47  }
48 
49  

 

【转-整理】Java 对外接口开发(http)服务端-客户端

标签:

原文地址:http://www.cnblogs.com/Nouno/p/5719010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!