"400 Bad request"使用 OpenSSL BIO 进行请求时

"400 Bad request" when using OpenSSL BIO for request

本文关键字:请求 BIO OpenSSL Bad request 使用      更新时间:2023-10-16

我正在尝试使用C 的OpenSSL连接到我的网站,这就是我所拥有的:

#include "openssl/ssl.h"
#include "openssl/bio.h"
#include "openssl/err.h"
#include <string>
int main( )
{
    BIO * bio;
    int p;
    char * request = (char*)"GET / HTTPS/1.1x0Dx0AHost: (website)/auth.phpx0Dx0Ax43onnection: Closex0Dx0Ax0Dx0A";
    char r[ 1024 ];
    ERR_load_BIO_strings( );
    SSL_load_error_strings( );
    OpenSSL_add_all_algorithms( );
    bio = BIO_new_connect( "(website):80" );
    if ( bio == NULL )
    {
        printf( "BIO is nulln" ); return 0;
    }
    if ( BIO_do_connect( bio ) <= 0 )
    {
        ERR_print_errors_fp( stderr );
        BIO_free_all( bio );
        return 0;
    }
    BIO_write( bio, request, strlen( request ) );    
    while(true)
    {
        p = BIO_read( bio, r, 1023 );
        if ( p <= 0 ) break;
        r[ p ] = 0;
        printf( "%s", r );
    }
    BIO_free_all( bio );
    system( "pause" );
    return 0;
}

当我运行此问题时,我会收到此错误:

HTTP/1.0 400 Bad request
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<html><body><h1>400 Bad request</h1>
Your browser sent an invalid request.
</body></html>
Press any key to continue . . .

该网站具有SSL证书,每当我尝试连接到主站点时,它都说它已移至同一域,但使用https://而不是http://在前面。

当我进入我的网站的位置,我将其与www一起在前面,当然还有TLD之后。如您所见,我正在尝试从PHP脚本阅读,因此,如果有人知道我如何解决这个问题,我会欣赏它。

char * request = (char*)"GET / HTTPS/1.1x0Dx0AHost: (website)/auth.phpx0Dx0Ax43onnection: Closex0Dx0Ax0Dx0A";

更改为:

const char request[] = "GET /auth.php HTTP/1.1rn"
    "Host: www.example.comrn"
    "Connection: Closernrn";

(感谢Sam Varshavchik和轨道上的轻度比赛,以捕获 HTTPS/1.1 (。


bio = BIO_new_connect( "(website):80" );

更改为:

bio = BIO_new_connect( "www.example.com:443" );

您可能还对OpenSSL Wiki上的SSL/TLS客户端感兴趣。您似乎缺少我希望看到的东西,例如SSL_CTX_newBIO_set_conn_hostnameSSL_set_tlsext_host_nameBIO_do_handshake等。

将来,请停止编辑重要信息,例如(website)。没有人可以通过缺少信息来复制您的问题。

c 可以使使用OpenSSL更容易。您可以使用unique_ptr避免使用EVP_CIPHER_CTX_free等功能的明确调用。有关某些C 示例,请参见EVP对称加密和解密|openssl Wiki,unique_ptr和openssl的stack_of(x509( *,如何将pkcs7_sign结果纳入char *或std :: string等,