Chapters
HttpURLConnection is a URLConnection with support for HTTP-specific features. See the spec for details. This class is a subclass of URLConnection. You should be knowledgeable about URLConnection first before reading this tutorial. I already discussed URLConnection in this article.
This example demonstrates HttpURLConnection.
We can use HttpURLConnection to handle redirects. Typically, redirects return response codes of Found(302), Moved Permanently(301) and See Other(303). More information about redirect response codes can be found in this article.
This example demonstrates on how to handle redirects manually.
In the example above, I set it to false because I want to manually handle redirects. Manually handling redirects is good when dealing with a redirect from http to https and vice versa.
Sometimes, a URLConnection will follow a redirect even we set
Note that not every redirect has redirection response codes. For example, some fle hosting site returns 404 not found response code if the requested file is not found and redirects clients to another URL.
Before testing the example below, assuming you're using apache server, create a directory in your document root directory and name it test. Then, create a php file and name it
HttpsURLConnection extends HttpURLConnection with support for https-specific features. This class is recommended to be used when opening a connection with a URL that has HTTPS protocol. See this article and RFC 2818 for more details on the https specification.
This example demonstrates HttpsURLConnection.
HttpURLConnection
HttpURLConnection is a URLConnection with support for HTTP-specific features. See the spec for details. This class is a subclass of URLConnection. You should be knowledgeable about URLConnection first before reading this tutorial. I already discussed URLConnection in this article.
This example demonstrates HttpURLConnection.
import java.net.*; public class SampleClass{ public static void main(String[] args) throws Exception{ URL url = new URL("https://www.google.com"); HttpURLConnection huc = (HttpURLConnection)url.openConnection(); System.out.println ("Request Method: " + huc.getRequestMethod()); System.out.println ("Response Code: " + huc.getResponseCode()); System.out.println ("Response Message: " + huc.getResponseMessage()); System.out.println ("Using Proxy? " + huc.usingProxy()); } } Result Request Method: GET Response Code: 200 Response Message: OK Using Proxy? FalseThe methods that I used above are pretty straightforward to understand.
getRequestMethod()
returns the request method of the domain like POST, GET, PUT, etc. You can find more about HTTP request methods in this article.
getResponseCode()
returns the HTTP response code of the domain like 404, 302, 200, etc. getResponseMessage()
returns the message associated with response code. You can find more information about response codes and messages in this article. usingProxy()
returns a boolean value that denotes if the domain uses proxy or not.
Handling Redirects
We can use HttpURLConnection to handle redirects. Typically, redirects return response codes of Found(302), Moved Permanently(301) and See Other(303). More information about redirect response codes can be found in this article.
This example demonstrates on how to handle redirects manually.
import java.net.*; public class SampleClass{ public static void main(String[] args) throws Exception{ URL url = new URL("http://www.twitter.com"); HttpURLConnection huc = (HttpURLConnection)url.openConnection(); HttpURLConnection. setFollowRedirects(false); huc.setInstanceFollowRedirects(true); System.out.println ("Original URL: " +huc.getURL()); int responseCode = huc.getResponseCode(); while( responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_SEE_OTHER){ URL redirectURL = new URL(huc.getHeaderField("Location")); huc = (HttpURLConnection)redirectURL.openConnection(); System.out.println("Redirect URL: " + huc.getURL()); responseCode = huc.getResponseCode(); } } } Result Original URL: http://www.twitter.com Redirect URL: https://www.twitter.com Redirect URL: https://twitter.comIn the example above, we see that
http://www.twitter.com
has two redirects. getHeaderField("Location")
returns the "Location" http header field value which is the redirect URL. setFollowRedirects(boolean set)
is a static method that enables/disables automatic redirect follow of every HttpURLConnection in our program.
In the example above, I set it to false because I want to manually handle redirects. Manually handling redirects is good when dealing with a redirect from http to https and vice versa.
setInstanceFollowRedirects(boolean followRedirects)
determines if the instance that calls the method follows redirects. I set huc
follow redirects flag to true because I wanna this instance to follow redirects.
Sometimes, a URLConnection will follow a redirect even we set
setFollowRedirects()
to false. Take a look at this example.
import java.net.*; public class SampleClass{ public static void main(String[] args) throws Exception{ URL url = new URL("https://dropapk.to"); HttpURLConnection huc = (HttpURLConnection)url.openConnection(); HttpURLConnection. setFollowRedirects(false); huc.setInstanceFollowRedirects(true); System.out.println ("Original URL: " +huc.getURL()); System.out.println ("Response Code: " + huc.getResponseCode()); System.out.println ("Redirect URL: " +huc.getURL()); } } Result Original URL: https://dropapk.to Response Code: 200 Redirect URL: https://drop.downloadTo prevent automatic redirect in this case, we need to set
setInstanceFollowRedirects()
method to false. Take a look at this example.
import java.net.*; public class SampleClass{ public static void main(String[] args) throws Exception{ URL url = new URL("https://dropapk.to"); HttpURLConnection huc = (HttpURLConnection)url.openConnection(); huc.setInstanceFollowRedirects(false); System.out.println ("Original URL: " +huc.getURL()); System.out.println ("Response Code: " + huc.getResponseCode()); System.out.println ("Redirect URL: " +huc.getURL()); } } Result Original URL: https://dropapk.to Response Code: 301 Redirect URL: https://dropapk.toEven we set
setFollowRedirects()
to true, huc
won't automatically follow redirects. Moreover, we can still get redirects manually since the response code is 301(Permanent Redirect).
Note that not every redirect has redirection response codes. For example, some fle hosting site returns 404 not found response code if the requested file is not found and redirects clients to another URL.
Reading From and Writing to a URL
Before testing the example below, assuming you're using apache server, create a directory in your document root directory and name it test. Then, create a php file and name it
url_con_test.php
. Then, put this in your php file.
<?php echo str_shuffle($_GET['text']); ?>This example demonstrates reading from and writing to a URL.
import java.net.*; import java.io.*; public class SampleClass{ public static void main(String[] args) throws IOException, MalformedURLException{ URL url = new URL( "http://localhost/test/url_con_test.php?text=String"); HttpURLConnection uc = (HttpURLConnection)url.openConnection(); uc.setRequestMethod("GET"); uc.setDoOutput(true); uc.setConnectTimeout(5000); uc.setReadTimeout(3000); uc.addRequestProperty ("Accept-Language","en-US, en-BR; q=0.8"); String stringToShuffle = new String("Brainy Ghosts".getBytes(), "UTF-8"); OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream()); out.write("text="+stringToShuffle); out.close(); BufferedReader in = new BufferedReader( new InputStreamReader( uc.getInputStream())); String result = null; while ((result = in.readLine()) != null) System.out.println (new String(result.getBytes(), "UTF-8")); in.close(); } } Result(may vary) trngSi
setRequestMethod
method sets the method for the URL request, one of: GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE are legal, subject to protocol restrictions. The default method is GET. The example above is similar to the example in this article that I've written. You can see the full explanation of the example there.
HttpsURLConnection
HttpsURLConnection extends HttpURLConnection with support for https-specific features. This class is recommended to be used when opening a connection with a URL that has HTTPS protocol. See this article and RFC 2818 for more details on the https specification.
This example demonstrates HttpsURLConnection.
import java.io.IOException; import java.net.*; import java.security.cert.Certificate; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLPeerUnverifiedException; public class SampleClass{ public static void main(String[] args) throws MalformedURLException, SSLPeerUnverifiedException, IOException{ URL url = new URL("https://www.google.com"); HttpsURLConnection https = (HttpsURLConnection)url.openConnection(); System.out.println ("URL: " +https.getURL()); System.out.println ("Response Code: " + https.getResponseCode()); System.out.println ("Cipher Suite: " + https.getCipherSuite()); System.out.println(); //dump domain certificates Certificate[] certs = https.getServerCertificates(); for(Certificate cert : certs){ System.out.println ("Cert Type: " + cert.getType()); System.out.println ("Cert Hash Code: " + cert.hashCode()); System.out.println ("Cert Public Key Algorithm: " + cert.getPublicKey().getAlgorithm()); System.out.println ("Cert Public Key Format: " + cert.getPublicKey().getFormat()); System.out.println(); } } } Result URL: https://www.google.com Response Code: 200 Cipher Suite: TLS_AES_256_GCM_SHA384 Cert Type: X.509 Cert Hash Code: 1129315267 Cert Public Key Algorithm: EC Cert Public Key Format: X.509 Cert Type: X.509 Cert Hash Code: 266225904 Cert Public Key Algorithm: RSA Cert Public Key Format: X.509 Cert Type: X.509 Cert Hash Code: 1081005260 Cert Public Key Algorithm: RSA Cert Public Key Format: X.509
No comments:
Post a Comment