用HTTP的PUT方式发送请求

3 篇文章 0 订阅

来源:http://zhidao.baidu.com/link?url=BA96AeYlYEefAKzwT-9O1MaZisfsK62CnCFz2SZjcc_U2UiahY4eplNzzdZFlmH5N4XVIs4AIccBGMC6OLfEXq

例如:

 public static String sendGetResult(String url,  int timeOut, String charset)
   {
 
     BufferedReader br = null;
     InputStreamReader isr = null;
     InputStream is = null;
     HttpURLConnection huc = null;
 
     StringBuffer result = new StringBuffer("");
     URL newUrl = null;
     String line = null;
     try {
       newUrl = new URL(url);
       huc = (HttpURLConnection)newUrl.openConnection();
 
       huc.setRequestMethod("PUT");
 
       if (timeOut > 0) {
         huc.setConnectTimeout(timeOut);
         huc.setReadTimeout(timeOut);
       }
 
       int state = huc.getResponseCode();
       if ((state > 207) || (state < 200))
         return null;
       int state;
       is = huc.getInputStream();
       isr = charset != null ? new InputStreamReader(is, charset) : 
         new InputStreamReader(is);
       br = new BufferedReader(isr);
       while ((line = br.readLine()) != null) {
         result.append("\r\n");
         result.append(line);
       }
       String str1 = result.toString().trim();
       return str1;
     }
     catch (IOException ioe)
     {
       return null;
     } catch (OutOfMemoryError menorException) {
       System.out.println("发送PUT请求内存溢出,内存占用:" + 
         result.capacity());
       System.out.println("本溢出请求地址:" + url);
       System.gc();
       return null;
     } catch (Exception e) {
       e.printStackTrace();
       System.out.println("发送Put请求异常!" + e.getMessage());
       return null;
     }
     finally {
       try {
         if (is != null) {
           is.close();
         }
         if (isr != null) {
           isr.close();
         }
         if (br != null) {
           br.close();
         }
         if (huc != null) {
           huc.disconnect()();
         }
         result = null;
       } catch (Exception e) {
         System.out.println("发送Put请求时关闭流和对象出错");
       }
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值