java HTTP 请求对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1695978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:38:48  来源:igfitidea点击:

HTTP Request Object

javahttp

提问by monksy

Is there an object within the standard Java SE that can accept a HTTP request from a socket? I have found how to create and send one, however I have not found a way to retrieve a HTTP object from a socket. I can create one my self, but I would rather rely on a heavily tested object.

标准 Java SE 中是否有可以接受来自套接字的 HTTP 请求的对象?我已经找到了如何创建和发送一个对象,但是我还没有找到一种从套接字检索 HTTP 对象的方法。我可以自己创建一个,但我宁愿依赖经过严格测试的对象。

This seems like something that would be readily available given the structure of JSP.

考虑到 JSP 的结构,这似乎很容易获得。

回答by Thorbj?rn Ravn Andersen

There is a small HTTP server in the Java 6 SDK (not sure if it will be in the JRE or in non-Sun JVM's).

Java 6 SDK 中有一个小型 HTTP 服务器(不确定它是在 JRE 中还是在非 Sun JVM 中)。

From http://www.java2s.com/Code/Java/JDK-6/LightweightHTTPServer.htm:

http://www.java2s.com/Code/Java/JDK-6/LightweightHTTPServer.htm

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class HttpServerDemo {
  public static void main(String[] args) throws IOException {
    InetSocketAddress addr = new InetSocketAddress(8080);
    HttpServer server = HttpServer.create(addr, 0);

    server.createContext("/", new MyHandler());
    server.setExecutor(Executors.newCachedThreadPool());
    server.start();
    System.out.println("Server is listening on port 8080" );
  }
}

class MyHandler implements HttpHandler {
  public void handle(HttpExchange exchange) throws IOException {
    String requestMethod = exchange.getRequestMethod();
    if (requestMethod.equalsIgnoreCase("GET")) {
      Headers responseHeaders = exchange.getResponseHeaders();
      responseHeaders.set("Content-Type", "text/plain");
      exchange.sendResponseHeaders(200, 0);

      OutputStream responseBody = exchange.getResponseBody();
      Headers requestHeaders = exchange.getRequestHeaders();
      Set<String> keySet = requestHeaders.keySet();
      Iterator<String> iter = keySet.iterator();
      while (iter.hasNext()) {
        String key = iter.next();
        List values = requestHeaders.get(key);
        String s = key + " = " + values.toString() + "\n";
        responseBody.write(s.getBytes());
      }
      responseBody.close();
    }
  }
}

回答by dlamblin

Yeah, you make a new HTTP Request object from what you accept on the socket. What you do after that is up to you, but it should probably involve an HTTP Response.

是的,您从套接字上接受的内容创建了一个新的 HTTP 请求对象。之后做什么由您决定,但它可能应该涉及 HTTP 响应。

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

public final class WebServer {
    public static void main(String args[]) throws Exception {
        int PORT = 8080;
        ServerSocket listenSocket = new ServerSocket(PORT);
        while(true) {
            HttpRequest request = new HttpRequest(listenSocket.accept());
            Thread thread = new Thread(request);
            thread.start();
        }
    }
}

From: http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=396
There's some more work to be done in the tutorial, but it does look nice.

来自:http: //www.devhood.com/tutorials/tutorial_details.aspx?
tutorial_id=396 本教程还有一些工作要做,但看起来确实不错。

回答by flybywire

It looks like you are looking for a Servlet. A servlet is an API that lets you receive and respond to an HTTP request.

看起来您正在寻找一个 Servlet。servlet 是一种 API,可让您接收和响应 HTTP 请求。

Your servlet gets deployed in a container, which is basically the actual Web server that will take care of all the protocol complexities. (The most populare are Tomcat and Jetty)

您的 servlet 被部署在一个容器中,它基本上是负责处理所有协议复杂性的实际 Web 服务器。(最流行的是Tomcat和Jetty)