Skip to content

Instantly share code, notes, and snippets.

@tomball
Created August 12, 2018 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomball/5f1175ec7f3b1663c8d067e93131b4f1 to your computer and use it in GitHub Desktop.
Save tomball/5f1175ec7f3b1663c8d067e93131b4f1 to your computer and use it in GitHub Desktop.
Java example which downloads the content of a specified HTTPS URL. Includes instructions for building with j2objc.
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
/**
* Simple command-line example demonstrating how to download content from an
* HTTP or HTTPS URL.
*
* To build with j2objc (https://j2objc.org) on a Mac, run:
*
* $ export PATH=$PATH:$(J2OBJC_DISTRIBUTION_DIRECTORY)
* $ j2objc HttpsExample.java
* $ j2objcc HttpsExample.m
* $ ./a.out HttpsExample https://j2objc.org/ # use any valid URL.
*/
public class HttpsExample {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("usage: java HttpsExample <https://your.url.here>");
System.exit(1);
}
URL httpsUrl = new URL(args[0]);
HttpsURLConnection conn = (HttpsURLConnection)httpsUrl.openConnection();
try (
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
) {
String inputLine;
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment