【问题标题】:get url address with network class使用网络类获取 url 地址
【发布时间】:2014-10-08 14:33:33
【问题描述】:

我编写了一个简单的代码,它是一个小巧的 Web 服务器。我想在客户端输入loalhost:8181/pic 时显示图片,但是当我 String str = in.readLine() str 仅显示 localhost:8181 我如何在他的浏览器中找到客户端输入localhost:8181/pic ?有我的简单代码:

    protected void start() {
    ServerSocket s;

    System.out.println("Webserver starting up on port 80");
    System.out.println("(press ctrl-c to exit)");
    try {
        // create the main server socket
        s = new ServerSocket(8181);
    } catch (Exception e) {
        System.out.println("Error: " + e);
        return;
    }

    System.out.println("Waiting for connection");
    for (;;) {
        try {
            Socket remote = s.accept();
            System.out.println("Connection, sending data.");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    remote.getInputStream()));
            PrintWriter out = new PrintWriter(remote.getOutputStream());
            String method = in.readLine();
            out.flush();
            remote.close();
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }
}

【问题讨论】:

    标签: java sockets http webserver


    【解决方案1】:

    你应该接受所有参数。不只是两个第一行;

        String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    

    【讨论】:

      【解决方案2】:

      你需要更多这样的东西:

      protected void start() {
          ServerSocket s;
      
          System.out.println("Webserver starting up on port 8181");
          System.out.println("(press ctrl-c to exit)");
          try {
              // create the main server socket
              s = new ServerSocket(8181);
          } catch (Exception e) {
              System.out.println("Error: " + e);
              return;
          }
      
          for (;;) {
              System.out.println("Waiting for connection");
              try {
                  Socket remote = s.accept();
                  System.out.println("Connection established.");
                  BufferedReader in = new BufferedReader(new InputStreamReader(remote.getInputStream()));
                  PrintWriter out = new PrintWriter(remote.getOutputStream());
      
                  String tokens[] = in.readLine().split("\\s+") ;
                  if (tokens.length != 3)
                  {
                      out.println("HTTP/1.0 400 Bad Request");
                      out.println("Connection: close");
                      out.println("");
                      out.flush();
                      remote.close();
                      continue;
                  }
      
                  if ((tokens[2].compareToIgnoreCase("HTTP/1.0") != 0) &&
                      (tokens[2].compareToIgnoreCase("HTTP/1.1") != 0))
                  {
                      out.println("HTTP/1.0 505 HTTP Version Not Supported");
                      out.println("Connection: close");
                      out.println("");
                      out.flush();
                      remote.close();
                      continue;
                  }
      
                  if (tokens[0].compareToIgnoreCase("GET") != 0)
                  {
                      out.println("HTTP/1.0 405 Method Not Allowed");
                      out.println("Connection: close");
                      out.println("");
                      out.flush();
                      remote.close();
                      continue;
                  }
      
                  String path = tokens[1];
                  String query = null;
      
                  int idx = path.indexOf('?');
                  if (idx != -1)
                  {
                      query = path.substring(idx+1);
                      file = path.substring(0, idx);
                  }
      
                  if (path != "/pic")
                  {
                      out.println("HTTP/1.0 404 Not Found");
                      out.println("Connection: close");
                      out.println("");
                      out.flush();
                      remote.close();
                      continue;
                  }
      
                  out.println("HTTP/1.0 200 OK");
                  out.println("Connection: close");
                  out.println("Content-Type: ..."); // for you to fill in
                  out.println("Content-Length: ..."); // for you to fill in
                  out.println();
      
                  // write out image data here...
      
                  out.flush();
                  remote.close();
              } catch (Exception e) {
                  System.out.println("Error: " + e);
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        您需要检索所有可用的输入,而不仅仅是第一行:

        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        

        这会给你这样的东西:

        获取 /pic?hello=true HTTP/1.1 主机:本地主机:8181 连接:保持活动 接受:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 用户代理:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36 DNT: 1 接受编码:gzip、deflate、sdch 接受语言:en-US,en;q=0.8,af;q=0.6

        现在,您可以看到浏览器实际请求的内容:

        它正在请求服务器GET /pic?hello=true

        还有一堆其他的请求头。

        【讨论】:

        • 更具体地说,您需要实现实际HTTP protocol 的基础知识,这需要的不仅仅是一个简单的readLine() 循环,尤其是不需要寻找null 的循环( readLine() 仅在流结束时返回,即套接字断开连接)。
        • @RemyLebeau,感谢您的洞察力! +1
        猜你喜欢
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        • 2012-08-10
        • 1970-01-01
        • 1970-01-01
        • 2012-07-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多