#include <stdio.h>
#include <stdlib.h>
#include "openssl/bio.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
char* join(char *s1, char *s2);
int main()
{
BIO *sbio, *out;
int len;
char tmpbuf[1024];
SSL_CTX *ctx;
SSL *ssl;
//ERR_load_crypto_strings();
ERR_load_SSL_strings();
//OpenSSL_add_all_algorithms();
SSL_library_init();
//如果系统平台不支持自动进行随机数种子的设置,这里应该进行设置(seed PRNG)
ctx = SSL_CTX_new(SSLv23_client_method());
//ctx = SSL_CTX_new(TLS_client_method());
//SSL_CTX_set_cipher_list(ctx,"EXP-RC4-MD5");
//通常应该在这里设置一些验证路径和模式等,因为这里没有设置,所以该例子可以跟使用任意CA签发证书的任意服务器建立连接
sbio = BIO_new_ssl_connect(ctx);
BIO_get_ssl(sbio, &ssl);
if(!ssl)
{
fprintf(stderr, "Can't locate SSL pointer/n");
ERR_print_errors_fp(stderr);
}
/* 不需要任何重试请求*/
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
//这里你可以添加对SSL的其它一些设置
BIO_set_conn_hostname(sbio, "106.39.167.232:443");
out = BIO_new_fp(stdout, BIO_NOCLOSE);
if(BIO_do_connect(sbio) <= 0)
{
fprintf(stderr, "Error connecting to server/n");
ERR_print_errors_fp(stderr);
}
printf("SSL connection using %s\n",SSL_get_cipher(ssl));
//SSL_CTX_set_cipher_list(ctx,SSL_get_cipher(ssl));
X509 *cert = SSL_get_peer_certificate(ssl);
printf ("Server certificate:\n");
char *str = X509_NAME_oneline(X509_get_subject_name(cert),0,0);
printf ("\t subject: %s\n", str);
OPENSSL_free(str);
str = X509_NAME_oneline(X509_get_issuer_name(cert),0,0);
printf ("\t issuer: %s\n", str);
OPENSSL_free(str);
X509_free(cert);
if(BIO_do_handshake(sbio) <= 0)
{
fprintf(stderr, "Error establishing SSL connection/n");
ERR_print_errors_fp(stderr);
}
/* 这里可以添加检测SSL连接的代码,得到一些连接信息*/
const char *http_req_hdr_tmpl = "GET %s HTTP/1.1\r\n"
"Accept: image/gif, image/jpeg, text/json,text/html */*\r\nAccept-Language: zh-cn\r\n"
"Accept-Encoding: gzip, deflate\r\nHost: %s\r\n"
"User-Agent: %s <0.1>\r\nConnection: Keep-Alive\r\n\r\n";
int bufsize = 1024;
char header_get[bufsize];
len = sprintf(header_get,http_req_hdr_tmpl,"/routerjson","api.jd.com","User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36");
//BIO_puts(sbio, header_get);
BIO_write(sbio,header_get,len);
for(;;)
{
len = BIO_read(sbio, tmpbuf, 1024);
if(len <= 0) break;
BIO_write(out, tmpbuf, len);
}
BIO_free_all(sbio);
BIO_free(out);
return 0;
}