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

一:头文件

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值