简体   繁体   中英

MS VC++ OpenSSL SSL_read() blocking

I am trying to send an HTTP-POST request from C++ over SSL/TLS using OpenSSL implementation. The following implementation works except for the SSL_read() part. SSL_write() successfully returns the number of sent bytes, but the SSL_read() simply blocks the program execution.

Any idea what's going wrong?

SSL_CTX *ctx;
SSL *ssl;
char buf[1024];
int server, bytes, recv;

SSL_library_init();
ctx = InitCTX();               // Custom Function   
server = OpenConnection();     // Custom Function
ssl = SSL_new(ctx);
SSL_set_fd(ssl, server);

ostringstream FormBuffer;
FormBuffer << "POST " << FormAction << " HTTP/1.1\n";
FormBuffer << "Host: " << Host << "\n";
FormBuffer << "User-Agent: " << UserAgent << "\n";
FormBuffer << "Content-Type: application/x-www-form-urlencoded\n";
FormBuffer << "Content-Length: " << ContentLength << "\n\n";
FormBuffer << "username=USER&password=PWD\n";
const auto str = FormBuffer.str();

if (SSL_connect(ssl) != -1)
{
    bytes = SSL_write(ssl, str.data(), str.length());
    recv = SSL_read(ssl, buf, sizeof(buf)); // <--- BLOCKING !!!
    ...
    SSL_free(ssl);
}

This is not related to SSL. You send the header part of the POST request to the server, but not the body (the data for posting). But the server will only respond once it received and processed the request body. This means that your read from the server will simply block because no data are sent by the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM