• http请求各种数据的接受


    一、接受请求的http数据

      1、接受客户请求的数据,例如xml数据格式,代码如下:

        此代码相当于服务端

    public String analyzeWeixinXMLStr(HttpServletRequest request){
            String  xmlStr = null;
            try
            {
                BufferedReader reader = request.getReader();
                String temp = null;
                StringBuffer requestXml = new StringBuffer();
                while((temp = reader.readLine()) != null)
                {
                    requestXml.append(temp);
                }
                //System.out.println(requestXml.toString());
                if(requestXml.toString().length()>0){
                    // 转码,解决乱码的问题
                    xmlStr = new String(requestXml.toString().getBytes("iso8859-1"), "UTF-8");
                }
            }
            catch (Exception e)
            {
                xmlStr = null;
                OttExecutionContext.error(e,"WxCommBiz.analyzeWeixinXMLStr is error");
            }
            return xmlStr;
        }

      2、将xml的数据格式转换为map,便于获取,代码如下:

    public HashMap<String, String> analyzeWeixinXMLHashMap(String requestXml){
            HashMap<String, String> paramsXmlMap = null;
            try
            {
                if(requestXml.toString().length()>0){
                    paramsXmlMap = new HashMap<String, String>();// 获取微信服务发送请求,并返回第一层级json格式数据
                    Document document = DocumentHelper.parseText(requestXml);
                    Element items = document.getRootElement();
                    for (Iterator i = items.elementIterator(); i.hasNext();)
                    {
                        Element item = (Element) i.next();
                        paramsXmlMap.put(item.getName(), item.getText());
                    }
                }
            }
            catch (Exception e)
            {
                paramsXmlMap = null;
                OttExecutionContext.error(e,"WxCommBiz.analyzeWeixinXMLHashMap is error");
            }
            return paramsXmlMap;
        }

       3、将xml格式的数据推送给接口服务器,并获取返回结果;

        此代码相当于客服端

        public String pushMsg(String reqXml) throws Exception
        {
            String result=null;
            try{
           //请求服务器接口(将数据推送给服务器接口) String address
    = SystemGlobals.getValue("addressCustomerService"); URL url = null; HttpURLConnection urlConn = null; url = new URL(address); urlConn = (HttpURLConnection) url.openConnection(); urlConn.setRequestMethod("POST"); urlConn.setDoOutput(true); OutputStream out = urlConn.getOutputStream(); //Post请求参数 reqXml=URLEncoder.encode(reqXml,"utf-8"); String param="msg="+reqXml; out.write(param.getBytes("utf-8")); out.flush(); out.close();        
           //接受服务器返回的数据结果
    StringBuffer str
    = new StringBuffer(); BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8")); String inputLine = null; while ( (inputLine = in.readLine()) != null){ str.append(inputLine); } in.close(); result=parseSingleStr(str.toString()); }catch(Exception e){ OttExecutionContext.error(e, "消息转发异常!"); } if(result!=null&&HTTP_SUCCESS.equals(result)){ OttExecutionContext.info("消息转发成功!result:"+result); }else{ OttExecutionContext.info("消息转发失败!result:"+result); } return result; } private static final Pattern singleResPattern=Pattern.compile("(.+)"http://tempuri.org/">(\S+)</(.+)"); /** * 解析xml格式单个返回结果 * @datetime 2017-4-12 */ public static String parseSingleStr(String resXML){ if(resXML==null){ return null; } String result=null; Matcher matcher=singleResPattern.matcher(resXML); if(matcher.matches()){ MatchResult matchResult=matcher.toMatchResult(); result=matchResult.group(2); } return result; }
  • 相关阅读:
    整理
    C# =>语法
    这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”解决办法
    腾讯云静态资源放到cos存储桶里,并开启CDN自定义域名加速
    react lodash节流this找不到正确用法
    reactrouter V6踩坑
    查询net Framework 安装版本
    Nginx实现同一个端口访问不同前后端项目
    vue3 + vite + ts 学习笔记
    Vue3 全家桶,从 0 到 1 实战项目
  • 原文地址:https://www.cnblogs.com/ouyy/p/9034998.html
Copyright © 2020-2023  润新知