[关闭]
@jimbo 2016-10-18T22:17:12.000000Z 字数 4447 阅读 829

HTTP的Scoket实现

  1. public class RequestLine {
  2. private String mVersion;
  3. private String mUri;
  4. private String mMethod;
  5. public RequestLine(String version, String method, String uri) {
  6. mVersion = version;
  7. mMethod = method;
  8. mUri = uri;
  9. }
  10. @Override
  11. public String toString() {
  12. return mMethod + " " + mUri + " " + mVersion + "\n";
  13. }
  14. }
  1. public class ResponeLine {
  2. private String version;
  3. private int mStatusCode;
  4. private String mStatusDescription;
  5. public RequestLine(String version, String statusCode, String statusDescription) {
  6. mVersion = version;
  7. mStatusDescription = statusDescription;
  8. mStatusCode = statusCode;
  9. }
  10. public int getStatusCode() {
  11. return mStatusCode;
  12. }
  13. public String getStatusDescription() {
  14. return mStatusDescription;
  15. }
  16. @Override
  17. public String toString() {
  18. return mVersion + " " + mStatusCode + " " + mStatusDescription + "\n";
  19. }
  20. }
  1. public class Header {
  2. private Map<String, String> mHeaders = new HashMap<>();
  3. public Header() {}
  4. public Header(String key, String value) {
  5. addHeader(key, value);
  6. }
  7. public void addHeader(String key, String value) {
  8. if (null == mHeaders) {
  9. throw new RuntimeException("mHeaders is null");
  10. }
  11. mHeaders.add(key, value);
  12. }
  13. public void getHeader(String key) {
  14. return mHeaders.get(key);
  15. }
  16. @Override
  17. public String toString() {
  18. String headStr = "";
  19. String headLine = "";
  20. for (Map.Entry<String, String> entry : mHeaders.entry()) {
  21. headLine = entry.getKey() + ":" + entry.getValue() + "\n";
  22. headStr += headLine;
  23. }
  24. return headStr;
  25. }
  26. }
  1. public class Content {
  2. private String mContent;
  3. public Content(String content) {
  4. mContent = content;
  5. }
  6. @Override
  7. public String toString() {
  8. return mContent;
  9. }
  10. }
  1. public class HttpRequest {
  2. private RequestLine mRequestLine;
  3. private Header mHeader;
  4. private Content mContent;
  5. public HttpRequest(String method, String url) {
  6. int n = url.indexOf("/");
  7. if (-1 == n) {
  8. throw new RuntimeException("url is not legal");
  9. }
  10. String uri = url.substring(n, url.length - 1);
  11. String url = url.substring(0, n - 1);
  12. mRequestLine = new RequestLine("HTTP/1.1", method, uri);
  13. mHeader = new Header("Host", url);
  14. }
  15. public void addHeader(String key, String value) {
  16. mHeader.addHeader(key, value);
  17. }
  18. public void setContent(Content content) {
  19. mContent = content;
  20. }
  21. @Override
  22. public String toString() {
  23. if (null == mContent) {
  24. return mRequestLine.toString() + mHeader.toString() + "\n";
  25. }
  26. return mRequestLine.toString() + mHeader.toString() + "\n" + mContent.toString();
  27. }
  28. }
  1. public class HttpRespone{
  2. private ResponeLine mResponeLine;
  3. private Header mHeader;
  4. private Content mContent;
  5. public HttpRespone(String version, String stautsCode, String statusDescriptoin) {
  6. mResponeLine = new ResponeLine(version, statusCode, statusDescription);
  7. mHeader = new Header();
  8. }
  9. public void addHeader(String key, String value) {
  10. mHeader.addHeadrt(key, value);
  11. }
  12. public void setContent(String content) {
  13. mContent = content;
  14. }
  15. @Override
  16. public String toString() {
  17. if (null == mContent) {
  18. return mResponeLine.toString + mHeader.toString + "\n";
  19. }
  20. return mResponeLine.toString + mHeader.toString + "\n" + mContent.toString();
  21. }
  22. }
  1. ////客户端代码
  2. public class SimpleHttpClient {
  3. private static String encoding = "GBK";
  4. public static void main(String[] args) {
  5. try {
  6. Socket s = new Socket(InetAddress.getLocalHost(), 8080);
  7. OutputStreamWriter osw = new OutputStreamWriter(s.getOutputStream());
  8. HttpRequest request = new HttpRequest("GET", "http://127.0.0.1:5050/hello.jsp");
  9. request.addHeader("Accept", "*\*");
  10. osw.write(request.toString());
  11. osw.flush();
  12. BufferedInputStream streamReader = new BufferedInputStream(socket.getInputStream());
  13. bufferedReader = new BufferedReader(new InputStreamReader(streamReader, "utf-8"));
  14. String line = null;
  15. while((line = bufferedReader.readLine())!= null) {
  16. System.out.println(line);
  17. }
  18. bufferedReader.close();
  19. bufferedWriter.close();
  20. socket.close();
  21. } catch (UnknownHostException e) {
  22. e.printStackTrace();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }
  1. import java.net.*;
  2. import java.io.*;
  3. public class MyWebServer {
  4. public static void main(String[] args) {
  5. Socket socket = null;
  6. try {
  7. // 创建一个监听8000端口的服务器Socket
  8. ServerSocket s = new ServerSocket(8000, 3);
  9. while (true) {
  10. socket = s.accept();
  11. new MyWebServerThread(socket).start();
  12. }
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }
  18. class MyWebServerThread extends Thread {
  19. private Socket socket;
  20. MyWebServerThread(Socket socket) {
  21. this.socket = socket;
  22. }
  23. @Override
  24. public void run() {
  25. try {
  26. InputStreamReader is = new InputStreamReader(socket.getInputStream());
  27. char[] bs = new char[2048];
  28. PrintStream out;
  29. out = new PrintStream(socket.getOutputStream());
  30. StringBuilder msg = new StringBuilder();
  31. socket.setSoTimeout(10);
  32. int len = -1;
  33. try {
  34. while ((len = is.read(bs)) != -1) {
  35. msg.append(bs, 0, len);
  36. msg.append("\n");
  37. }
  38. } catch (Exception ex) {
  39. // ex.printStackTrace();
  40. }
  41. HttpRespone respone = new HttpRespone("HTTP/1.1", "200", "OK");
  42. respone.adHeader("Content-Type", "text/html");
  43. respone.setContent("hello");
  44. out.println(respone.toString());
  45. out.flush();
  46. out.close();
  47. is.close();
  48. System.out.println("close");
  49. // 关闭连接
  50. socket.close();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注