• java实现http协议发送和接收数据


      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   
     13        BufferedReader reader = null;
     14   
     15        try {
     16            String strMessage = "";
     17            StringBuffer buffer = new StringBuffer();
     18   
     19            // 接报文的地址
     20            URL uploadServlet = new URL(
     21                   "http://localhost:9090/TestTransfers");
     22   
     23            HttpURLConnection servletConnection = (HttpURLConnection) uploadServlet
     24                   .openConnection();
     25            // 设置连接参数
     26            servletConnection.setRequestMethod("POST");
     27            servletConnection.setDoOutput(true);
     28            servletConnection.setDoInput(true);
     29            servletConnection.setAllowUserInteraction(true);
     30   
     31            // 开启流,写入XML数据
     32            OutputStream output = servletConnection.getOutputStream();
     33            System.out.println("发送的报文:");
     34            System.out.println(sendStr.toString());
     35   
     36            output.write(sendStr.toString().getBytes());
     37            output.flush();
     38            output.close();
     39   
     40            // 获取返回的数据
     41            InputStream inputStream = servletConnection.getInputStream();
     42            reader = new BufferedReader(new InputStreamReader(inputStream));
     43            while ((strMessage = reader.readLine()) != null) {
     44               buffer.append(strMessage);
     45            }
     46   
     47            System.out.println("接收返回值:" + buffer);
     48   
     49        } catch (java.net.ConnectException e) {
     50            throw new Exception();
     51        } finally {
     52            if (reader != null) {
     53               reader.close();
     54            }
     55   
     56        }
     57     }
     58 1
     59 2
     60 3
     61 4
     62 5
     63 6
     64 7
     65 8
     66 9
     67 10
     68 11
     69 12
     70 13
     71 14
     72 15
     73 16
     74 17
     75 18
     76 19
     77 20
     78 21
     79 22
     80 23
     81 24
     82 25
     83 26
     84 27
     85 28
     86 29
     87 30
     88 31
     89 32
     90 33
     91 34
     92 35
     93 36
     94 37
     95 38
     96 39
     97 40
     98 41
     99 42
    100 43
    101 44
    102 45
    103 46
    104 47
    105 48
    106 49
    107 50
    108 51
    109 52
    110 53
    111 54
    112 55
    113 56
    114 57
    115 58
    116 59
    117 60
    118 61
    119 62
    120 63
    121 64
    122 65
    123 66
    124 67
    125 public class TestTransfers extends HttpServlet {
    126   
    127     private static final long serialVersionUID = 1L;
    128   
    129     protected void doGet(HttpServletRequest request,
    130            HttpServletResponse response) throws ServletException, IOException {
    131     }
    132   
    133     protected void doPost(HttpServletRequest request,
    134            HttpServletResponse response) throws ServletException, IOException {
    135   
    136        //判断请求报文是否来自代维系统的ip地址
    137        String ip = request.getRemoteHost();
    138  
    139 // 获取收到的报文
    140         BufferedReader reader = request.getReader();
    141          String line = "";
    142         StringBuffer inputString = new StringBuffer();
    143         while ((line = reader.readLine()) != null) {
    144         inputString.append(line);
    145         }
    146          
    147        //如有必要,可以在报文中增加其他验证和加密的参数
    148        //解析获取到的报文,根据ip地址、其他验证、加密等等来判断请求报文的服务器是否有权限
    149        
    150        //如果请求验证合格,则根据请求的参数装配返回的报文
    151   
    152        // 要返回的报文
    153        StringBuffer resultBuffer = new StringBuffer();
    154        resultBuffer.append("<?xml version="1.0" encoding="UTF-8"?>");
    155        resultBuffer.append("<report_data>");
    156        resultBuffer.append("<respon_req>953947334</respon_req>");
    157        resultBuffer.append("<respon_time>20120402113943</respon_time>");
    158        resultBuffer.append("<result>");
    159        resultBuffer.append("<id>0000</id>");
    160        resultBuffer.append("<comment>成功</comment>");
    161        resultBuffer.append("</result>");
    162        resultBuffer.append("<items>");
    163        resultBuffer.append("<item>");
    164        resultBuffer.append("<county>长治县</county>");
    165        resultBuffer.append("<company>铁通</company>");
    166        resultBuffer.append("<speciality>线路</speciality>");
    167        resultBuffer.append("<personnel>王加和</personnel>");
    168        resultBuffer.append("<begin_time>20120301000000</begin_time>");
    169        resultBuffer.append("<end_time>20120331235959</end_time>");
    170        resultBuffer.append("<plan_quantity>50</plan_quantity>");
    171        resultBuffer.append("<checkout_quantity>40</checkout_quantity>");
    172        resultBuffer.append("<patrol_rate>0.80</patrol_rate>");
    173        resultBuffer.append("</item>");
    174        //......
    175        //......
    176        //......
    177        //循环组装响应的报文
    178        
    179        resultBuffer.append("</items>");
    180        resultBuffer.append("</report_data>");
    181   
    182        // 设置发送报文的格式
    183        response.setContentType("text/xml");
    184        response.setCharacterEncoding("UTF-8");
    185   
    186        PrintWriter out = response.getWriter();
    187        out.println(resultBuffer.toString());
    188        out.flush();
    189        out.close();
    190     }
    191 }
  • 相关阅读:
    操作系统路径.islink在使用python的windows上
    Python shutil模块zz
    CMake 两种变量原理 ZZ
    cmake语法-函数和宏的定义
    cmake函数、宏和模块
    cmake函数、宏和模块
    CMake 常用宏分享
    设定cmake输出目录
    android layerlist 里面的rotate标签得到三角形原理详解
    abp(net core)+easyui+efcore实现仓储管理系统——出库管理之三(五十一)
  • 原文地址:https://www.cnblogs.com/-lpf/p/4924807.html
Copyright © 2020-2023  润新知