一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java如何执行html代码?java执行html代码的实例

时间:2017-02-20 编辑:简简单单 来源:一聚教程网


  1.  代码如下复制代码
    1. StringBuffer response = new StringBuffer();  

    2. HttpURLConnection connection = null;  

    3. try  

    4. {  

    5.     URL url = new URL("http://……");  

    6.     connection = (HttpURLConnection) url.openConnection();  

    7.     connection.connect();  

    8.     BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));  

    9.     String lines;  

    10.     while((lines = reader.readLine()) != null)  

    11.     {  

    12.         if(lines != null && lines.trim().length() > 0)  

    13.         {  

    14.             response.append(lines.trim());  

    15.         }  

    16.         else  

    17.         {  

    18.             continue;  

    19.         }  

    20.     }  

    21.     System.out.println(response.toString());  

    22.     reader.close();  

    23. }  

    24. catch(Exception e)  

    25. {  

    26.     throw e;  

    27. }  

    28. finally  

    29. {  

    30.     if(connection != null)  

    31.     {  

    32.         connection.disconnect();  

    33.     }  

    34. }


热门栏目