zoukankan      html  css  js  c++  java
  • http请求与响应

    一、用http写服务类

      1 package com.montnets.customer.servlet;
      2 
      3 import java.io.BufferedReader;
      4 import java.io.IOException;
      5 import java.io.InputStreamReader;
      6 import java.io.PrintWriter;
      7 import java.sql.Timestamp;
      8 import java.util.LinkedHashMap;
      9 import java.util.List;
     10 
     11 import javax.servlet.ServletException;
     12 import javax.servlet.ServletInputStream;
     13 import javax.servlet.http.HttpServlet;
     14 import javax.servlet.http.HttpServletRequest;
     15 import javax.servlet.http.HttpServletResponse;
     16 
     17 
     18 
     19 import com.alibaba.fastjson.JSON;
     20 import com.alibaba.fastjson.JSONObject;
     21 import com.montnets.customer.entity.Message;
     22 import com.montnets.emp.common.biz.BaseBiz;
     23 import com.montnets.emp.common.context.EmpExecutionContext;
     24 import com.montnets.emp.entity.employee.LfEmployee;
     25 import com.montnets.emp.entity.sysuser.LfSysuser;
     26 import com.montnets.emp.sysuser.servlet.opt_sysuserSvt;
     27 
     28 
     29 /**
     30  * 人员修改接口,当用户修改信息时,调用此接口,同步用户信息表
     31  * @author ouyangyu
     32  *
     33  */
     34 public class PersonUpdate extends HttpServlet{
     35     
     36         BaseBiz baseBiz=new BaseBiz();
     37         Message message = new Message();
     38         opt_sysuserSvt sysuserSvt = new opt_sysuserSvt();
     39         public String errorInfoString = "";
     40         
     41         @Override
     42         public void doPost(HttpServletRequest request, HttpServletResponse response)
     43                 throws ServletException, IOException {
     44             
     45             //解决乱码问题,必须放在最前面
     46             request.setCharacterEncoding("utf-8");
     47             response.setCharacterEncoding("utf-8");
     48             response.setContentType("text/html");
     49              51             boolean flag =false;
     52             StringBuffer output = new StringBuffer();
     53             try {  
     54                 //获取客户请求数据的流
     55                   BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(),"utf-8")); 
     56                   String inputLine = null; 
     57                   while ( (inputLine = in.readLine()) != null){ 
     58                       output.append(inputLine); 
     59                   } 
     60                   in.close(); 
     61                   
     62                   String result = output.toString();
     68                   System.out.println("调用修改账户的接口,客户请求参数result:"+result);
     79                 if(flag){
     80                     message.setMsgCode("000");
     81                     message.setMsgStr("success");
     82                     EmpExecutionContext.info("调用修改人员信息接口获取成功!");
     83                 }else{
     84                     message.setMsgCode("111");
     85                     message.setMsgStr(errorInfoString);
     86                     EmpExecutionContext.error("调用修改人员信息接口失败!");
     87                 }
     88                 
     89                  //返回值
     90                 PrintWriter out = response.getWriter();
     91                 //将对象转为json格式的字符串
     92                 String jsonStr= JSON.toJSONString(message);
     93                 out.write(jsonStr);
     94                 out.flush();
     95                 out.close();
     96                     
     97              }catch (Exception e) {  
     98                  EmpExecutionContext.error(e, "获取修改人员信息接口失败"); 
     99              }  
    100         }
    101         
    102         @Override
    103         protected void doGet(HttpServletRequest request, HttpServletResponse response)
    104                 throws ServletException, IOException {
    105             this.doPost(request, response);
    106         }
    107         
    108       
    313         /**
    314          * 将json字符串变为java对象
    315          */
    316         public Message jsonToMessage(String jsonStr){
    317             Message msg = null;
    318             JSONObject object = null;
    319             try {
    321                 object = this.jsonToObject(jsonStr);
    322                 if(object!=null){
    323                     msg = new Message();
    324                     msg.setMsgCode(object.get("msgCode").toString());
    325                     msg.setMsgStr(object.get("msgStr").toString());
    326                 }
    327 
    328             } catch (NullPointerException e) {
    329                 EmpExecutionContext.error(e, "调用人员查询接口,将json字符串变为java对象异常。");
    330             }
    331             return msg;
    332         }
    333         
    334         
    335         //判断字符串是否为空或者空字符串
    336         public static boolean isEmpty(String str){
    337             boolean flag=false;
    338             if(str==null||"".equals(str)){
    339                 flag=true;
    340             }
    341             return flag;
    342         }
    343 
    344     }

    二、http请求类

     1 package com;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.InputStreamReader;
     5 import java.io.OutputStream;
     6 import java.net.HttpURLConnection;
     7 import java.net.URL;
     8 import java.util.HashMap;
     9 import java.util.Iterator;
    10 import java.util.Map;
    11 
    12 
    13 import net.sf.json.JSONException;
    14 import net.sf.json.JSONObject;
    15 
    16 
    17 public class TestPersonUpdate {
    18     public static void main(String[] args) throws Exception {
    19 
    20         String address="http://127.0.0.1:8080/emp_sta/services/personUpdate.htm";
    21         String serviceBody = "{"employeeNumber":"","status":"0"," +
    22                                 ""usercn":"小花花","sex":"1","+
    23                                 ""startTime":"20090101","personGroup":"2","+
    24                                 ""idCardNum":"421024199202021345","mobile":"13827453210","+
    25                                 ""email":"","wxStr":"","+
    26                                 ""qqNum":""}";
    27 
    28         URL url = null;
    29         HttpURLConnection urlConn = null;
    30         url = new URL(address);
    31         urlConn = (HttpURLConnection) url.openConnection();
    32         urlConn.setRequestMethod("POST");
    33         urlConn.setDoOutput(true);
    34         OutputStream out = urlConn.getOutputStream();
    35         String param=serviceBody;
    36         out.write(param.toString().getBytes("utf-8"));
    37         
    38         out.flush();
    39         out.close();
    40 
    41         StringBuffer str = new StringBuffer(); 
    42         BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8")); 
    43         String inputLine = null; 
    44         while ( (inputLine = in.readLine()) != null){ 
    45             str.append(inputLine); 
    46         } 
    47         in.close(); 
    48         
    49         String Strjson=str.toString();
    50         System.out.println("原始数据:");
    51         System.out.println(Strjson.toString()); 
    52         
    53         Map<String,String> mapst=toMap(Strjson);
    54 
    55         for (String s : mapst.keySet()) {
    56             System.out.println("key:" + s+",values:" + mapst.get(s));
    57         }
    58 
    59         System.out.println("
    ");
    60 
    61         
    62     }
    63 
    64     /**
    65      * 将Json对象转换成Map
    66      * 
    67      * @param jsonObject
    68      *            json对象
    69      * @return Map对象
    70      * @throws JSONException
    71      */
    72     public static Map<String,String> toMap(String jsonString) throws JSONException {
    73 
    74         JSONObject jsonObject = new JSONObject();
    75         jsonObject=JSONObject.fromObject(jsonString);
    76         
    77         Map<String,String> result = new HashMap<String,String>();
    78         Iterator iterator = jsonObject.keys();
    79         String key = null;
    80         String value = null;
    81         
    82         while (iterator.hasNext()) {
    83             key = (String) iterator.next();
    84             value = jsonObject.getString(key);
    85             result.put(key, value);
    86 
    87         }
    88         return result;
    89 
    90     }
    91 
    92 }
  • 相关阅读:
    博客地址
    Version 1.4.2_03 of the JVM not suitable for this product.解决
    http请求(一) 工具
    Service 的两种启动方法和区别
    软件开发过程应该采用集中优势兵力各个击破
    架构感悟
    嵌套事务模版
    软件行业对人才的依赖
    使用SQL Server 2005 新的语法ROW_NUMBER()进行分页的两种不同方式的性能比较
    架构设计中的分层与分区
  • 原文地址:https://www.cnblogs.com/ouyy/p/8991047.html
Copyright © 2011-2022 走看看