ZUDN

博客园 首页 新随笔 联系 订阅 管理
 


BOOL PhpServer::SendTrack(const CString& strFileURLInServer,
const CString& strFileLocalFullPath,
const CString& strFileResult)//发送数据
{
    //CString strFileLocalFullPath;

//strFileLocalFullPath = "C:\\GuoRuan\\pagefile.jpg";//要传的本地文件地址
    int startp = strFileLocalFullPath.ReverseFind('\\');
    int namelen = strFileLocalFullPath.GetLength()-startp-1;
 
    CString pcmname = strFileLocalFullPath;//.Mid(startp+1,namelen);

//由于是公司那边的服务器,我不能把地址按公司的给你们,你们自已找个图片上传PC端软件,然后抓包,进行发送
//www.GuoRuan.com这个网址是一个可以用PC端软件进行网站的文件传输,可以用抓包工具抓包查看它那边网站所要的信息对程序相应的修改
//本程序已在我所在的公司测试OK
    //CString strServer ="www.GuoRuan.com";//服务器名
    //CString strAddress ="/system/UploadScreen/upfile_article.asp";//保存的地址


    // USES_CONVERSION;
    CInternetSession Session;
    CHttpConnection *pHttpConnection = NULL;
    INTERNET_PORT  nPort = 80;
    CFile fTrack;
    CHttpFile* pHTTP;
    CString strHTTPBoundary;
    CString strPreFileData;
    CString strPostFileData;
    DWORD dwTotalRequestLength;
    DWORD dwChunkLength;
    DWORD dwReadLength;
    DWORD dwResponseLength;
    TCHAR szError[MAX_PATH];
    void* pBuffer;
    LPSTR szResponse;
    CString strResponse;
    BOOL bSuccess = TRUE;

    CString strDebugMessage;
   
    if (FALSE == fTrack.Open(strFileLocalFullPath, CFile::modeRead | CFile::shareDenyWrite))//读出文件
    {
      AfxMessageBox(_T("Unable to open the file."));
      return FALSE;
    }

    int iRecordID = 1;
    strHTTPBoundary = _T("---------------------------7b4a6d158c9");//定义边界值
    strPreFileData = MakePreFileData(strHTTPBoundary, pcmname, iRecordID);
    strPostFileData = MakePostFileData(strHTTPBoundary);

    dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength() + fTrack.GetLength();//计算整个包的总长度

  dwChunkLength = 64 * 1024;

    pBuffer = malloc(dwChunkLength);

    if (NULL == pBuffer)
    {
    return FALSE;
    }


DWORD dwType  =  0 ;
CString strServer = _T("");
CString strObject = _T("");
//INTERNET_PORT wPort  =  0 ;
//DWORD dwFileLength  =  0 ;
//char  *  pFileBuff  =  NULL;

//CHttpConnection  *  pHC  =  NULL;
//CHttpFile  *  pHF  =  NULL;
//CInternetSession cis;

BOOL bResult  =  AfxParseURL(strFileURLInServer, dwType, strServer, strObject, nPort);
char *pszBuffer = NULL;
    try
    {


      pHttpConnection = Session.GetHttpConnection(strServer,nPort);
      pHTTP = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
      pHTTP->AddRequestHeaders(MakeRequestHeaders(strHTTPBoundary));//发送包头请求
      pHTTP->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE);
     
    #ifdef _UNICODE
      pHTTP->Write(W2A(strPreFileData), strPreFileData.GetLength());
    #else
      pHTTP->Write((LPSTR)(LPCSTR)strPreFileData, strPreFileData.GetLength());//写入服务器所需信息
    #endif
     
      dwReadLength = -1;
      while (0 != dwReadLength)
      {
          strDebugMessage.Format(_T("%u / %u\n"), fTrack.GetPosition(), fTrack.GetLength());
          TRACE(strDebugMessage);
          dwReadLength = fTrack.Read(pBuffer, dwChunkLength);//文件内容
          if (0 != dwReadLength)
          {
                pHTTP->Write(pBuffer, dwReadLength);//写入服务器本地文件,用二进制进行传送
          }
      }
     
  #ifdef _UNICODE
      pHTTP->Write(W2A(strPostFileData), strPostFileData.GetLength());
    #else
      pHTTP->Write((LPSTR)(LPCSTR)strPostFileData, strPostFileData.GetLength());
    #endif
     
      pHTTP->EndRequest(HSR_SYNC);
      //
      //dwResponseLength = pHTTP->GetLength();
      //while (0 != dwResponseLength)
      //{
      // szResponse = (LPSTR)malloc(dwResponseLength + 1);
      // szResponse[dwResponseLength] = '\0';
      // pHTTP->Read(szResponse, dwResponseLength);
      // strResponse += szResponse;
      // free(szResponse);
      // dwResponseLength = pHTTP->GetLength();
      //}
      //
  HANDLE hFile = CreateFile(strFileResult, GENERIC_WRITE,
FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
NULL);//创建本地文件
if(hFile == INVALID_HANDLE_VALUE)
{
pHTTP->Close();
pHttpConnection->Close();
Session.Close();
return FALSE;
}
 
  const int BUFFER_LENGTH = 1024 * 10;
pszBuffer = new char[BUFFER_LENGTH];
DWORD dwWrite = 0;
DWORD dwTotalWrite = 0;
  UINT nRead = pHTTP->Read(pszBuffer, BUFFER_LENGTH);

while(nRead > 0)
{
WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL);
dwTotalWrite += dwWrite;
nRead = pHTTP->Read(pszBuffer, BUFFER_LENGTH);
}
delete []pszBuffer;
pszBuffer = NULL;
CloseHandle(hFile);


     
    }
    catch (CException* e)
    {
delete []pszBuffer;
pszBuffer = NULL;
      e->GetErrorMessage(szError, MAX_PATH);
      e->Delete();
      AfxMessageBox(szError);
      bSuccess = FALSE;
    }

    pHTTP->Close();
    delete pHTTP;
   
    fTrack.Close();
   
    if (NULL != pBuffer)
    {
      free(pBuffer);
    }
    return bSuccess;
}

CString PhpServer::MakeRequestHeaders(CString &strBoundary)//包头
{
    CString strFormat;
    CString strData;
   
    strFormat = _T("Content-Type: multipart/form-data; boundary=%s\r\n");//二进制文件传送Content-Type类型为: multipart/form-data
   
    strData.Format(strFormat, strBoundary);
    return strData;
}

CString PhpServer::MakePreFileData(CString &strBoundary, CString &strFileName, int iRecordID)
{
    /**/////////////////////////////////////////////////////////////////////////////////
    //Content-Type:
    //JPG image/pjpeg
    //PNG image/x-png
    //BMP image/bmp
    //TIF image/tiff
    //GIF image/gif
    CString strFormat;
    CString strData;


//strFormat += _T("--%s");
//  strFormat += _T("\r\n");
//strFormat += _T("Content-Disposition: form-data; name=\"para1\"");//传给网络上的参数,根据网站抓包查看到底是需要哪些
//  strFormat += _T("\r\n\r\n");
//strFormat += _T("my name is xiaoxiong");
//  strFormat += _T("\r\n");
//
//strFormat += _T("--%s");
//  strFormat += _T("\r\n");
//strFormat += _T("Content-Disposition: form-data; name=\"para2\"");
//  strFormat += _T("\r\n\r\n");
//strFormat += _T("国软软件研究所");
//  strFormat += _T("\r\n");

    strFormat += _T("--%s");
    strFormat += _T("\r\n");
    strFormat += _T("Content-Disposition: form-data; name=\"path\"; filename=\"%s\"");//文件地址信息
    strFormat += _T("\r\n");
    strFormat += _T("Content-Type: application/octet-stream");

    strFormat += _T("\r\n\r\n");
    strData.Format(strFormat, strBoundary, strFileName);//

    return strData;
}

CString PhpServer::MakePostFileData(CString &strBoundary)//发送请求包
{
    CString strFormat;
    CString strData;

strFormat = _T("\r\n");
    strFormat += _T("--%s");
    strFormat += _T("\r\n");
    strFormat += _T("Content-Disposition: form-data; name=\"submitted\"");
    strFormat += _T("\r\n");
    strFormat += _T("--%s--");
    strFormat += _T("\r\n");


    strData.Format(strFormat, strBoundary, strBoundary);

    return strData;
}
posted on 2011-09-15 14:36  ZUDN  阅读(4666)  评论(0编辑  收藏  举报