HTTP的Scoket实现
public class RequestLine {
private String mVersion;
private String mUri;
private String mMethod;
public RequestLine(String version, String method, String uri) {
mVersion = version;
mMethod = method;
mUri = uri;
}
@Override
public String toString() {
return mMethod + " " + mUri + " " + mVersion + "\n";
}
}
public class ResponeLine {
private String version;
private int mStatusCode;
private String mStatusDescription;
public RequestLine(String version, String statusCode, String statusDescription) {
mVersion = version;
mStatusDescription = statusDescription;
mStatusCode = statusCode;
}
public int getStatusCode() {
return mStatusCode;
}
public String getStatusDescription() {
return mStatusDescription;
}
@Override
public String toString() {
return mVersion + " " + mStatusCode + " " + mStatusDescription + "\n";
}
}
public class Header {
private Map<String, String> mHeaders = new HashMap<>();
public Header() {}
public Header(String key, String value) {
addHeader(key, value);
}
public void addHeader(String key, String value) {
if (null == mHeaders) {
throw new RuntimeException("mHeaders is null");
}
mHeaders.add(key, value);
}
public void getHeader(String key) {
return mHeaders.get(key);
}
@Override
public String toString() {
String headStr = "";
String headLine = "";
for (Map.Entry<String, String> entry : mHeaders.entry()) {
headLine = entry.getKey() + ":" + entry.getValue() + "\n";
headStr += headLine;
}
return headStr;
}
}
public class Content {
private String mContent;
public Content(String content) {
mContent = content;
}
@Override
public String toString() {
return mContent;
}
}
public class HttpRequest {
private RequestLine mRequestLine;
private Header mHeader;
private Content mContent;
public HttpRequest(String method, String url) {
int n = url.indexOf("/");
if (-1 == n) {
throw new RuntimeException("url is not legal");
}
String uri = url.substring(n, url.length - 1);
String url = url.substring(0, n - 1);
mRequestLine = new RequestLine("HTTP/1.1", method, uri);
mHeader = new Header("Host", url);
}
public void addHeader(String key, String value) {
mHeader.addHeader(key, value);
}
public void setContent(Content content) {
mContent = content;
}
@Override
public String toString() {
if (null == mContent) {
return mRequestLine.toString() + mHeader.toString() + "\n";
}
return mRequestLine.toString() + mHeader.toString() + "\n" + mContent.toString();
}
}
public class HttpRespone{
private ResponeLine mResponeLine;
private Header mHeader;
private Content mContent;
public HttpRespone(String version, String stautsCode, String statusDescriptoin) {
mResponeLine = new ResponeLine(version, statusCode, statusDescription);
mHeader = new Header();
}
public void addHeader(String key, String value) {
mHeader.addHeadrt(key, value);
}
public void setContent(String content) {
mContent = content;
}
@Override
public String toString() {
if (null == mContent) {
return mResponeLine.toString + mHeader.toString + "\n";
}
return mResponeLine.toString + mHeader.toString + "\n" + mContent.toString();
}
}
////客户端代码
public class SimpleHttpClient {
private static String encoding = "GBK";
public static void main(String[] args) {
try {
Socket s = new Socket(InetAddress.getLocalHost(), 8080);
OutputStreamWriter osw = new OutputStreamWriter(s.getOutputStream());
HttpRequest request = new HttpRequest("GET", "http://127.0.0.1:5050/hello.jsp");
request.addHeader("Accept", "*\*");
osw.write(request.toString());
osw.flush();
BufferedInputStream streamReader = new BufferedInputStream(socket.getInputStream());
bufferedReader = new BufferedReader(new InputStreamReader(streamReader, "utf-8"));
String line = null;
while((line = bufferedReader.readLine())!= null) {
System.out.println(line);
}
bufferedReader.close();
bufferedWriter.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class MyWebServer {
public static void main(String[] args) {
Socket socket = null;
try {
// 创建一个监听8000端口的服务器Socket
ServerSocket s = new ServerSocket(8000, 3);
while (true) {
socket = s.accept();
new MyWebServerThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MyWebServerThread extends Thread {
private Socket socket;
MyWebServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
InputStreamReader is = new InputStreamReader(socket.getInputStream());
char[] bs = new char[2048];
PrintStream out;
out = new PrintStream(socket.getOutputStream());
StringBuilder msg = new StringBuilder();
socket.setSoTimeout(10);
int len = -1;
try {
while ((len = is.read(bs)) != -1) {
msg.append(bs, 0, len);
msg.append("\n");
}
} catch (Exception ex) {
// ex.printStackTrace();
}
HttpRespone respone = new HttpRespone("HTTP/1.1", "200", "OK");
respone.adHeader("Content-Type", "text/html");
respone.setContent("hello");
out.println(respone.toString());
out.flush();
out.close();
is.close();
System.out.println("close");
// 关闭连接
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}