In Java How to Read GitHub File Contents using HttpURLConnection + ConvertStreamToString() utility

Load Github URL Content using HttpURLConnection In Java How to Read GitHub File Contents using HttpURLConnection + ConvertStreamToString() utility

In this Java Tutorial we will go over steps to retrieve GitHub URL content using HttpURLConnection. In other words below is a Java API to get a file content from GitHub.

Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances. getHeaderFields() Returns an unmodifiable Map of the header fields. The Map keys are Strings that represent the response-header field names. Each Map value is an unmodifiable List of Strings that represents the corresponding field values.

Now let’s get started:

  1. Create class CrunchifyLoadGithubContent.java
  2. We will download contents: https://raw.githubusercontent.com/Crunchify/All-in-One-Webmaster/master/all-in-one-webmaster-premium.php (this is a file from one of my plugin: All in One Webmaster)
  3. Get all Header Fields using getHeaderFields() API. We need this to find out if above URL or any other URL is getting redirected or not? Note: This is totally optional. In case of HTTP 301 and HTTP 302 redirection this will help.
  4. Create API crunchifyGetStringFromStream(InputStream crunchifyStream) to convert Stream to String.
  5. Print the same output to Console.

NOTE: HTTP Status 301 means that the resource (page) is moved permanently to a new location. 302 is that he requested resource resides temporarily under a different URI. Mostly 301 vs 302 is important for indexing in search engines as their crawlers take this in account and transfer page rank when using 301.

Also, there is an assumption that – GitHub URL needs to be public.

While debugging I got this as a part of crunchifyHeader value. Also, this tutorial applies to Bitbucket public repo also.

Get Github content in Java In Java How to Read GitHub File Contents using HttpURLConnection + ConvertStreamToString() utility

Have anything to add to this article? Please chime in and join the conversion.
twitter In Java How to Read GitHub File Contents using HttpURLConnection + ConvertStreamToString() utilityfacebook In Java How to Read GitHub File Contents using HttpURLConnection + ConvertStreamToString() utilitygoogle In Java How to Read GitHub File Contents using HttpURLConnection + ConvertStreamToString() utilitypinterest In Java How to Read GitHub File Contents using HttpURLConnection + ConvertStreamToString() utilitytumblr In Java How to Read GitHub File Contents using HttpURLConnection + ConvertStreamToString() utilitybuffer In Java How to Read GitHub File Contents using HttpURLConnection + ConvertStreamToString() utility