在linux上使用套接字发出https请求
发布时间:2020-12-13 19:14:41 所属栏目:Linux 来源:网络整理
导读:如何在 linux上使用套接字发出http请求?目前,我正在接受 HTTP/1.1 301 Moved Permanently//etcLocation: https://server.com 这里是相关的代码部分(函数太大了,不能在这里发布): /* Socket file descriptor. */ int sock; struct sockaddr_in sockaddr; st
如何在
linux上使用套接字发出http请求?目前,我正在接受
HTTP/1.1 301 Moved Permanently //etc Location: https://server.com 这里是相关的代码部分(函数太大了,不能在这里发布): /* 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,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例程并从此代码中键入以使您更加干净. 解决方法
https请求看起来就像http请求,但是对客户端和服务器之间的实际通信以及不同的默认端口进行透明加密.好消息是,透明加密允许您像编写常规HTTP客户端一样进行编程.坏消息是加密很复杂,你需要一个专门的库来实现它.
一个这样的库是OpenSSL.使用OpenSSL,客户端的最小代码如下所示: #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 – 对于多个大型软件安装,什么是好的解决方案?
- redhat – 如何强制NetworkManager更新/etc/resolv.conf?
- linux – 使用rsync通过多级SSH复制文件
- Linux Shell中反引号``, 单引号'', 双引号"&#
- 编译 – 为Linux编译CUDA SDK V4.1.28?
- linux – 如何在复制大文件时减少资源使用?
- Linux3.4.2移植CS8900网卡驱动到优龙FS2410开发板
- Linux下cut命令使用
- linux – 定义Global Descriptor和Local Descriptor表的数据
- linux – 使用bash脚本更新CRON