함수 getTextFromURL() 편집하기

경고: 로그인하지 않았습니다. 편집을 하면 IP 주소가 공개되게 됩니다. 로그인하거나 계정을 생성하면 편집자가 사용자 이름으로 기록되고, 다른 장점도 있습니다.

편집을 취소할 수 있습니다. 이 편집을 되돌리려면 아래의 바뀐 내용을 확인한 후 게시해주세요.

최신판 당신의 편집
1번째 줄: 1번째 줄:
==개요==
==개요==
;함수 getTextFromURL()
{{DISPLAYTITLE:함수 get_http_content()}}
;함수 get_http_content()
;함수 get_http_content()


22번째 줄: 22번째 줄:


func getHTTPContent(url string) string {
func getHTTPContent(url string) string {
resp, err := http.Get(url)
response, err := http.Get(url)
if err != nil {
if err != nil {
return ""
return ""
}
}
defer resp.Body.Close()
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
contentBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
if err != nil {
return ""
return ""
}
}
return string(bodyBytes)
return string(contentBytes)
}
}


38번째 줄: 38번째 줄:
fmt.Println(content)
fmt.Println(content)
}
}
</syntaxhighlight>
==Java==
[[분류: Java]]
<syntaxhighlight lang='java' run>
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
class App {
    public static void main(String args[]) throws IOException {
        URL url = new URL("https://raw.githubusercontent.com/kubernetes/kubernetes/v1.26.3/.go-version");
        Scanner scanner = new Scanner(url.openStream(), "UTF-8");
        scanner.useDelimiter("\\A");
        String content = scanner.next();
        scanner.close();
        System.out.println(content);
    }
}
</syntaxhighlight>
<syntaxhighlight lang='java' run>
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
class App {
    public static void main(String args[]) throws IOException {
        URL url = new URL("https://raw.githubusercontent.com/kubernetes/kubernetes/v1.26.3/.go-version");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String line;
        String content = "";
        while ((line = in.readLine()) != null) content += line + System.getProperty("line.separator");
        System.out.println(content);
    }
}
</syntaxhighlight>
<syntaxhighlight lang='java' run>
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
class App {
    public static void main(String args[]) throws IOException {
        URL url = new URL("https://raw.githubusercontent.com/kubernetes/kubernetes/v1.26.3/.go-version");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String line;
        List<String> lines = new ArrayList<String>();
        while ((line = in.readLine()) != null) lines.add(line);
        String content = String.join(System.getProperty("line.separator"), lines);
        System.out.println(content);
    }
}
</syntaxhighlight>
==JavaScript==
[[category: JavaScript]]
<syntaxhighlight lang='JavaScript' run>
fetch('https://raw.githubusercontent.com/kubernetes/kubernetes/v1.26.3/.go-version')
    .then(response => response.text())
    .then(text => console.log(text));
</syntaxhighlight>
<syntaxhighlight lang='JavaScript' run>
const fetchText = async function(url) {
    const response = await fetch(url);
    const data = await response.text();
    console.log( data );
};
fetchText('https://raw.githubusercontent.com/kubernetes/kubernetes/v1.26.3/.go-version');
</syntaxhighlight>
<syntaxhighlight lang='JavaScript' run>
var xhr = new XMLHttpRequest();
xhr.open("GET",'https://raw.githubusercontent.com/kubernetes/kubernetes/v1.26.3/.go-version',false);
xhr.send();
console.log( xhr.responseText );
</syntaxhighlight>
<syntaxhighlight lang='JavaScript' run>
function getTextFromURL(url) {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.send();
return xhr.responseText;
}
console.log( getTextFromURL('https://raw.githubusercontent.com/kubernetes/kubernetes/v1.26.3/.go-version') );
</syntaxhighlight>
==Objective-C==
[[category: Objective-C]]
<syntaxhighlight lang='objc'>
//  ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <NSURLConnectionDelegate> {
    NSURLConnection* urlconn;
}
@end
</syntaxhighlight>
<syntaxhighlight lang='objc'>
//  ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    urlconn = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt"]] delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
@end
</syntaxhighlight>
</syntaxhighlight>


162번째 줄: 47번째 줄:
$content = file_get_contents('https://raw.githubusercontent.com/kubernetes/kubernetes/v1.26.3/.go-version');
$content = file_get_contents('https://raw.githubusercontent.com/kubernetes/kubernetes/v1.26.3/.go-version');
var_dump($content);
var_dump($content);
</syntaxhighlight>
==Ruby==
[[category: Ruby]]
<syntaxhighlight lang='Ruby'>
require 'open-uri'
h = open('https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt);
contents = h.read
print contents
</syntaxhighlight>
==UnityScript==
[[category: UnityScript]]
<syntaxhighlight lang='javascript'>
var www:WWW = new WWW("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
yield www;
Debug.Log(www.text);
</syntaxhighlight>
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[함수 readAllText()]]
* [[함수 get_http_code()]]
* [[함수 file_get_contents()]]
* [[함수 getHTTPCode()]]
* [[함수 getHTTPContentType()]]

제타위키에서의 모든 기여는 크리에이티브 커먼즈 저작자표시-동일조건변경허락 3.0 라이선스로 배포된다는 점을 유의해 주세요(자세한 내용에 대해서는 제타위키:저작권 문서를 읽어주세요). 만약 여기에 동의하지 않는다면 문서를 저장하지 말아 주세요.
또한, 직접 작성했거나 퍼블릭 도메인과 같은 자유 문서에서 가져왔다는 것을 보증해야 합니다. 저작권이 있는 내용을 허가 없이 저장하지 마세요!

취소 편집 도움말 (새 창에서 열림)

이 문서에서 사용한 틀: