Java基于HTTP协议的一个DEMO.(包含SWING)
package Mulder .com;import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class WebRequest extends JFrame{
public Socket socket ;
public String strHttp ;
public static final int WEB_PORT = 8080;
public String hostName = "localhost";
public PrintWriter pw ;
public BufferedReader br;
public JButton btnOk = new JButton("连接");
public JTextField txtAddress = new JTextField("localhost");
public JTextArea ta = new JTextArea();
public JScrollPane jsp = new JScrollPane();
public JPanel panNorth = new JPanel();
public JPanel panCenter = new JPanel();
public WebRequest() {
this.setSize(700,500);
Container cont = this.getContentPane();
cont.add(panNorth,BorderLayout.NORTH);
cont.add(panCenter);
panNorth.setLayout(new BorderLayout());
panNorth.add(txtAddress);
panNorth.add(btnOk,BorderLayout.EAST);
panCenter.setLayout(new BorderLayout());
panCenter.add(jsp);
jsp.getViewport().add(ta);
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
sendHttpRequest();//向服务器发送请求
}
});
this.setLocationRelativeTo(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void sendHttpRequest() {
try {
//发送GET请求
strHttp = "GET /index.jsp HTTP/1.1\r\n";/*
请求报文是指客户端浏览器向网络服务器请求页面时,
发送到服务器端的字符串,必须符合HTTP协议的格式要求
*/
strHttp += "Host: " + hostName + ":" + WEB_PORT + "\r\n";
//表示客户端是否与服务器持续连接。
//连接服务器具
socket = new Socket(hostName,WEB_PORT);
pw = new PrintWriter(socket.getOutputStream(),true);
pw.println(strHttp);
//把请求报文发送给服务器端
System.out.println (strHttp);
System.out.println (socket);
//开始读取服务器返回的资源信息
br = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String str = "";
StringBuffer strContent= new StringBuffer();
do{
str = br.readLine();
strContent.append(str);
strContent.append("\r\n");
// System.out.println (str);
//是否读到完文件
}while(!str.toLowerCase().endsWith("</html>"));
ta.setText(strContent.toString());
br.close();
pw.close();
socket.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
WebRequest sr = new WebRequest();
}
}
--------------------------------------------------
package Mulder.com;
import java.net.*;
import java.io.*;
import java.util.*;
public class WebResponse {
public Socket socket;
public ServerSocket ss;
public BufferedReader in;
public PrintWriter out;
public static final int PORT = 8080;
public String reqStr;
public String reqFile;
public WebResponse() {
try {
ss = new ServerSocket(PORT);
System.out.println ("等待连接");
socket = ss.accept();
System.out.println (socket);//获得客户端连接
//获得输入流
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
//获得输出流
out = new PrintWriter(socket.getOutputStream(),true);
reciveRequest();//接收客户端的请求
sendFile();//向客户端发送文件
close();
} catch (IOException ie) {
ie.printStackTrace();
}
}
//关闭连接
public void close() throws IOException {
in.close();
out.close();
socket.close();
ss.close();
}
public void reciveRequest() throws IOException {
StringBuffer s = new StringBuffer();
reqStr = in.readLine();
System.out.println ("请求资源信息");
System.out.println (reqStr+":GET");
while(reqStr.length()>1) {
s.append(reqStr+"\r\n");
reqStr = in.readLine();
}
System.out.println (reqStr=s.toString());
}
//获得资源文件名
public String getURLString() {
String[] strAry = reqStr.split("\n");
String strTemp = strAry[0];//取第一行数据
//默认对空格进行分割
StringTokenizer stk = new StringTokenizer(strTemp);
stk.nextToken();
String urlString = stk.nextToken().substring(1);
System.out.println (urlString);
return urlString;
}
//将文件内容发送到客户端
public void sendFile() {
//向客户端发响应报文
File file = new File(this.getURLString());
if(!file.exists()) {
this.out.println("Http/1.1 404 OK");
this.out.println("Content-Type:text/html; charset=GB2312");
this.out.println();
System.out.println ("不存在");
out.println("<html>");
out.println("<body>");
out.println("<h1>错误404,请求的资源不存在</h1>");
out.println("</body>");
out.println("</html>");
}else {
this.out.println("Http/1.1 200 OK");
this.out.println("Content-Type:text/html; charset=GB2312");
this.out.println();
out.println(readFile(file));
}
}
//读取指定文件内容
public String readFile(File file) {
StringBuffer s = new StringBuffer();
try {
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(
new InputStreamReader(fis));
String str = "";
while((str = br.readLine())!=null) {
s.append(str+"\n");
}
}catch (IOException ioe) {
ioe.printStackTrace();
}
return s.toString();
}
public static void main(String[] args) {
WebResponse wr = new WebResponse();
wr.getURLString();
}
}
页:
[1]
