JAVA通过 URLConnection 获取HTTP接口

来源:互联网 发布:淘宝上买尼康d750港货 编辑:程序博客网 时间:2024/05/29 09:22

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

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


  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.     }  

0 0
原创粉丝点击