gpt4 book ai didi

java - 在Java中验证Http请求的方法?

转载 作者:行者123 更新时间:2023-12-01 06:03:15 24 4
gpt4 key购买 nike

我正在尝试验证服务器中的 HTTP 请求。 http 客户端可以向我发送不同的请求,其中一些可能与我的服务器不匹配,因此在这种情况下,我必须向客户端发送一个响应,表明请求有问题。

在继续阅读之前,请考虑更改字段名称的顺序不会影响响应。因此字段的顺序并不重要。

例如:这是一个合法的请求:

POST http://localhost HTTP/1.1
HOST: (here is an IP adress)
Content-Length: 50
Connection: Close
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
User_Id = 123456
Item_Id = 5007
Color = 1010

(注意:如果请求只有这些字段名称,则该请求是合法的,如果它包含任何其他字段名称,它将被视为非法)

因为这是有效的请求,我的服务器将发送 200 OK 作为响应,如下所示:

HTTP/1.1 200 OK
Connection: Close
Payment: 2755201

但是,例如,如果请求的字段值错误或未知字段,如下所示:User_Id 值错误:

POST http://localhost HTTP/1.1
HOST: (here is an IP adress)
Content-Length: 50
Connection: Close
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
User_Id = -2323
Item_Id = 5007
Color = 1010

响应将是:

HTTP/1.1 200 OK
Connection: Close
Error_ID: 602
Error_String: Error in user_Id (illegal user ID)

或未知字段名称:

POST http://localhost HTTP/1.1
HOST: (here is an IP adress)
Content-Length: 50
Connection: Close
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
User_Id = 123456
Item_Id = 5007
Sale = 0.5
Color = 1010

响应将是:

HTTP/1.1 200 OK
Connection: Close
Error_ID: 602
Error_String: Sale , unknown field name

作为一种验证方式,我考虑将所有有效字段名称放入枚举中,当我收到请求时,我根据此枚举分析它们,但我有更多有效字段(不仅是 user_Id 、item_Id 和 color ),因此应用这种方式可能需要很长时间并且可能效率低下,特别是因为字段的顺序并不重要。

这是我的服务器代码:

@Test
public void main() throws IOException
{
serverSocket = new ServerSocket(80);
Socket clnt = null;
while (true)
{
clnt = serverSocket.accept();
System.out.println("after accept in server");
BufferedReader request = new BufferedReader(new InputStreamReader(clnt.getInputStream()));
BufferedWriter response = new BufferedWriter(new OutputStreamWriter(clnt.getOutputStream()));

String req = ".", strReq = "";
try
{
req=request.readLine();
while (!req.equals(""))
{
System.out.println(req);
strReq += (req + "\n");
req=request.readLine();
}
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
System.out.println("xxxx");
}
System.out.println(strReq);
String httpResponse = "HTTP/1.1 200 OK\r\n";
//here will be the rest of the response
response.write(httpResponse);
System.out.println(httpResponse);
response.flush();
}
}

我是 http 的新手,因此如果有一些验证库或任何其他更有效的验证方法,如果您能提出建议,我将不胜感激。

最佳答案

使用 Spring mvc Controller 。它让您的生活更轻松。验证将自动进行。例如

  @RequestMapping(value = "user", method = RequestMethod.POST) 
public Response addUser(@RequestBody @Valid User user) {
return userService.addUser(user);
}

public class User implements Serializable {

private Long id;
@NotNull
@NotEmpty
private String userName;
@NotNull
@NotEmpty
private String password;
@NotNull
@NotEmpty
private String firstName;
private String lastName;

//all getters and setters here

}
@ControllerAdvice
@RestController
public class GlobalExceptionController {

@ExceptionHandler({Exception.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public Response handleBadRequest(Exception ex) {
Response response = new Response();
response.setStatus(HttpStatus.BAD_REQUEST.value());
response.setMessage(ex.getMessage());
return response;
}
}

所有验证都将由注释 @Valid 负责。如果需要,可以在 Spring 全局异常 Controller 中处理异常

关于java - 在Java中验证Http请求的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52056729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com