不同web服务器之间采用http发送请求以及处理返回数据

本文介绍了一种通过HTTP协议实现不同Web服务器间通信的方法,包括JSON数据格式化、使用密钥加密及解密的过程,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不同web服务器之间采用http发送请求以及处理返回数据
 1:数据采用json格式传送。
 2:双方共同约定好的密钥进行加解密。
 3:发送请求时,加密后再采用encode编码,处理请求时,不需要decode编码直接解密即可。

步骤:
    1:构造json数据格式
                String custId = "123456789";
                String partnerCode = "test";
                User user = new User(custId,partnerCode);
                JSONObject jsonObject = JSONObject.fromObject(user); //构造json格式数据
    2: 加密
                DiscuzAuthcode uc = new DiscuzAuthcode();
                String decString = uc.uc_authcode(jsonObject.toString(), "ENCODE", "9876543210");
                LOGGER.info("加密后:" + decString);

    3:发送到其它web服务器的http请求
        String connURL = "http://192.168.0.118/taobao/service/xxxxxx.do";
        String result = HttpConnection.httpConnection.sendAndReceiveHttpRequest(connURL, "param", decString);
        发送http请求大概步骤
        3.1.1 根据connURL获取http连接对象
         /**
         * 获取HTTP链接
         *
         * @return
         */
        private HttpURLConnection getHttpConnection(String connURL) {
                logger.info("request URL:" + connURL);
                URL url = null;
                HttpURLConnection httpConn = null;
                try {
                        url = new URL(connURL);
                        httpConn = (HttpURLConnection) url.openConnection();
                        httpConn.setRequestProperty("Proxy-Connection", "Keep-Alive");
                        httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                        HttpURLConnection.setFollowRedirects(true);
                        httpConn.setDoOutput(true);
                        httpConn.setDoInput(true);
                        httpConn.setRequestMethod("POST");
                        httpConn.setUseCaches(false);
                        return httpConn;
                } catch (MalformedURLException e) {
                        logger.info("", e);
                } catch (IOException e) {
                        logger.info("", e);
                }
                return null;
        }
        3.1.2:发送http请求并获取返回数据
        public String sendAndReceiveHttpRequest(String connURL, String key, String value) {
                HttpURLConnection httpConn = this.getHttpConnection(connURL);
                InputStream in = null;
                OutputStream out = null;
                try {
                        if (null == httpConn) {
                                return null;
                        }
                        httpConn.connect();
                        out = httpConn.getOutputStream();
                        DataOutputStream dos = new DataOutputStream(out);
                        dos.writeBytes(key + "=" + URLEncoder.encode(value, "UTF-8")); //写参数
                        dos.flush();
                        dos.close();
                       
                        InputStream is = httpConn.getInputStream();
                        String str = null;
                        StringBuffer buffer = new StringBuffer();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                        while ((str = br.readLine()) != null) {
                                buffer.append(str);
                        }
                        br.close(); 
                        String data = buffer.toString();
                        return StringUtils.isBlank(data) ? null :data;
                } catch (Exception e) {
                        logger.error(e);
                } finally {
                        try {
                                if (in != null){
                                        in.close();
                                }
                        } catch (Exception e2) {}

                        try {
                                if (out != null) {
                                        out.close();
                                }
                        } catch (Exception e2) {}
                       
                        try {
                                if (httpConn != null) {
                                        httpConn.disconnect();
                                }
                        } catch (Exception e2) {}
                }
                return null;
        }
        3.3.3: web服务器处理接受到的http请求
        public ActionForward queryPoint(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response)
                        throws Exception {
                String ciphertext = request.getParameter("param");
                String respStr = "{resultCode: '2', resultDetail: '传入的参数不正确'}";
                logger.info("queryPoint() - 密文:" + ciphertext);
                response.setCharacterEncoding("UTF-8");
                //处理请求的具体逻辑
                logger.info("queryPoint() - " + respStr);
                response.getWriter().write(respStr);
                return null;
        }

    4: 处理请求返回信息,保存到DB   
                LOGGER.info("返回的信息:" + result);
                jsonObject = JSONObject.fromObject(result);   //解析json格式数据
                String resultCode = jsonObject.getString("resultCode");
                String resultDetail = jsonObject.getString("resultDetail");
                //todo 保存返回信息到DB
    5:补充:
    用到的加密工具类DiscuzAuthcode.java和 发送请求的类HttpConnection.java

    6:用到的jar文件
    json-lib-2.2.3-jdk15.jar,json-taglib-0.4.1.jar,ezmorph-1.0.6.jar,commons-xxx.jar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值