Network basic in java


How to read web page via Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class ReadWebPage {
  public static void main(String[] args) {
    String urlValue = "https://thaihoanghai.wordpress.com";
    BufferedReader in = null;
    try {
      URL url = new URL(urlValue);
      in = new BufferedReader(new InputStreamReader(url.openStream()));

      String inputLine;
      while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

How to get code from a webpage

Return Code Explaination
200 Ok
301 Permanent redirect to another webpage
400 Bad request
404 Not found
<pre>import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class ReadCode {
  public static void main(String[] args) throws IOException {
    String urltext = "https://thaihoanghai.wordpress.com";
    URL url = new URL(urltext);
    int responseCode = ((HttpURLConnection) url.openConnection())
        .getResponseCode();
    System.out.println(responseCode);
  }
}</pre>

How to get Content Type / MIME Type

<pre>import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {
  public static void main(String[] args) throws IOException {
    String urltext = "https://thaihoanghai.wordpress.com";
    URL url = new URL(urltext);
    String contentType = ((HttpURLConnection) url.openConnection()).getContentType();
    System.out.println(contentType);
  }
}</pre>

How To Get HttpResponse Header In Java

import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class ResponseHeader {

  public static void main(String[] args) {

	try {

		URL obj = new URL("https://thaihoanghai.wordpress.com");
		URLConnection conn = obj.openConnection();
		Map<String, List<String>> map = conn.getHeaderFields();

		System.out.println("Printing Response Header...\n");

		for (Map.Entry<String, List<String>> entry : map.entrySet()) {
			System.out.println("Key : " + entry.getKey() + 
                                           " ,Value : " + entry.getValue());
		}

		System.out.println("\nGet Response Header By Key ...\n");
		List<String> server = map.get("Server");

		if (server == null) {
			System.out.println("Key 'Server' is not found!");
		} else {
			for (String values : server) {
				System.out.println(values);
			}
		}

		System.out.println("\n Done");

	} catch (Exception e) {
		e.printStackTrace();
	}

  }

}

 


Key : null ,Value : [HTTP/1.1 200 OK]
Key : X-Pingback ,Value : [https://thaihoanghai.wordpress.com/xmlrpc.php]
Key : Link ,Value : [<http://wp.me/2aFPl>; rel=shortlink]
Key : Date ,Value : [Sun, 28 Apr 2013 05:44:23 GMT]
Key : Transfer-Encoding ,Value : [chunked]
Key : Vary ,Value : [Cookie, Accept-Encoding]
Key : X-hacker ,Value : [If you're reading this, you should visit automattic.com/jobs and apply to join the fun, mention this header.]
Key : Content-Type ,Value : [text/html; charset=UTF-8]
Key : Connection ,Value : [keep-alive]
Key : Server ,Value : [nginx]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: