gpt4 book ai didi

c - 对 www.httpbin.org 的错误请求?

转载 作者:太空宇宙 更新时间:2023-11-04 08:10:43 25 4
gpt4 key购买 nike

我正在使用 openSSL 库与 www.httpbin.org 创建 SSL session 。我能够在服务器的端口号 443 上成功创建它。我在 ubuntu 中使用了 ping www.httpbin.org 命令来获取 httpbinip address 这样我就可以在我的程序中使用它。我在下面粘贴了我的程序。我从服务器收到 400 Bad Request

有人能解释一下为什么我会收到这个错误吗?。 char msg[] 保存 HTTP 初始行和 header 。可能问题出在我的 http headers 上,但我无法解决这个问题。

#include <iostream>
#include <string.h>

#include <unistd.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>

#define DEFAULT_PORT_NUMBER 443

int create_socket(char *, uint16_t port_num);

int openSSL_client_init()
{
OpenSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
SSL_load_error_strings();

if (SSL_library_init() < 0)
return -1;
return 0;
}

int openSSL_create_client_ctx(SSL_CTX **ctx)
{
const SSL_METHOD *method = SSLv23_client_method();

if ((*ctx = SSL_CTX_new(method)) == NULL)
return -1;

//SSL_CTX_set_options(*ctx, SSL_OP_NO_SSLv2);

return 0;
}


int main(int argc, char *argv[])
{
BIO *outbio = NULL;
X509 *cert;
X509_NAME *certname = NULL;
SSL_CTX *ctx;
SSL *ssl;
int server = 0;
int ret, i;

if (openSSL_client_init()) {
std :: cerr << "Could not initialize the OpenSSL library !" << std :: endl;
return -1;
}

outbio = BIO_new_fp(stdout, BIO_NOCLOSE);

if (openSSL_create_client_ctx(&ctx)) {
std :: cerr << "Unable to create a new SSL context structure." << std :: endl;
return -1;
}

ssl = SSL_new(ctx);
server = create_socket(argv[1], atoi(argv[2]));

if (server < 0) {
std :: cerr << "Error: Can't create TCP session" << std :: endl;
return -1;
}
std :: cout << "Successfully made the TCP connection to: " << argv[1] << " port: " << atoi(argv[2]) << std :: endl;

SSL_set_fd(ssl, server);

if (SSL_connect(ssl) != 1) {
std :: cerr << "Error: Could not build a SSL session to: " << argv[1] << std :: endl;
return -1;
}

std :: cout << "Successfully enabled SSL/TLS session to: " << argv[1] << std :: endl;
//SSL_SESSION *ss = SSL_get_session(ssl);

cert = SSL_get_peer_certificate(ssl);
if (cert == NULL) {
std :: cerr << "Error: Could not get a certificate from: " << argv[1] << std :: endl;
return -1;
}

certname = X509_NAME_new();
certname = X509_get_subject_name(cert);

std :: cout << "Displaying the certificate subject data:" << std :: endl;
X509_NAME_print_ex(outbio, certname, 0, 0);
std :: cout << std :: endl;


char msg[1024] = "GET https://www.httpbin.org/ HTTP/1.1\r\nAccept: */*\r\nAccept-Encoding: gzip\r\nUser-Agent: runscope/0.1\r\n\r\n";
SSL_write(ssl, msg, strlen(msg));
SSL_read(ssl, msg, 1024);
std :: cout << "Message is " << msg << std :: endl;

SSL_free(ssl);
close(server);
X509_free(cert);
SSL_CTX_free(ctx);
std :: cout << "Finished SSL/TLS connection with server" << std :: endl;
return 0;
}

int create_socket(char *ip_cstr, uint16_t port_num)
{
int fd;
struct sockaddr_in dest_addr;

fd = socket(AF_INET, SOCK_STREAM, 0);

dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(port_num);
dest_addr.sin_addr.s_addr = inet_addr(ip_cstr);

memset(&(dest_addr.sin_zero), '\0', 8);

if (connect(fd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) == -1)
return -1;

return fd;
}

要编译以上程序,您可以使用gcc -lssl -lcrypto FILE_NAME.C。我在 Ubuntu 上使用 gcc。要安装 openSSL 开发包,您可以在 Ubuntu 上使用 sudo apt-get install libssl-dev。要执行编译后的程序,请使用 ./a.out IP_ADDERSS PORT_NUMBER

我从程序中得到的输出是:

Successfully made the TCP connection to: 23.22.14.18 port: 443 Successfully enabled SSL/TLS session to: 23.22.14.18 Displaying the certificate subject data: =Domain Control Validated, OU=EssentialSSL Wildcard, CN=*.httpbin.org Message is

HTTP/1.1 400 Bad Request
Server: nginx
Date: Thu, 29 Sep 2016 11:10:59 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>

Finished SSL/TLS connection with server

最佳答案

GET https://www.httpbin.org/ HTTP/1.1
Accept: */*
Accept-Encoding: gzip
User-Agent: runscope/0.1

我不知道在 GET 行中提供协议(protocol)和域是有效的(尽管不要引用我的话)。当然,Host 行对于 HTTP 1.1 是强制性的,但您没有提供。

你的意思是:

GET / HTTP/1.1
Host: www.httpbin.org
Accept: */*
Accept-Encoding: gzip
User-Agent: runscope/0.1

我推荐阅读 HTTP requests在尝试实现之前。

关于c - 对 www.httpbin.org 的错误请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39768928/

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