In our application, sometimes we will test whether a file exists, whether a picture exists, but do not go to the real download, especially when the file is relatively large. So this time, how should our code be written? The following is a way to implement the JDK's own API: using the HTTP head method.
Import java.io.IOException;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Org.junit.Test;
public class Urltest {
@Test public
void Testurl () {
String urlstr= "https://www.broward.org/EasyPay/ Learning/documents/timecardsschedulessupervuseguide.pdf ";
try {
long begin=system.currenttimemillis ();
URL url=new url (urlstr);
HttpURLConnection httpurlconnection= (httpurlconnection) url.openconnection ();
Httpurlconnection.setrequestmethod ("Head");
int Responsecode=httpurlconnection.getresponsecode ();
SYSTEM.OUT.PRINTLN ("-----" +responsecode);
Long End=system.currenttimemillis ();
System.out.println ("--time Consumed---" + (end-begin));
} catch (Malformedurlexception e) {
e.printstacktrace ();
} catch (IOException e) {
e.printstacktrace (); c21/>}}}
If the Fiddler 4 is simulated, its message is as follows: