在Linux上使用套接字发出https请求 [英] Make an https request using sockets on linux

查看:68
本文介绍了在Linux上使用套接字发出https请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Linux上使用套接字发出http请求?目前,我正在

How do I make an http request using sockets on linux? currently, I'm getting

HTTP/1.1 301 Moved Permanently
//etc
Location: https://server.com

这是代码的相关部分(该函数太大,无法在此处发布):

here's relevant part of code(the function is too big to post here):

 /* Socket file descriptor. */
        int sock;
    struct sockaddr_in sockaddr;
    struct hostent *host; /* Host information. */
    sock = socket(AF_INET, /* IPV4 protocol. */
              SOCK_STREAM, /* TCP socket. */
              0); /* O for socket() function choose the correct protocol based on the socket type. */

    if(sock == INVALID_SOCKET) return SOCK_GENERROR;

    if((host = gethostbyname(server)) == NULL) {
        close(sock);
        return SOCK_HOSTNFOUND;
    }

    /* zero buffer */
    memset(&sockaddr, 0, sizeof(sockaddr));
    sockaddr.sin_family = AF_INET;
    memcpy(&sockaddr.sin_addr,
           host -> h_addr,
           host -> h_length );
    sockaddr.sin_port = htons(port);

    if(connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == INVALID_SOCKET) {
        close(sock);
        return SOCK_FERRCONN;
    }

    if(send(sock, sendbuf, bufsize, 0) == INVALID_SOCKET) {
        close(sock);
        return SOCK_FERRWRITE;
    }


       if((readed = recv(sock, recvbuffer, sizeof(recvbuffer), 0)) <= 0)
    break;

在呼叫中

server ="server.com"; port = 80;

我试图删除我的onw例程,并从此代码中键入内容,以使您更加干净.

I tried to remove as possible my onw routines and type from this code to make more clean for you.

推荐答案

https 请求看起来像 http 请求一样,但是对客户端之间的实际通信进行了透明加密.和服务器,并在其他默认端口上.好消息是,透明加密使您可以像编写常规HTTP客户端一样进行编程.坏消息是加密非常复杂,您需要一个专门的库来为您实现它.

https requests look just like http requests, but with transparent encryption of the actual communication between the client and the server, and on a different default port. The good news is that transparent encryption allows you to program just like you're writing a regular HTTP client. The bad news is that the encryption is complex enough that you need a specialized library to implement it for you.

一个这样的库是 OpenSSL .使用OpenSSL,客户端的最少代码如下所示:

One such library is OpenSSL. Using OpenSSL, the minimal code for a client would look like this:

#include <openssl/ssl.h>

// first connect to the remote as usual, but use the port 443 instead of 80

// initialize OpenSSL - do this once and stash ssl_ctx in a global var
SSL_load_error_strings ();
SSL_library_init ();
SSL_CTX *ssl_ctx = SSL_CTX_new (SSLv23_client_method ());

// create an SSL connection and attach it to the socket
SSL *conn = SSL_new(ssl_ctx);
SSL_set_fd(conn, sock);

// perform the SSL/TLS handshake with the server - when on the
// server side, this would use SSL_accept()
int err = SSL_connect(conn);
if (err != 1)
   abort(); // handle error

// now proceed with HTTP traffic, using SSL_read instead of recv() and
// SSL_write instead of send(), and SSL_shutdown/SSL_free before close()

这篇关于在Linux上使用套接字发出https请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆