VC++访问https的网站
(2012-01-31 12:36:44)| 分类: vc |
HTTPS是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。在vc++里面我实现的方法是利用winlnet api编程
正常winlnet访问http的网站的步骤是:
1.调用InternetOpen函数
2.调用InternetConnect函数
3.调用HttpOpenRequest函数
4.调用HttpSendRequest函数
5.可以调用InternetReadFile函数
而访问https的网站与http网站的区别:
1.在第2步打开回话的时候设定的端口 http是80 https是443
2.在第3步创建请求句柄的时候设置的Flag不同
访问http的时候可以是ConnFlag=INTERNET_FLAG_RELOAD|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_NO_CACHE_WRITE;
访问https的时候可以是ConnFlag=INTERNET_FLAG_RELOAD|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_NO_CACHE_WRITE
其余的步骤都是相同的 第4步发送的消息主体是什么内容就发什么内容,第5步接收到的也是ssl层解密后的内容,就是说除了设置端口和Flag的时候有些区别ssl层基本上是透明的
下面是一个能简单实现访问http/https的类
GettCon.h
// GettCon.h: interface for the GettCon class.
//
//////////////////////////////////////////////////////////////////////
#if
!defined(AFX_GETTCON_H__4B47EEFF_7202_43DD_9548_896D9198D39D__INCLUDED_)
#define
AFX_GETTCON_H__4B47EEFF_7202_43DD_9548_896D9198D39D__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include
<windows.h>//
#include <wincrypt.h>
#pragma comment (lib, "crypt32.lib")
#include <wininet.h>
#pragma
#include<string>
using namespace std;
class GettCon
{
public:
private:
};
#endif //
!defined(AFX_GETTCON_H__4B47EEFF_7202_43DD_9548_896D9198D39D__INCLUDED_)
// GettCon.cpp: implementation of the GettCon class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GettCon.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
GettCon::GettCon()
{
}
GettCon::~GettCon()
{
}
bool GettCon::ConnnectToServer(string SAddr,string DPath,bool
SslState,string Verb)
{
}
void GettCon::ClearHandles()
{
}
bool
{
}
string GettCon::GetResult()
{
}
函数的使用方法
1.GettCon con;
2.调用con.ConnnectToServer(服务器IP地址,子路径,是否为https,"POST"/"GET"/"PUT");
3.调用con.SendRequest(要发送的消息主体,主体长度(字节为单位))
4.调用con.GetResult()函数获得从服务器返回的字符串
在网上有童鞋说每发送一次数据最好是把2-4步重做一遍不然容易出错,不知道是不是这样。。。。。

加载中…