함수 getTextFromURL()

Jmnote (토론 | 기여)님의 2023년 4월 15일 (토) 22:03 판 (→‎Java)

1 개요

함수 getTextFromURL()
함수 get_http_content()

2 Bash

content=$(curl -s https://raw.githubusercontent.com/kubernetes/kubernetes/v1.26.3/.go-version)
echo content=[$content]

3 Go

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

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

func main() {
	content := getHTTPContent("https://raw.githubusercontent.com/kubernetes/kubernetes/v1.26.3/.go-version")
	fmt.Println(content)
}

4 Java

URL url = new URL("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
Scanner scanner = new Scanner(url.openStream(), "UTF-8");
scanner.useDelimiter("\\A");
String content = scanner.next();
scanner.close();
System.out.println(content);
URL url = new URL("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
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);
URL url = new URL("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
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);

5 JavaScript

var xhr = new XMLHttpRequest();
xhr.open("GET",'https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt',false);
xhr.send();
console.log( xhr.responseText );
function file_get_contents(url) {
	var xhr = new XMLHttpRequest();
	xhr.open("GET", url, false);
	xhr.send();
	return xhr.responseText;
}
console.log( file_get_contents('https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt') );

6 PHP

<?php
$content = file_get_contents('https://raw.githubusercontent.com/kubernetes/kubernetes/v1.26.3/.go-version');
var_dump($content);

7 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}