Using Sockets To Retrieve Data Through HTTP

Using Sockets To Retrieve Data Through HTTP
Use the Java Socket object and the BufferedReader to retrieve the response of an HTTP web request.
1.  import java.awt.BorderLayout;
2.  import java.awt.GridBagConstraints;
3.  import java.awt.GridBagLayout;
4.  import java.io.BufferedReader;
5.  import java.io.IOException;
6.  import java.io.InputStreamReader;
7.  import java.io.PrintWriter;
8.  import java.net.Socket;
9.  
10. import javax.swing.JButton;
11. import javax.swing.JFrame;
12. import javax.swing.JPanel;
13. import javax.swing.JTextArea;
14. import javax.swing.JTextField;
15. import javax.swing.JToolBar;
16.  
17. public class Browser extends JFrame {
18.  
19.         private static final long serialVersionUID = 1L;
20.  
21.         private JPanel jContentPane = null;
22.  
23.         private JPanel jPanel = null;
24.  
25.         private JToolBar jToolBar = null;
26.  
27.         private JTextField jTextField = null;
28.  
29.         private JButton goButton = null;
30.  
31.         private JTextArea jTextArea = null;
32.        
33.         private Socket webSocket;  //  @jve:decl-index=0:
34.         private BufferedReader buffIn;
35.         private PrintWriter printOut;
36.  
37.         /**
38.          * This is the default constructor
39.          */
40.         public Browser() {
41.                 super();
42.                 initialize();
43.         }
44.  
45.         /**
46.          * This method initializes this
47.          *
48.          * @return void
49.          */
50.         private void initialize() {
51.                 this.setSize(300, 200);
52.                 this.setContentPane(getJContentPane());
53.                 this.setTitle("Browser");
54.         }
55.  
56.         /**
57.          * This method initializes jContentPane
58.          *
59.          * @return javax.swing.JPanel
60.          */
61.         private JPanel getJContentPane() {
62.                 if (jContentPane == null) {
63.                         jContentPane = new JPanel();
64.                         jContentPane.setLayout(new BorderLayout());
65.                         jContentPane.add(getJPanel(), BorderLayout.CENTER);
66.                         jContentPane.add(getJToolBar(), BorderLayout.NORTH);
67.                 }
68.                 return jContentPane;
69.         }
70.  
71.         /**
72.          * This method initializes jPanel       
73.          *      
74.          * @return javax.swing.JPanel   
75.          */
76.         private JPanel getJPanel() {
77.                 if (jPanel == null) {
78.                         GridBagConstraints gridBagConstraints = new GridBagConstraints();
79.                         gridBagConstraints.fill = GridBagConstraints.BOTH;
80.                         gridBagConstraints.gridy = 0;
81.                         gridBagConstraints.weightx = 1.0;
82.                         gridBagConstraints.weighty = 1.0;
83.                         gridBagConstraints.gridx = 0;
84.                         jPanel = new JPanel();
85.                         jPanel.setLayout(new GridBagLayout());
86.                         jPanel.add(getJTextArea(), gridBagConstraints);
87.                 }
88.                 return jPanel;
89.         }
90.  
91.         /**
92.          * This method initializes jToolBar     
93.          *      
94.          * @return javax.swing.JToolBar 
95.          */
96.         private JToolBar getJToolBar() {
97.                 if (jToolBar == null) {
98.                         jToolBar = new JToolBar();
99.                         jToolBar.add(getJTextField());
100.                         jToolBar.add(getGoButton());
101.                 }
102.                 return jToolBar;
103.         }
104.  
105.         /**
106.          * This method initializes jTextField   
107.          *      
108.          * @return javax.swing.JTextField       
109.          */
110.         private JTextField getJTextField() {
111.                 if (jTextField == null) {
112.                         jTextField = new JTextField();
113.                         jTextField.setText("www.geekpedia.com");
114.                 }
115.                 return jTextField;
116.         }
117.  
118.         /**
119.          * This method initializes goButton     
120.          *      
121.          * @return javax.swing.JButton 
122.          */
123.         private JButton getGoButton() {
124.                 if (goButton == null) {
125.                         goButton = new JButton();
126.                         goButton.setText("Go");
127.                         goButton.addMouseListener(new java.awt.event.MouseAdapter() {
128.                                 public void mouseReleased(java.awt.event.MouseEvent e) {
129.                                         openConnection();
130.                                 }
131.                         });
132.                 }
133.                 return goButton;
134.         }
135.        
136.         private void openConnection()
137.         {
138.                 try
139.                 {
140.                        jTextArea.setText(""); // Clear the text area
141.                         webSocket = new Socket(jTextField.getText(), 80);
142.                        
143.                         buffIn = new BufferedReader(new InputStreamReader(webSocket.getInputStream()));
144.                         printOut = new PrintWriter(webSocket.getOutputStream(), true);
145.                         printOut.println("GET index.html");
146.                         printOut.flush();
147.                         String currLine;
148.                         while((currLine = buffIn.readLine()) != null)
149.                         {
150.                                 jTextArea.setText(jTextArea.getText() + currLine);
151.                         }
152.                         printOut.close();
153.                         buffIn.close();
154.                         webSocket.close();
155.                 }
156.                 catch(IOException e)
157.                 {
158.                         e.printStackTrace();
159.                 }
160.         }
161.  
162.         /**
163.          * This method initializes jTextArea   
164.          *      
165.          * @return javax.swing.JTextArea       
166.          */
167.         private JTextArea getJTextArea() {
168.                 if (jTextArea == null) {
169.                         jTextArea = new JTextArea();
170.                         jTextArea.setLineWrap(true);
171.                 }
172.                 return jTextArea;
173.         }
174.  
175. }
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top