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
#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
相关文章推荐
- one or more files from the emulator for windows ce installation is missing 解决方法
- (转)one or more files from the emulator for windows ce installation is missing 解决方法
- win7下eMbedded Visual C++ 4.0不能仿真(one or more files from Emulator for Windows CE is missing)解决
- 用c++ 操作mysql 数据库类(for linux or windows and others)
- GSL安装使用方法MinGW 、 VC、Borland C++ Builder、Windows开发环境下的使用
- 用c++ 操作mysql 数据库类(for linux or windows and others)
- 用c++ 操作mysql 数据库类(for linux or windows and others)
- Emulator for windows CE安装中有一个或多个文件丢失的解决方法。
- 用c++ 操作mysql 数据库类(for linux or windows and others)
- 谈谈MFC for WindowsCE中的CDC::GetClipBox方法
- 规范化的C++编程方法备忘录 c++ for windows
- One or more files from the Emulator for Windows CE installation is missing.Please reinstall Emulator for Windows CE and try agai
- 规范化的C++编程方法备忘录 c++ for windows(2)
- https使用HttpWebRequest出现错误:System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.的解决方法
- GSL 安装使用方法 MinGW 、 VC、Borland C++ Builder、Windows开发环境下的使用
- 用c++ 操作mysql 数据库类(for linux or windows and others)
- jquery读取本地文件,Windows上报错。XMLHttpRequest cannot load xxx. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.k.cors.a.c
- 有谁再wince6.0 上跑过Qt libraries 4.8.2 for Windows CE http://qt.nokia.com/downloads/win-ce-cpp
- VC++ Windows平台字符透明编程大总结 - [C/C++]
- 在 Borland C++BuilderX for Windows 中进行 GTK+ 编程