HttpClient发送消息

 /**
     * 发送消息
     * @param text
     * @throws IOException
     * @throws UnsupportedEncodingException
     */
    public void send(String text) throws UnsupportedEncodingException, IOException
    {
        long startTime=System.currentTimeMillis();
        HttpURLConnection urlConnection = null;
        OutputStreamWriter osw=null;
        BufferedReader reader=null;
        String response =null;
        try
        {

            // 发送请求
            URL url = new URL(this.requestUrl);
            urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            // 设置超时时间
            urlConnection.setConnectTimeout(5000);
//            urlConnection.setReadTimeout(10000);
            // POST请求
            urlConnection.setRequestMethod(Constants.REQUEST_POST);
            // 不适用缓存
            urlConnection.setUseCaches(false);

            urlConnection.setRequestProperty("Content-Type", "text/xml; charset=" + Constants.ENCODING_UTF8);
            urlConnection.setRequestProperty("Connection", "close");
             osw = new OutputStreamWriter(urlConnection.getOutputStream(), Constants.ENCODING_UTF8);
            osw.write(text + "\r\n");
            osw.flush();

//            log.debug("Send Request info:" + text);

            // 读取请求返回值
            StringBuffer sb = new StringBuffer();
            InputStream errorStream = urlConnection.getErrorStream();
            if (errorStream != null)
            {
                BufferedReader error = new BufferedReader(new InputStreamReader(errorStream, Constants.ENCODING_UTF8));
                String line;
                while ((line = error.readLine()) != null)
                {
                    sb.append(new String(line.getBytes(Constants.ENCODING_UTF8), Constants.ENCODING_UTF8));
                }
                errorStream.close();
                error.close();
            }
             reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),
                    Constants.ENCODING_UTF8));
            String line;
            while ((line = reader.readLine()) != null)
            {
                sb.append(new String(line.getBytes(Constants.ENCODING_UTF8), Constants.ENCODING_UTF8));
            }

             response = sb.toString();
//            log.debug("Response info:" + response);
        }
        finally
        {
            if(osw!=null){
                osw.close();
            }
            if(reader!=null){
                reader.close();
            }
            if(urlConnection!=null)
            {
                urlConnection.disconnect();
            }
            StringBuffer sb=new StringBuffer();
            sb.append("request xml =").append(text).append("\r\nresponse=").append(response);
            sb.append(",task time=").append((System.currentTimeMillis()-startTime)).append("ms");
            log.info(sb);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值