Java Reference
In-Depth Information
is granted access to the resource (and any other resource belonging to the
realm).
Greg Stein maintains a testing server at http://test.webdav.org/ that can
be used to test basic authentication and more. For example, when you specify ht-
tp://test.webdav.org/auth-basic/ in your browser, you'll be challenged
with a 401 response, as the application in Listing 9-14 demonstrates.
Listing 9-14. Demonstrating the need for basic authentication by outputting the server's various
HTTP headers
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
class BasicAuthNeeded
{
public static void main(String[] args) throws IOExcep-
tion
{
String s = "http://test.webdav.org/auth-basic/";
URL url = new URL(s);
URLConnection urlc = url.openConnection();
Map<String,List<String>>
hf
=
urlc.getHeaderFields();
for (String key: hf.keySet())
System.out.println(key+":
"+urlc.getHeaderField(key));
System.out.println(((HttpURLConnection)
urlc).getResponseCode());
}
}
Search WWH ::




Custom Search