高分请教C++/VC++向https服务器发送请求的问题

wonglaye 2006-04-19 11:49:09
我想向https服务器发送GET请求(只是简单的发送请求得到响应),请问在C++或MFC下有无库支持?
我目前的尝试是使用SSL,但是走到
SSL_set_fd (ssl, sd); //ssl是SLL实例 sd是tcp socket
SSL_connect (ssl);
时候就会出现错误,错误代码10054,说连接被强行关闭

以下是代码:
// OpenSSL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <memory.h>
#include <errno.h>
#include <winsock2.h>
#include <rsa.h>
#include <crypto.h>
#include <x509.h>
#include <pem.h>
#include <ssl.h>
#include <err.h>
#include <rand.h>



int main(int argc, char* argv[])
{

int err;
struct sockaddr_in sa;
SSL_CTX* ctx;
SSL* ssl;
X509* server_cert;
char* str;
char buf [4096];
SSL_METHOD *meth;

SSLeay_add_ssl_algorithms();
meth = SSLv2_client_method();
SSL_load_error_strings();
ctx = SSL_CTX_new (meth);
if(ctx==NULL) cout<<"ERROR CTX"<<endl;

/* Create a socket and connect to server using normal socket calls. */
WSADATA Data;
if(WSAStartup(MAKEWORD(2,2),&Data)!=0) cout<<"error"<<endl;
SOCKET sd;
sd=socket(AF_INET,SOCK_STREAM,0);


sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr ("65.54.179.228");
sa.sin_port = htons (443);

err = connect(sd,(struct sockaddr*)&sa,sizeof(sa));
if(err==-1)
{
cout<<"ERROR SSL CONNECT...The Error Code is "<<WSAGetLastError()<<endl;
return 0;
}

/* Now we have TCP conncetion. Start SSL negotiation. */

ssl = SSL_new (ctx);
err=SSL_set_fd(ssl, sd);
cout<<err<<endl; //这里返回err为1,不知道成功否?
err = SSL_connect (ssl);
if(err==-1)
{
cout<<"ERROR SSL CONNECT...The Error Code is "<<WSAGetLastError()<<endl;
return 0;
}

/* Following two steps are optional and not required for
data exchange to be successful. */

/* Get the cipher - opt */

printf ("SSL connection using %s\n", SSL_get_cipher (ssl));

/* Get server's certificate (note: beware of dynamic allocation) - opt */

server_cert = SSL_get_peer_certificate (ssl);
printf ("Server certificate:\n");

str = X509_NAME_oneline (X509_get_subject_name (server_cert),0,0);
printf ("\t subject: %s\n", str);
OPENSSL_free (str);

str = X509_NAME_oneline (X509_get_issuer_name (server_cert),0,0);
printf ("\t issuer: %s\n", str);
OPENSSL_free (str);

/* We could do all sorts of certificate verification stuff here before
deallocating the certificate. */

X509_free (server_cert);


/* DATA EXCHANGE - Send a message and receive a reply. */

err = SSL_write (ssl, "GET /rdr/pprdr.asp HTTP/1.0\r\n\r\n", strlen("GET /rdr/pprdr.asp HTTP/1.0\r\n\r\n"));

err = SSL_read (ssl, buf, sizeof(buf) - 1);
buf[err] = '\0';
printf ("Got %d chars:'%s'\n", err, buf);
SSL_shutdown (ssl);



closesocket (sd);
SSL_free (ssl);
SSL_CTX_free (ctx);

return 0;
}
...全文
1139 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zeronow 2006-09-11
  • 打赏
  • 举报
回复
塔破铁鞋无觅处,默然回首谢coolzdp
lzd 2006-04-19
  • 打赏
  • 举报
回复
http://www.codeproject.com/internet/wininet_ssl___certificate.asp

http://www.codeproject.com/library/lyoulhttpclient.asp
wonglaye 2006-04-19
  • 打赏
  • 举报
回复
刚刚查了SSL的手册,SSL_set_fd(ssl, sd);返回1是成功,说明问题出在 SSL_connect
各位帮忙
wonglaye 2006-04-19
  • 打赏
  • 举报
回复
up
wonglaye 2006-04-19
  • 打赏
  • 举报
回复
但还是请告诉我这个socket和ssl连接,是怎么个错误
wonglaye 2006-04-19
  • 打赏
  • 举报
回复
只知道C不知道MFC,我真的真的很失败阿
ajn_sailing 2006-04-19
  • 打赏
  • 举报
回复
up
wonglaye 2006-04-19
  • 打赏
  • 举报
回复
谢谢lzd,coolzdp

mfc的也可以阿,谢谢我正在看winnet和CHttpConnection

能否麻烦看看这个openssl的连接怎么出问题,
用SSL_get_error(ssl,err)得到的错误是
SSL_ERROR_SYSCALL
Some I/O error occurred. The OpenSSL error queue may contain more information on the error. If the error queue is empty (i.e. ERR_get_error() returns 0), ret can be used to find out more about the error: If ret == 0, an EOF was observed that violates the protocol. If ret == -1, the underlying BIO reported an I/O error (for socket I/O on Unix systems, consult errno for details).


coolzdp 2006-04-19
  • 打赏
  • 举报
回复
不好意思没看清lz用C的socket。上面只是用wininet类
coolzdp 2006-04-19
  • 打赏
  • 举报
回复
不需要证书的话直接用wininet类,
OpenRequest设置dwFlags为
INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;

就完了。都有封装好现成的不知道搂住为什么要自己弄ssl?

18,363

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧