java从流,写入取出数据函数


   /**
  *根据url地址和要发送的字符串通过http协议用post方法把xml字符串用流格式发送到指定url的网页。
  * @param sUrl 要发送的url地址
  * @param sXML 要发送的XML
  * @param sContent 要发送的字符串
  * @throws
  * @return 通过该次发送到目标网页并返回的结果字符串。
  */

   public String doHttpPost(String strUrl, String sXML) throws
           IOException {
       StringBuffer strReturnVal = new StringBuffer("");
       HttpURLConnection httpURLConnection = null;
       OutputStreamWriter osw = null;
       try {
           //开始发送的时间
           java.net.URL url = new java.net.URL(strUrl);
           java.net.URLConnection urlcn = url.openConnection();
           httpURLConnection = (HttpURLConnection) urlcn;
           httpURLConnection.setRequestMethod("POST");
           System.setProperty("sun.net.client.defaultConnectTimeout", "6000");
           System.setProperty("sun.net.client.defaultReadTimeout", "6000");

           httpURLConnection.setDoOutput(true);
           httpURLConnection.setDoInput(true);
           osw = new OutputStreamWriter(httpURLConnection.
                                        getOutputStream(),
                                        "GB2312");
           osw.write(sXML);
       } catch (Exception e) {
           e.printStackTrace();
           return null;
       } finally {
           if (osw != null) {
               osw.close();
           }
       }

       InputStreamReader isr = null;
       try {
           isr = new InputStreamReader(httpURLConnection.
                                       getInputStream(),
                                       "GB2312");

           while (true) {
               char[] c = new char[1024];
               int ilen = isr.read(c);
               if (ilen <= 0) {
                   break;
               }
               strReturnVal = strReturnVal.append(new String(c, 0, ilen));
           }
           return strReturnVal.toString();
       } catch (Exception e) {
           e.printStackTrace();
           return null;
       } finally {
           if (isr != null) {
               isr.close();
           }
       }
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值