JAVA通过 URLConnection 获取HTTP接口

本文提供了一个用于调试接口的示例代码,通过该代码可以调用特定URL并获取返回的结果。示例展示了如何使用Java进行URL编码、设置连接超时时间、读取输入流等操作。

工作中遇到的,需要调试接口的时候要调用的,

有需要类似用处的朋友可以借鉴一下.


  1. public String getResult(String sName)  
  2.     {  
  3.         String result = "";  
  4.         try  
  5.         {  
  6.             URL uAddr = new URL("http://xxx.xxx.com/xxx/xxx.jsp?xxx="+java.net.URLEncoder.encode(sName,"UTF-8"));  
  7.             URLConnection conn = uAddr.openConnection();  
  8.             conn.setDoInput(true);  
  9.             conn.setDoOutput(false);  
  10.             conn.setConnectTimeout(2000);  
  11.             BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));  
  12.             String inputLine;  
  13.             while ((inputLine = in.readLine()) != null) result = inputLine;   
  14.         }  
  15.         catch(Exception e)  
  16.         {  
  17.             e.printStackTrace();  
  18.         }  
  19.   
  20.         return result;  
  21.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值