Java HTTP client

HTTP client

 

public static void main(String[] args) throws IOException {
    
  String url = "http://movie.xunlei.com/kankan_shangsheng_for_union.html";
  
  HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
        conn.connect();
        int code = conn.getResponseCode();
        if(code > 0 && code != HttpURLConnection.HTTP_OK) throw new IOException("response code error : " + code);
        String content = read(conn.getInputStream(), "utf-8");
        conn.disconnect();
        String[] lines = content.split("\n");
        //if(lines.length != 10) throw new IOException("content error : " + content + "; path : " + path);      
        for(final String line : lines) {
           if(line.trim().length() == 0){
            continue;
           }
          System.out.println(line);
        }  
 }//...
 
 private static  String read(InputStream in, String charset) throws IOException {
        int pos = -1;
        byte[] buf = new byte[1024*4];
        final int maxsize = 500*1024;
        int c = 0;
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024*4);
        while((pos = in.read(buf)) != -1 && c <= maxsize){
            c += pos;
            out.write(buf, 0, pos);
        }
        in.close();
        return (charset == null) ? new String(out.toByteArray()) : new String(out.toByteArray(), charset);
    }//...

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(java,html,C++,c,C#)