您的位置:首页 > 理论基础 > 计算机网络

Connecting through Http or Https for CE/Windows Mobile c++/vc++方法一

2011-06-10 16:41 946 查看
一:头文件

#include <altcecrt.h>
#include <Wininet.h>
#include "Afxinet.h"
#include <wincrypt.h>

二:.h文件

public:
CString sReturnXmlString;
CString sExceptionError;
DWORD dwRetValue;
CString szHeaders;
DWORD dwHttpRequestFlags;
CInternetSession session;
CHttpConnection* pServer;
CHttpFile* pHttpFile;
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
bool GetHttpsConnection(CString url);
bool SendRequest(CString *sSendXML,CString strWorkPath);

三:.cpp文件

初始化:

szHeaders = _T("Content-Type: application/x-www-form-urlencoded;Accept: text/xml, text/plain, text/html, text/htm/r/n");
strServerName = "10.130.124.193";
strObject = "";
sReturnXmlString ="";
sExceptionError = "";

函数:

bool CHTTPSDlg::GetHttpsConnection(CString url)//URL地址
{
try
{
if (!AfxParseURL(url, dwServiceType, strServerName, strObject, nPort)) //地址解析
{
AfxMessageBox(_T("Error: can only use URLs beginning with http or https"));
}
session.EnableStatusCallback(TRUE); //session值设置 比如超时
//session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5);
//session.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 5);
//session.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 5);

//// The delay value in milliseconds to wait between connection retries.
//session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1);
//// The retry count to use for Internet connection requests. If a connection
//// attempt still fails after the specified number of tries, the request is canceled.
//// The default is five.
//session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,1);
// session.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 7000); // 7秒的接收超时
dwHttpRequestFlags = INTERNET_FLAG_NO_AUTO_REDIRECT|INTERNET_FLAG_SECURE;//|INTERNET_FLAG_IGNORE_CERT_CN_INVALID;//|INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;
//if it is not working add below flags
// INTERNET_FLAG_IGNORE_CERT_CN_INVALID | // INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;
pServer = session.GetHttpConnection(strServerName, nPort);
pHttpFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,strObject,NULL,(DWORD)this,NULL,NULL,dwHttpRequestFlags ); // 上传HTTP_VERB_POST 下载HTTP_VERB_GET
if (pHttpFile != NULL)
{
//AfxMessageBox(_T("OpenRequest成功!/r/n"));
}
else
{
AfxMessageBox(_T("OpenRequest失败!/r/n"));
}

}
catch(CException *e)
{
TCHAR szCause[255];
e->GetErrorMessage(szCause,255);
sExceptionError = _T("Error: ");
sExceptionError += szCause;
pServer = NULL;
pHttpFile = NULL;
return FALSE;
}
return true;
}

bool CHTTPSDlg::SendRequest(CString *sSendXML,CString strWorkPath)//strWorkPath下载和上传返回值所存储的文件路径

{//sSendXML 这个值随便设置
CString sReturnXmlString;
try
{
pHttpFile->AddRequestHeaders(szHeaders);
pHttpFile->SendRequest(szHeaders, (void*) (LPCTSTR) sSendXML, sSendXML->GetLength() );
pHttpFile->QueryInfoStatusCode(dwRetValue);
DWORD iFileLength = (DWORD)pHttpFile->GetLength();//最多833字节
//int iFileLength = pHttpFile->GetLength();
char *fBuf = new char[1024];
char* pszBuffer = NULL;
if (dwRetValue == HTTP_STATUS_OK)
{/*
UINT nRead = pHttpFile->Read(fBuf, 1024);
while(nRead > 0)
{
sReturnXmlString = CString(fBuf);
CFile file;
if(file.Open(strWorkPath,CFile::modeCreate | CFile::modeReadWrite))
{
file.SeekToEnd();
int i = sReturnXmlString.GetLength();
file.Write(sReturnXmlString,i);
}
nRead = pHttpFile->Read(fBuf, 1024);
}*/
//--------------------------
HANDLE hFile = CreateFile(strWorkPath, GENERIC_WRITE,FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL); //创建本地文件
if(hFile == INVALID_HANDLE_VALUE)
{
pHttpFile->Close();
session.Close();
return false;
}
char szInfoBuffer[1000]; //返回消息
DWORD dwFileSize = 0; //文件长度
DWORD dwInfoBufferSize = sizeof(szInfoBuffer);
BOOL bResult = FALSE;
bResult = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,
(void*)szInfoBuffer,&dwInfoBufferSize,NULL);

dwFileSize = atoi(szInfoBuffer);
const int BUFFER_LENGTH = 1024 * 10;
pszBuffer = new char[BUFFER_LENGTH]; //读取文件的缓冲
DWORD dwWrite, dwTotalWrite;
dwWrite = dwTotalWrite = 0;
UINT nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH); //读取服务器上数据
while(nRead > 0)
{
WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL); //写到本地文件
dwTotalWrite += dwWrite;
nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH);
}
delete[]pszBuffer;
pszBuffer = NULL;
CloseHandle(hFile);
//--------------------
return true;
}
else
{
return false;
}
}
catch(CException *e)
{
TCHAR szCause[255];
e->GetErrorMessage(szCause,255);
sExceptionError = _T("Error: ");
sExceptionError += szCause;
return false;
}
return false;
}

参考文档:

http://blog.csdn.net/long80226/archive/2010/04/11/5471879.aspx HTTPS
http://www.tinystrong.com/post-data-for-inet#more-601 小强工作室

http://social.msdn.microsoft.com/Forums/zh-CN/vcmfcatl/thread/e99b751d-c48c-48ea-8d48-c09e379e4c6e HTTPS
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐