Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8104
  • 博文数量: 4
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-03 19:05
文章分类
文章存档

2015年(2)

2014年(2)

我的朋友
最近访客

分类: C/C++

2015-09-23 00:14:29

原文地址:用SSL发送HTTPS请求 作者:NKLOVERENE

     通过OpenSSL发送HTTPS请求只需通过一下步骤:
     建立普通socket-->初始化SSL-->将socket与SSL进行关联-->连接SSL-->通过SSL发送请求-->接收https应答-->关闭SSL连接和socket

int getPages(const char* host_addr, const int host_port, const char* pObject)
{
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 0), &wsaData);
    
    SOCKET sockfd;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        DebugMsg("Socket Error: %s", strerror(errno));
        return -1;
    }
    
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(host_port);
    server_addr.sin_addr.s_addr = inet_addr(host_addr);
    
    if (connect(sockfd, (struct sockaddr *) (&server_addr), sizeof(struct sockaddr)) == -1)
    {
        DebugMsg("Connect Error:%s", strerror(errno));
        return -1;
    }
    
    SSL *ssl;
    SSL_CTX *ctx;
    // SSL initialize
    SSL_library_init();
    SSL_load_error_strings();
    ctx = SSL_CTX_new(SSLv23_client_method());
    if (ctx == NULL)
    {
        DebugMsg("SSL CTX new failed!");
        return -1;
    }
    
    ssl = SSL_new(ctx);
    if (ssl == NULL)
    {
        DebugMsg("SSL new failed!");
        return -1;
    }
    
    // link socket & SSL
    int ret = SSL_set_fd(ssl, sockfd);
    if (ret == 0)
    {
        DebugMsg("SSL link socket failed!");
        return -1;
    }
    
    RAND_poll();
    while (RAND_status() == 0)
    {
        unsigned short rand_ret = rand() % 65536;
        RAND_seed(&rand_ret, sizeof(rand_ret));
    }
    
    // SSL connect
    ret = SSL_connect(ssl);
    if (ret != 1)
    {
        DebugMsg("SSL connect failed!");
        return -1;
    }
    
    char getRequest[1024];
    sprintf(getRequest, "GET %s HTTP/1.0\r\nHost: %s\r\nCookie: %s\r\n\r\n", pObject, "10.18.4.80", cookie);
    
    // send https request
    int totalsend = 0;
    int requestLen = strlen(getRequest);
    while (totalsend < requestLen)
    {
        int send = SSL_write(ssl, getRequest + totalsend, requestLen - totalsend);
        if (send == -1)
        {
            DebugMsg("SSL send failed!");
        }
        totalsend += send;
        DebugMsg("%d bytes send OK!", totalsend);
    }
    
    // receive https response
    int responseLen = 0;
    int i = 0;
    char buffer[8];
    memset(buffer, 0, 8);
    char returnBuffer[1024];
    memset(returnBuffer, 0, 1024);
    int p = 0;
    while ((responseLen = SSL_read(ssl, buffer, 1)) == 1 && p<1024)
    {
        returnBuffer[p] = buffer[0];
        p++;
        if (i < 4)
        {
            if (buffer[0] == '\r' || buffer[0] == '\n')
                i++;
            else
                i = 0;
        }
    }
    
    DebugMsg("%s", returnBuffer);
    
    // shutdown community 
    ret = SSL_shutdown(ssl);
    if (ret != 1)
    {
        DebugMsg("SSL shutdown failed!");
        return -1;
    }
    SSL_free(ssl);
    SSL_CTX_free(ctx);
    ERR_free_strings();
    closesocket(sockfd);
    WSACleanup();
    return 0;
}

阅读(612) | 评论(0) | 转发(0) |
0

上一篇:linux编程所用的一些工具

下一篇:没有了

给主人留下些什么吧!~~