A Web Page Source Viewer : HttpURLConnection « Network Protocol « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Network Protocol » HttpURLConnection 




A Web Page Source Viewer
    

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Main extends JFrame {
  JButton go = new JButton("Get Source");

  JTextArea codeArea = new JTextArea();

  public Main() {
    JPanel inputPanel = new JPanel(new BorderLayout(33));
    go.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          URL pageURL = new URL("http://www.google.com");
          HttpURLConnection urlConnection = (HttpURLConnectionpageURL.openConnection();
          int respCode = urlConnection.getResponseCode();
          String response = urlConnection.getResponseMessage();
          codeArea.setText("HTTP/1.x " + respCode + " " + response + "\n");
          int count = 1;
          while (true) {
            String header = urlConnection.getHeaderField(count);
            String key = urlConnection.getHeaderFieldKey(count);
            if (header == null || key == null) {
              break;
            }
            codeArea.append(urlConnection.getHeaderFieldKey(count": " + header + "\n");
            count++;
          }
          InputStream in = new BufferedInputStream(urlConnection.getInputStream());
          Reader r = new InputStreamReader(in);
          int c;
          while ((c = r.read()) != -1) {
            codeArea.append(String.valueOf((charc));
          }
          codeArea.setCaretPosition(1);
        catch (Exception ee) {
        }
      }
    });
    inputPanel.add(BorderLayout.EAST, go);
    JScrollPane codeScroller = new JScrollPane(codeArea);
    add(BorderLayout.NORTH, inputPanel);
    add(BorderLayout.CENTER, codeScroller);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(700500);
    this.setVisible(true);
  }

  public static void main(String[] args) {
    JFrame webPageSourceViewer = new Main();
  }
}

   
    
    
    
  














Related examples in the same category
1.Get the date of a url connection
2.Get the document expiration date
3.Get the document Last-modified date
4.Show the content type
5.Show the content length
6.Display request method
7.Get response code
8.Display response message
9.Display header information
10.Download and display the content
11.Reading from a URLConnection
12.Use BufferedReader to read content from a URL
13.Check if a page exists
14.Identify ourself to a proxy
15.Connect through a Proxy
16.Preventing Automatic Redirects in a HTTP Connection
17.Converting x-www-form-urlencoded Data
18.Getting the Cookies from an HTTP Connection
19.Sending a Cookie to an HTTP Server
20.Getting the Response Headers from an HTTP Connection
21.java.net.Authenticator can be used to send the credentials when needed
22.Grabbing a page using socket
23.Http Header Helper
24.Http connection Utilities
25.Available Port Finder
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.