CSS font stacks, developer FAQs & web standards

Your banner ad here

WestNIC provides reliable web hosting services

Top Canadian Hotels no booking fees from Victoria BC to Nova Scotia

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQ answers.

Java Web clients

Q: How can I download a Web page and write it to a file?

A: The standard Java input/output and network APIs make it quite easy to acquire content from the Internet and write files. Writing from a stream input to a file output is the slightly complex part. The example below uses a copy(InputStream, OutputStream) method adapted from a version in Java I/O by Elliotte Rusty Harold.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I download a Web page and write it to a file?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: My Java Web client gets the wrong site!

A: Many Web sites use virtual hosting, which requires you send an HTTP Host header with your request.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
My Java Web client gets the wrong site!

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How can I set a code name identifier for my feed reader?

A: Your application should pass a User-Agent header in the HTTP request with the code name of your application as the header value. Web browser user agents often include the full version and build number for the application and the software platform it was built for. Feed readers tend to use simpler name and version number combination. See the Java example below.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I set a code name identifier for my feed reader?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Java networking API

Q: How do I get the domain name from a URL?

A: If you want to extract a domain name from an existing URL object, use the URL's getHost() method.

String urlString = "http://www.test.example/path/page.htm";

URL url = new URL(urlString);

String domain = url.getHost();
          

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How do I get the registered domain name from a URL?

A: The URL getHost() method returns the fully qualified domain name (FQDN). To get the registered domain name you would need to break the FQDN further to remove any subdomains. The example below uses the String lastIndexOf() method to get the index of the dot separators and substring() to extract codestyle.org from www.codestyle.org.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How do I get the registered domain name from a URL?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How do I get a domain name from an IP address?

A: It is possible to do a reverse name look-up for an IP address using the java.net.InetAddress class. The example below first gets an instance of the class using the static getByName(String) method, then applies the getHostName() method to it. The method will only resolve a domain name if there is one configured for the host, otherwise the method returns the original IP address.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How do I get a domain name from an IP address?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Add this page to your chosen social bookmarking service

Style warning, please read

Home · CSS · Java · Javascript · HTML · Help · Log