http服务器和客户端实现head和get方法

发布于 2024-12-14 14:25:35 字数 2936 浏览 4 评论 0原文

我正在尝试使用 java 制作一个简单的 http 客户端服务器。它将显示客户端的请求以及服务器的响应。例如服务器将发回

   HTTP/1.0 200 OK
   Connection: Close. etc.

以前我有一个回显客户端服务器。现在我已经将 echo 服务器转变为 http 服务器。我已经尝试过如何实现 head 并与客户端取得联系,但我注意到通常所有示例都使用 apache 框架。有没有办法在没有apache框架的情况下实现这些方法。 我正在尝试将其转换为 http 客户端的 echo 客户端:

       import java.io.*;
       import java.net.*;

          public class Ec1
      {
      public static void main(String[] args)
        {
           try
        {
          Socket s = new Socket("127.0.0.1", 80);
        BufferedReader r = new BufferedReader(new                                 InputStreamReader(s.getInputStream()));
            PrintWriter w = new PrintWriter(s.getOutputStream(), true);
           BufferedReader con = new BufferedReader(new InputStreamReader(System.in));
          String line;
           do
        {
         line = r.readLine();
          if ( line != null )
        System.out.println(line);
       line = con.readLine();
          w.println(line);
        }
         while ( !line.trim().equals("bye") );
          }
          catch (Exception err)
        {
         System.err.println(err);
        }
        }
     }

我的 Http 服务器:

          import java.io.BufferedReader;
          import java.io.InputStreamReader;
          import java.io.PrintWriter;
          import java.net.ServerSocket;
          import java.net.Socket;

          public class Echo
                 { 

 protected void start() {
        ServerSocket s;

        System.out.println("Webserver starting up on port 80");

        try {
          // create the main server socket
          s = new ServerSocket(80);
        } catch (Exception e) {
          System.out.println("Error: " + e);
          return;
        }
        Socket clientSocket = null; 
        System.out.println ("Waiting for connection.....");
        try { 
             clientSocket = s.accept(); 
             System.out.println("Connection, sending data.");
             BufferedReader in = new BufferedReader(new InputStreamReader(
                     clientSocket.getInputStream()));
             PrintWriter out = new PrintWriter(clientSocket.getOutputStream());


             String str = ".";
             while (!str.equals(""))
               str = in.readLine();


             out.println("HTTP/1.0 200 OK");
             out.println("Content-Type: text/html");
             out.println("Server: Bot");

             out.println("");

             out.println("<H1>Welcome to the Ultra Mini-WebServer</H2>");
             out.flush();
             clientSocket.close();
             s.close(); 
           } catch (Exception e) {
             System.out.println("Error: " + e);
           }
         }

        public static void main(String args[]) {
            WebServer ws = new WebServer();
            ws.start();
                    }
           }

I'm trying to make a simple http client server using java. It will show the client's request as well as the server's response. For example server will send back

   HTTP/1.0 200 OK
   Connection: Close. etc.

Previously i had a echo client server. Now I've turned my echo server to act as a http server. I've tried Goggling about how to implement the head and get with the client but i noticed usually all of the example used apache framework. Is there a way to implement these method without apache framework.
My echo client which i'm trying to convert into a http client:

       import java.io.*;
       import java.net.*;

          public class Ec1
      {
      public static void main(String[] args)
        {
           try
        {
          Socket s = new Socket("127.0.0.1", 80);
        BufferedReader r = new BufferedReader(new                                 InputStreamReader(s.getInputStream()));
            PrintWriter w = new PrintWriter(s.getOutputStream(), true);
           BufferedReader con = new BufferedReader(new InputStreamReader(System.in));
          String line;
           do
        {
         line = r.readLine();
          if ( line != null )
        System.out.println(line);
       line = con.readLine();
          w.println(line);
        }
         while ( !line.trim().equals("bye") );
          }
          catch (Exception err)
        {
         System.err.println(err);
        }
        }
     }

My Http server:

          import java.io.BufferedReader;
          import java.io.InputStreamReader;
          import java.io.PrintWriter;
          import java.net.ServerSocket;
          import java.net.Socket;

          public class Echo
                 { 

 protected void start() {
        ServerSocket s;

        System.out.println("Webserver starting up on port 80");

        try {
          // create the main server socket
          s = new ServerSocket(80);
        } catch (Exception e) {
          System.out.println("Error: " + e);
          return;
        }
        Socket clientSocket = null; 
        System.out.println ("Waiting for connection.....");
        try { 
             clientSocket = s.accept(); 
             System.out.println("Connection, sending data.");
             BufferedReader in = new BufferedReader(new InputStreamReader(
                     clientSocket.getInputStream()));
             PrintWriter out = new PrintWriter(clientSocket.getOutputStream());


             String str = ".";
             while (!str.equals(""))
               str = in.readLine();


             out.println("HTTP/1.0 200 OK");
             out.println("Content-Type: text/html");
             out.println("Server: Bot");

             out.println("");

             out.println("<H1>Welcome to the Ultra Mini-WebServer</H2>");
             out.flush();
             clientSocket.close();
             s.close(); 
           } catch (Exception e) {
             System.out.println("Error: " + e);
           }
         }

        public static void main(String args[]) {
            WebServer ws = new WebServer();
            ws.start();
                    }
           }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心如荒岛 2024-12-21 14:25:35

是的,有,只需解释您从客户那里收到的请求即可。

从以下代码(在 HttpServer 中),解析:

String str = ".";
while (!str.equals("")) {
  str = in.readLine();

  if (str.startsWith("HEAD")) {
      //Head execution here...
  }
}

等等...

Yes, there is, just interpret your request you're getting from the client.

From the following code (in HttpServer), parse:

String str = ".";
while (!str.equals("")) {
  str = in.readLine();

  if (str.startsWith("HEAD")) {
      //Head execution here...
  }
}

Etc...

风透绣罗衣 2024-12-21 14:25:35

每个人都在客户端和/或服务器端使用某种库或框架,因为 HTTP 有点复杂,不需要重新发明每一个轮子。然而,很可能直接从 HTTP 的 RFC 开始编写自己的实现。

Everybody uses some kind of library or framework for the client and/or the server side because HTTP is somewhat complex and there is no need to reinvent every wheel. However, it is quite possible to write your own implementation by starting directly from the RFCs for HTTP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文