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; }

 

posted @ 2018-05-14 10:53  欧阳宇  阅读(1188)  评论(0编辑  收藏  举报