package cn.itcast.net.p2.ie_server;
import java.io.IOException;
import java.io.InputStream;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;public class MyBrowser {
/**
* @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException {Socket s = new Socket("192.168.1.100",8080);
//模拟浏览器,给tomcat服务端发送符合http协议的请求消息。 PrintWriter out = new PrintWriter(s.getOutputStream(),true); out.println("GET /myweb/1.html HTTP/1.1"); out.println("Accept: */*"); out.println("Host: 192.168.1.100:8080"); out.println("Connection: close"); out.println(); out.println(); InputStream in = s.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); String str =new String(buf,0,len); System.out.println(str); s.close(); //http://192.168.1.100:8080/myweb/1.html }}
package cn.itcast.net.p2.ie_server;
import java.io.IOException;
import java.io.InputStream;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;public class MyTomcat {
/**
* @param args * @throws IOException */ public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(9090);
Socket s = ss.accept(); System.out.println(s.getInetAddress().getHostAddress()+".....connected"); InputStream in = s.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); String text = new String(buf,0,len); System.out.println(text); //给客户端一个反馈信息。 PrintWriter out = new PrintWriter(s.getOutputStream(),true); out.println("<font color='red' size='7'>欢迎光临</font>"); s.close(); ss.close(); }}
package cn.itcast.net.p2.ie_server;
import java.io.IOException;
import java.io.InputStream;import java.net.URL;import java.net.URLConnection;public class URLDemo {
/**
* @param args * @throws IOException */ public static void main(String[] args) throws IOException {String str_url = "";
URL url = new URL(str_url); // System.out.println("getProtocol:"+url.getProtocol());// System.out.println("getHost:"+url.getHost());// System.out.println("getPort:"+url.getPort());// System.out.println("getFile:"+url.getFile());// System.out.println("getPath:"+url.getPath());// System.out.println("getQuery:"+url.getQuery()); // InputStream in = url.openStream(); //获取url对象的Url连接器对象。将连接封装成了对象:java中内置的可以解析的具体协议的对象+socket. URLConnection conn = url.openConnection(); // String value = conn.getHeaderField("Content-Type");// System.out.println(value); // System.out.println(conn); //sun.net.www.protocol.http.HttpURLConnection:http://192.168.1.100:8080/myweb/1.html InputStream in = conn.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); String text = new String(buf,0,len); System.out.println(text); in.close(); }}
package cn.itcast.net.p3.iegui;
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.io.InputStream;import java.net.URL;import javax.swing.JButton;
import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.SwingUtilities;import javax.swing.WindowConstants; /*** This code was edited or generated using CloudGarden's Jigloo* SWT/Swing GUI Builder, which is free for non-commercial* use. If Jigloo is being used commercially (ie, by a corporation,* company or business for any purpose whatever) then you* should purchase a license for each developer using Jigloo.* Please visit for details.* Use of Jigloo implies acceptance of these licensing terms.* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.*/public class MyBrowseGUI extends javax.swing.JFrame { private JTextField url_text; private JButton goto_but; private JScrollPane jScrollPane1; private JTextArea page_content;/**
* Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { MyBrowseGUI inst = new MyBrowseGUI(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); } public MyBrowseGUI() { super(); initGUI(); } private void initGUI() { try { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(null); { url_text = new JTextField(); getContentPane().add(url_text); url_text.setBounds(12, 36, 531, 44); url_text.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { url_textKeyPressed(evt); } }); } { goto_but = new JButton(); getContentPane().add(goto_but); goto_but.setText("\u8f6c \u5230"); goto_but.setBounds(555, 36, 134, 44); goto_but.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { goto_butActionPerformed(evt); } }); } { jScrollPane1 = new JScrollPane(); getContentPane().add(jScrollPane1); jScrollPane1.setBounds(12, 92, 676, 414); { page_content = new JTextArea(); jScrollPane1.setViewportView(page_content); } } pack(); this.setSize(708, 545); } catch (Exception e) { //add your error handling code here e.printStackTrace(); } } private void goto_butActionPerformed(ActionEvent evt) { showPage(); } private void url_textKeyPressed(KeyEvent evt) { if(evt.getKeyCode()==KeyEvent.VK_ENTER) showPage(); }private void showPage() {
try { String url_str = url_text.getText(); URL url = new URL(url_str); InputStream in = url.openConnection().getInputStream();//url.openStream(); page_content.setText(""); byte[] buf = new byte[1024]; int len = in.read(buf); String text = new String(buf,0,len,"utf-8"); page_content.setText(text); in.close(); } catch (Exception e) { // TODO: handle exception } }}