报错HttpResponseProxy{HTTP/1.1 400 Bad Request,单独main方法调用接口不报错,通过程序调用就报错

本文讲述了在特定目录结构下,将证书文件放置于resources目录下的子目录中,解决HTTP请求返回400错误的问题。通过调整证书位置,从main方法调用成功避免了问题,为后续遇到类似情况提供了宝贵经验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决:把证书client.keystore和client.truststore不放在resources目录下,而是放在resources目录的下一个目录下,比如resources目录下的i18n,获取的方式就是getResourceAsStream("/i18n/client.keystore")。

这个问题困扰了我好久,有一周左右,怎末想也想不通,为什莫main方法调用就200,通过程序里的方法调用就400,方法里的代码一模一样,证书的位置也一模一样,百思不得其解,非常苦恼。是程序框架问题,导致编码不能正确识别,请求参数有问题,还是方法的参数,返回值,有问题。也都不可能啊,方法参数显示是正常的,不乱码,网上百度400,修改tomcat URIEncoding,也都不好使。把程序放到其他方法不好使,换浏览器也不好使。原来在controller层,然后放到新建的service层方法还不好使。把证书放到类的同包下还不好使,今天突然把证书放到resources下的目录下,就突然好使了,也不知道咋回事。解决了这个问题后感觉很激动!很开心!终于解决了这个问题!也为以后解决问题积累了信心,经验。增添了克服困难的必胜心、决心。

总结:遇到问题,想解决问题的思路,然后顺着这个思路多进行尝试。

解决问题思路总结:

 

思路一:查看代码,查看请求语法哪里有问题。

思路二:深挖程序代码含义,查找问题原因。

思路三:换浏览器。

思路四:百度400状态码。

思路五:询问同事。

思路六:询问代码提供第三方,寻求解决办法。

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <fcntl.h> #include <arpa/inet.h> #include <sys/select.h> #include <sys/socket.h> #include <sys/sendfile.h> #include <libgen.h> #include <sys/stat.h> #include <ctype.h> #include <netinet/in.h> #include "utils.h" #include "http_parser.h" #define MAX_CLIENTS 1024 // 接受新连接 void accept_new_connection(int server_fd, fd_set *read_fds, int *max_fd) { struct sockaddr_in client_addr; socklen_t client_len = sizeof(client_addr); int client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len); if (client_fd < 0) { perror("accept"); return; } if (client_fd >= MAX_CLIENTS) { dprintf(client_fd, "HTTP/1.1 503 Service Unavailable\r\n\r\n"); close(client_fd); return; } FD_SET(client_fd, read_fds); if (client_fd > *max_fd) *max_fd = client_fd; } // 读取客户端请求 void handle_client(int client_fd, fd_set *read_fds) { char buffer[BUFFER_SIZE] = {0}; if (read_http_request(client_fd, buffer, sizeof(buffer)) < 0) { dprintf(client_fd, "HTTP/1.1 400 Bad Request\r\n\r\n"); FD_CLR(client_fd, read_fds); close(client_fd); return; } http_request req; if (parse_http_request(buffer, &req) < 0) { dprintf(client_fd, "HTTP/1.1 400 Bad Request\r\n\r\n"); FD_CLR(client_fd, read_fds); close(client_fd); return; } printf("Received %s request for %s\n", req.method, req.path); if (strcasecmp(req.method, "GET") == 0) { process_request(&req, client_fd); } else if (strcasecmp(req.method, "POST") == 0) { if (strcmp(req.path, "/data/contact.json") == 0) { handle_contact_form(&req, client_fd); } else { dprintf(client_fd, "HTTP/1.1 405 Method Not Allowed\r\n\r\n"); } } else { dprintf(client_fd, "HTTP/1.1 405 Method Not Allowed\r\n\r\n"); } FD_CLR(client_fd, read_fds); close(client_fd); } int main() { int server_fd = create_listen_socket(80); fd_set read_fds; int max_fd = server_fd; printf("Web server started on port 80\n"); while (1) { FD_ZERO(&read_fds); FD_SET(server_fd, &read_fds); for (int i = 0; i <= max_fd; i++) { if (i != server_fd && FD_ISSET(i, &read_fds)) handle_client(i, &read_fds); else if (i == server_fd) accept_new_connection(server_fd, &read_fds, &max_fd); } } close(server_fd); return 0; } 给这个函数也添加合适的log_message输出
最新发布
08-15
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值