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

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







5.1请求程序代码
public void sendMessage() throws Exception {
       System.out.println("调用servlet开始=================");
       StringBuffer sendStr = new StringBuffer();
       sendStr.append("");
       sendStr.append("");
       sendStr.append("953943547334");
       sendStr.append("2012040211394324");
       sendStr.append("");
       sendStr.append("201203");
       sendStr.append("");
       sendStr.append("");
 
       BufferedReader reader = null;
 
       try {
           String strMessage = "";
           StringBuffer buffer = new StringBuffer();
 
           // 接报文的地址
           URL uploadServlet = new URL(
                  "http://localhost:9090/TestTransfers");
 
           HttpURLConnection servletConnection = (HttpURLConnection) uploadServlet
                  .openConnection();
           // 设置连接参数
           servletConnection.setRequestMethod("POST");
           servletConnection.setDoOutput(true);
           servletConnection.setDoInput(true);
           servletConnection.setAllowUserInteraction(true);
 
           // 开启流,写入XML数据
           OutputStream output = servletConnection.getOutputStream();
           System.out.println("发送的报文:");
           System.out.println(sendStr.toString());
 
           output.write(sendStr.toString().getBytes());
           output.flush();
           output.close();
 
           // 获取返回的数据
           InputStream inputStream = servletConnection.getInputStream();
           reader = new BufferedReader(new InputStreamReader(inputStream));
           while ((strMessage = reader.readLine()) != null) {
              buffer.append(strMessage);
           }
 
           System.out.println("接收返回值:" + buffer);
 
       } catch (java.net.ConnectException e) {
           throw new Exception();
       } finally {
           if (reader != null) {
              reader.close();
           }
 
       }
    }
5.2响应程序代码

public class TestTransfers extends HttpServlet {
 
    private static final long serialVersionUID = 1L;
 
    protected void doGet(HttpServletRequest request,
           HttpServletResponse response) throws ServletException, IOException {
    }
 
    protected void doPost(HttpServletRequest request,
           HttpServletResponse response) throws ServletException, IOException {
 
       //判断请求报文是否来自代维系统的ip地址
       String ip = request.getRemoteHost();

// 获取收到的报文
        BufferedReader reader = request.getReader();
         String line = "";
        StringBuffer inputString = new StringBuffer();
        while ((line = reader.readLine()) != null) {
        inputString.append(line);
        }
        
       //如有必要,可以在报文中增加其他验证和加密的参数
       //解析获取到的报文,根据ip地址、其他验证、加密等等来判断请求报文的服务器是否有权限
      
       //如果请求验证合格,则根据请求的参数装配返回的报文
 
       // 要返回的报文
       StringBuffer resultBuffer = new StringBuffer();
       resultBuffer.append("");
       resultBuffer.append("");
       resultBuffer.append("953947334");
       resultBuffer.append("20120402113943");
       resultBuffer.append("");
       resultBuffer.append("0000");
       resultBuffer.append("成功");
       resultBuffer.append("");
       resultBuffer.append("");
       resultBuffer.append("");
       resultBuffer.append("长治县");
       resultBuffer.append("铁通");
       resultBuffer.append("线路");
       resultBuffer.append("王加和");
       resultBuffer.append("20120301000000");
       resultBuffer.append("20120331235959");
       resultBuffer.append("50");
       resultBuffer.append("40");
       resultBuffer.append("0.80");
       resultBuffer.append("");
       //......
       //......
       //......
       //循环组装响应的报文
      
       resultBuffer.append("");
       resultBuffer.append("");
 
       // 设置发送报文的格式
       response.setContentType("text/xml");
       response.setCharacterEncoding("UTF-8");
 
       PrintWriter out = response.getWriter();
       out.println(resultBuffer.toString());
       out.flush();
       out.close();
    }
}

你可能感兴趣的:(java开发接口利用http协议传输数据)