2018년 3월 24일 토요일

[Java] Download Http File with URL openStream()


1. Download Http File with URL openStream()

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package bloggerpostsnippetjava;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

public class NetworkHttpUrl {
    
    NetworkHttpUrl() {
    }
    
    String readFromStream( InputStream is ) {
        Scanner scanner;
        scanner = new Scanner( is, "UTF-8" );
        String s = scanner.useDelimiter("\\A").next();
        scanner.close();
        return s; 
    }
    
    String getUrl(String urlStr) throws MalformedURLException, IOException {
        URL url = new URL(urlStr);
        return readFromStream(url.openStream());
    }
    public static void main( String[] args ) {
        try {
            NetworkHttpUrl url = new NetworkHttpUrl();
            String body = url.getUrl("https://www.google.com");
            System.out.println(body);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



2. Http GET with URLConnection()

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class HttpClient {
 
    static void testGet(String urlStr, String param) throws Exception {
        URL url;
        if( param != null ) {
            param = "name=" + URLEncoder.encode(param);
            url = new URL(urlStr + "?" + param);
        } else {
            url = new URL(urlStr);
        }
        URLConnection conn = url.openConnection();
        conn.setUseCaches(false);
        InputStream is = conn.getInputStream();
        Scanner scan = new Scanner(is);
        while(scan.hasNext()) {
            System.out.println(scan.nextLine());
        }
        scan.close();
    }
    
    public static void main(String[] args) {
        try {
            testGet("http://google.com", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



3. Http POST with URLConnection()

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
static void testPost() throws Exception {
        String param = "name=" + URLEncoder.encode("value");
        URL url = new URL("http://www.google.co.kr");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestProperty("ContentType", "application/x-www-form-urlencoded");
        
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        out.writeBytes(param);
        out.flush();
        out.close();
        
        InputStream is = conn.getInputStream();
        Scanner scan = new Scanner(is);
        while(scan.hasNext()) {
            System.out.println(scan.nextLine());
        }
        scan.close();
    }




댓글 없음:

댓글 쓰기