HttpURLConnection请求到响应


        HttpURLConnection conn = null;
        InputStream is = null;
        BufferedReader reader = null;
        String msg;

        try {

//httpUrl=http://localhost:18080/promptfile/sendErrorMessage?user=admin&pwd=test123&provinceId={0}&message={1}&nowDate={2}&exception=#{3}

//URLEncoder.encode将参数的特殊字符处理。解决由于特殊字符导致的接收请求的参数获取不到问题。         

httpUrl = MessageFormat.format(httpUrl,参数一,

                    URLEncoder.encode(message, "utf-8"),
                    String.valueOf(nowDate),
                    URLEncoder.encode(exception, "utf-8"));
            String[] urls = httpUrl.split("\\?");
            URL url = new URL(urls[0]);
            conn = (HttpURLConnection) url.openConnection();
            // 设定请求的方法为"POST",默认是GET    
            conn.setRequestMethod("POST");    
            // 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在    
            // http正文内,因此需要设为true, 默认情况下是false;    
            conn.setDoOutput(true);    
            // 设置是否从httpUrlConnection读入,默认情况下是true;    
            conn.setDoInput(true);    
            // Post 请求不能使用缓存    
            conn.setUseCaches(false);    
            // 设定传送的内容类型是可序列化的java对象    
            // (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)    
            //conn.setRequestProperty("Content-type", "application/x-java-serialized-object");  
            
            // 此处getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法,    
            // 所以在开发中不调用上述的connect()也可以)。   
            OutputStream outStrm = conn.getOutputStream();
            outStrm.write(urls[1].getBytes());
            outStrm.flush();
            outStrm.close();
            int resCode = conn.getResponseCode();
            if (resCode == HttpURLConnection.HTTP_OK) {
                is = conn.getInputStream();
                reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                StringBuffer sb = new StringBuffer();
                String strRead = null;
                while ((strRead = reader.readLine()) != null) {
                    sb.append(strRead);
                }
                msg = sb.toString();
                JSONObject jobj = JSONObject.fromObject(msg);
                String returnCode = (String) jobj.get("returnCode");
                if ("0".equals(returnCode)) {
                    logger.info("sendErrorMessage成功执行,同步错误信息。");
                }
            } else {
                logger.error("ResCode:" + resCode);
                msg = URLDecoder.decode(conn.getResponseMessage(), "UTF-8");
                logger.error("同步错误信息:" + msg);
            }
        } catch (IOException e) {
            logger.error("sendErrorMessage执行失败", e);
            throw new Exception("同步错误信息", e);
        } finally {
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(is);
            if (conn != null) {
                conn.disconnect();
            }
        }
        
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值