ใน Java วิธีอ่านเนื้อหาไฟล์ GitHub โดยใช้ยูทิลิตี้ HttpURLConnection + ConvertStreamToString()
เผยแพร่แล้ว: 2017-12-30ในบทช่วยสอน Java นี้ เราจะทำตามขั้นตอนต่างๆ เพื่อดึงเนื้อหา GitHub URL โดยใช้ HttpURLConnection กล่าวอีกนัยหนึ่งด้านล่างคือ Java API เพื่อรับเนื้อหาไฟล์จาก GitHub
อินสแตนซ์ HttpURLConnection
แต่ละรายการใช้เพื่อสร้างคำขอเดียว แต่การเชื่อมต่อเครือข่ายพื้นฐานไปยังเซิร์ฟเวอร์ HTTP อาจถูกแชร์โดยอินสแตนซ์อื่นอย่างโปร่งใส getHeaderFields()
ส่งกลับแผนที่ที่ไม่สามารถแก้ไขได้ของฟิลด์ส่วนหัว คีย์แผนที่คือสตริงที่แสดงชื่อฟิลด์ส่วนหัวตอบกลับ ค่า Map แต่ละค่าคือ List of Strings ที่ไม่สามารถแก้ไขได้ซึ่งแสดงถึงค่าฟิลด์ที่เกี่ยวข้อง
มาเริ่มกันเลย:
- สร้างคลาส
CrunchifyLoadGithubContent.java
- เราจะดาวน์โหลดเนื้อหา: https://raw.githubusercontent.com/Crunchify/wp-super-cache/master/wp-cache.php (จากปลั๊กอิน: WP Super Cache Github Repo)
- รับฟิลด์ส่วนหัวทั้งหมดโดยใช้ getHeaderFields() API เราต้องการสิ่งนี้เพื่อดูว่า URL ด้านบนหรือ URL อื่น ๆ ถูกเปลี่ยนเส้นทางหรือไม่? หมายเหตุ: นี่เป็นทางเลือกทั้งหมด ในกรณีของการเปลี่ยนเส้นทาง HTTP 301 และ HTTP 302 สิ่งนี้จะช่วยได้
- สร้าง API
crunchifyGetStringFromStream( InputStream crunchifyStream)
เพื่อแปลง Stream เป็น String - พิมพ์ผลลัพธ์เดียวกันไปยังคอนโซล
หมายเหตุ: สถานะ HTTP 301 หมายความว่าทรัพยากร (หน้า) ถูกย้ายอย่างถาวรไปยังตำแหน่งใหม่ 302 คือเขาขอทรัพยากรอยู่ชั่วคราวภายใต้ URI อื่น ส่วนใหญ่ 301 เทียบกับ 302 มีความสำคัญสำหรับการจัดทำดัชนีในเครื่องมือค้นหา เนื่องจากโปรแกรมรวบรวมข้อมูลจะพิจารณาสิ่งนี้ในบัญชีและโอนอันดับของหน้าเมื่อใช้ 301
นอกจากนี้ยังมีข้อสันนิษฐานว่า – GitHub URL ต้องเป็นสาธารณะ
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package crunchify . com . tutorial ; import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . io . Reader ; import java . io . StringWriter ; import java . io . Writer ; import java . net . HttpURLConnection ; import java . net . URL ; import java . util . List ; import java . util . Map ; /** * @author Crunchify.com * */ public class CrunchifyLoadGithubContent { public static void main ( String [ ] args ) throws Throwable { String link = "https://raw.githubusercontent.com/Crunchify/All-in-One-Webmaster/master/all-in-one-webmaster-premium.php" ; URL crunchifyUrl = new URL ( link ) ; HttpURLConnection crunchifyHttp = ( HttpURLConnection ) crunchifyUrl . openConnection ( ) ; Map < String , List <String> > crunchifyHeader = crunchifyHttp . getHeaderFields ( ) ; // If URL is getting 301 and 302 redirection HTTP code then get new URL link. // This below for loop is totally optional if you are sure that your URL is not getting redirected to anywhere for ( String header : crunchifyHeader . get ( null ) ) { if ( header . contains ( " 302 " ) | | header . contains ( " 301 " ) ) { link = crunchifyHeader . get ( "Location" ) . get ( 0 ) ; crunchifyUrl = new URL ( link ) ; crunchifyHttp = ( HttpURLConnection ) crunchifyUrl . openConnection ( ) ; crunchifyHeader = crunchifyHttp . getHeaderFields ( ) ; } } InputStream crunchifyStream = crunchifyHttp . getInputStream ( ) ; String crunchifyResponse = crunchifyGetStringFromStream ( crunchifyStream ) ; System . out . println ( crunchifyResponse ) ; } // ConvertStreamToString() Utility - we name it as crunchifyGetStringFromStream() private static String crunchifyGetStringFromStream ( InputStream crunchifyStream ) throws IOException { if ( crunchifyStream ! = null ) { Writer crunchifyWriter = new StringWriter ( ) ; char [ ] crunchifyBuffer = new char [ 2048 ] ; try { Reader crunchifyReader = new BufferedReader ( new InputStreamReader ( crunchifyStream , "UTF-8" ) ) ; int counter ; while ( ( counter = crunchifyReader . read ( crunchifyBuffer ) ) ! = - 1 ) { crunchifyWriter . write ( crunchifyBuffer , 0 , counter ) ; } } finally { crunchifyStream . close ( ) ; } return crunchifyWriter . toString ( ) ; } else { return "No Contents" ; } } } |
ขณะดีบักฉันได้รับสิ่งนี้เป็นส่วนหนึ่งของค่า crunchifyHeader
นอกจากนี้ บทช่วยสอนนี้ใช้กับ repo สาธารณะของ Bitbucket ด้วย

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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
{ null = [ HTTP / 1.1200OK // this is what we are checking in above for loop. If 301 or 302 then get new URL. ] , X - Cache - Hits = [ 1 ] , ETag = [ "94a3eb8b3b5505f746aa8530667969673a8e182d" ] , Content - Length = [ 24436 ] , X - XSS - Protection = [ 1 ; mode = block ] , Expires = [ Mon , 27Oct201420 : 00 : 31GMT ] , X - Served - By = [ cache - dfw1825 - DFW ] , Source - Age = [ 14 ] , Connection = [ Keep - Alive ] , Server = [ Apache ] , X - Cache = [ HIT ] , Cache - Control = [ max - age = 300 ] , X - Content - Type - Options = [ nosniff ] , X - Frame - Options = [ deny ] , Strict - Transport - Security = [ max - age = 31536000 ] , Vary = [ Authorization , Accept - Encoding ] , Access - Control - Allow - Origin = [ https : //render.githubusercontent.com ] , Date = [ Mon , 27Oct201419 : 55 : 31GMT ] , Via = [ 1.1varnish ] , Keep - Alive = [ timeout = 10 , max = 50 ] , Accept - Ranges = [ bytes ] , Content - Type = [ text / plain ; charset = utf - 8 ] , Content - Security - Policy = [ default - src 'none' ] } |