Home > Back-end >  C use openssl source for baidu
C use openssl source for baidu

Time:03-08

When I was in code for baidu home page source does not transform coding for the source code is only half, and the last time ssl_read the return value is 1, and each receives the data transformation coding format and then joining together to gain the source string line only 3, 4, is this about and buffer, bosses, please solve,
 # include 
# include
# include
# include
# include
# include
# include



# pragma comment (lib, "ws2_32. Lib")
# pragma comment (lib, "libssl. Lib")
# pragma comment (lib, "libcrypto. Lib")

using namespace std;




String Utf8ToGbk (const char * utf8) {

Int len=MultiByteToWideChar (CP_UTF8, 0, utf8, 1, NULL, 0).

Would be * WSTR=new would [len (int64_t) + 1);
Memset (WSTR, 0, len (int64_t) + 1);
MultiByteToWideChar (CP_UTF8, 0, utf8, 1, WSTR, len);
Len=WideCharToMultiByte (WSTR CP_ACP, 0, 1, NULL, 0, NULL, NULL);
Char * STR=new char [len (int64_t) + 1);
Memset (STR, 0, len (int64_t) + 1);
WideCharToMultiByte (WSTR CP_ACP, 0, 1, STR, len, NULL, NULL);
If (WSTR) delete [] WSTR;
Return the STR;
}


Int main () {

SSL_load_error_strings ();
SSLeay_add_ssl_algorithms ();
Const SSL_METHOD * meth=SSLv23_client_method ();

An SSL_CTX * CTX=SSL_CTX_new (meth);

If (CTX==NULL) {

ERR_print_errors_fp (stderr);
Cout & lt; <"The CTX error!" return -1;
}


WSADATA WSADATA;
Res ADDRINFO * and * cur;
ADDRINFO hints.
SOCKADDR_IN * addr.

Memset (& amp; Hints, 0, sizeof (ADDRINFO));

If (WSAStartup (MAKEWORD (2, 2), & amp; Wsadata)!=0) {

Cout & lt; <"Wsastartup error" & lt;
return -1;
}

Hints. Ai_family=AF_INET;
Hints. Ai_socktype=SOCK_STREAM;

Getaddrinfo (" www.baidu.com ", "HTTP", & amp; Hints, & amp; Res);

Cur=res;

Addr=(SOCKADDR_IN *) res - & gt; Ai_addr;

Addr - & gt; Sin_port=htons (443);

SOCKET sock=SOCKET (AF_INET SOCK_STREAM, IPPROTO_TCP);

If (the sock==INVALID_SOCKET) {

Cout & lt; <"Socket error" & lt;
return -1;
}

If (connect (the sock, (SOCKADDR *) addr, sizeof (* addr))==SOCKET_ERROR) {

Cout & lt; <"The connect error" & lt;
return -1;
}

Int ret.

SSL * SSL=SSL_new (CTX);

If (SSL==NULL) {

Cout & lt; <"SSL error" & lt;
return -1;
}

SSL_set_fd (SSL, the sock);

Ret=SSL_connect (SSL);

If (ret==1) {

Cout & lt; <"Ssl_connect error" & lt;
return -1;
}


Stringstream stream;

The stream & lt; <"GET/HTTP/1.1 \ r \ n";
The stream & lt; <"Accept: */* \ r \ n";
The stream & lt; <"Accept - Language: useful - Hans - CN, useful - Hans; Q=0.8, en - US; Q=0.5, en. Q=0.3 \ r \ n ";
The stream & lt; <"Connection: close \ r \ n";
The stream & lt; The stream & lt; <"The user-agent: Mozilla/5.0 (Windows NT 10.0; Win64. X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/Edge/537.36 17.17134 \ r \ n ";
The stream & lt; <"\ r \ n";

String request=stream. STR ();

Ret=SSL_write (SSL, request c_str (), the request, the size ());

If (ret==1) {

Cout & lt; <"Ssl_writ error" & lt;
return -1;
}

Char pageBuf [1024].

string s;

While (true) {

If ((ret=SSL_read (SSL, pageBuf, 1023)) & lt;=0) {
Cout & lt; break;
}

S +=Utf8ToGbk (pageBuf);
}


Cout & lt;

SSL_shutdown (SSL);
SSL_free (SSL);
SSL_CTX_free (CTX);
Closesocket (sock);
Freeaddrinfo (cur);
WSACleanup ();

}

CodePudding user response:

Don't call Utf8ToGbk transcoding, and obtain the response is only half of the SSL_read the return value is 1., call SSL_read after the return value is zero, but to obtain the response of the lack of the last few lines,
  • Related