zoukankan      html  css  js  c++  java
  • java http协议,添加header以及post传参,以及服务端获取

    一、客户端请求

    public static String test(){
    JSONObject obj = new JSONObject();
    obj.put("cusName","禹州林业集团");
    obj.put("socialCreditCode","11431000006542054G");
    String parm = obj.toString();
    String result = "";//BASE64加密的返回结果
    String res = "";//解密后的字符串
    PrintWriter out = null;
    BufferedReader in = null;
    try {
    java.net.URL realUrl;
    realUrl = new URL("http://127.0.0.1:9998/test/c/getC");
    HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
    conn.setRequestMethod("POST");
    // 设置通用的请求属性
    conn.setRequestProperty("accept", "*/*");
    conn.setRequestProperty("connection", "Keep-Alive");
    conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    conn.setRequestProperty("Content-Type", "text/plain;charset=utf-8");
    conn.addRequestProperty("token","11111"); //设置header信息
    // 发送POST请求必须设置如下两行
    conn.setDoOutput(true);
    conn.setDoInput(true);
    // 获取URLConnection对象对应的输出流
    out = new PrintWriter(conn.getOutputStream());
    // 发送请求参数
    out.print(parm);//json传参
    // flush输出流的缓冲
    out.flush();
    // 定义BufferedReader输入流来读取URL的响应
    in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) {
    result += line;
    }
    System.out.println(res);
    System.out.println(result);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {// 使用finally块来关闭输出流、输入流
    try {
    if (out != null) {
    out.close();
    }
    if (in != null) {
    in.close();
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    return result;
    }

    二、服务端获取

    @RequestMapping(value = "/c/getC",method=RequestMethod.POST)
    public String getCustomerInfoForGGIP(HttpServletRequest request) throws Exception{
    String cusName = "";
    String socialCreditCode = "";
    String result = "";
    BASE64Encoder base64Encoder = new BASE64Encoder();
    JSONObject resultObject = new JSONObject();
    String token = request.getHeader("token");//获取header中内容
    if(StringUtils.isEmpty(token)){
    resultObject.put("code","1111");
    resultObject.put("msg","鉴权的token不能为空!");
    return resultObject.toString();
    }
    if(!"ZGCDB".equals(token)){
    resultObject.put("code","1111");
    resultObject.put("msg","鉴权不成功!");
    return resultObject.toString();
    }else{
    String parm = "";
    BufferedReader in = null;
    in = new BufferedReader(new InputStreamReader(request.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) { //获取参数
    parm += line;
    }
    if(parm!=null){
    JSONObject paramJson = JSONObject.fromObject(parm);
    if(!paramJson.isNullObject()){
    cusName = paramJson.getString("cusName");
    socialCreditCode = paramJson.getString("socialCreditCode");
    result = customerBo.getCustomerInfoForGGIP(cusName,socialCreditCode,request);
    }else{
    resultObject.put("code","1111");
    resultObject.put("msg","参数解析错误!");
    return resultObject.toString();
    }
    }else{
    resultObject.put("code","1111");
    resultObject.put("msg","参数解析错误!");
    return resultObject.toString();
    }
    }
    return result;
    }

  • 相关阅读:
    Servlet监听器及在线用户
    数据分页jdbc+mysql实现
    使用ajax验证用户名重复
    Mysql中的事务
    用户登录注册案例分析
    Java连接mysql数据库
    Java连接sqlite数据库
    虚拟主机TOMCAT配置
    用jquery控制表格奇偶行及活动行颜色
    JDK安装后 没有tools.jar 和dt.jar包的解决办法
  • 原文地址:https://www.cnblogs.com/yelanggu/p/15504658.html
Copyright © 2011-2022 走看看