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

猜你喜欢

转载自blog.csdn.net/weixin_39372979/article/details/80757903