none
CInternetSession - open files via Internet RRS feed

  • Question

  •  

    In VS 6 I use that code:
    C++:
           CInternetSession InternetSession;
            CStdioFile *pHttpFile;
            CFile File;
            try {
                    m_sAktualizacja = "";
                    pHttpFile = InternetSession.OpenURL("http://site.pl/data.xml");

                    if (File.Open(pathTempDirectory+"\\data.xml", CFile::modeWrite | CFile::modeCreate))
                    {
                            char pBuffer[200];
                            while (pHttpFile->ReadString(pBuffer, 199) != NULL)
                            {
                                    CString strLinia1(pBuffer);}
                    }
                    pHttpFile->Close();
            }
            catch(CInternetException *e)
            {
                    e->Delete();
            }


    and everythink is OK. 
    In VS 2005 the same code does not work. I changed it.
    C++:

            CInternetSession InternetSession;
            CStdioFile *pHttpFile;
            CFile File;
            try {
                    m_sAktualizacja = "";

                    // Konwersja CString na LPTSTR
                    CString theString("http://site.pl/data.xml");
                    int sizeOfString = (theString.GetLength() + 1);
                    LPTSTR url = new TCHAR[sizeOfString];
                    _tcscpy_s(url, sizeOfString, theString);
                    // Koniec
                   
                    pHttpFile = InternetSession.OpenURL(url);

                    pathTempDirectory += "\\data.xml";
                    CString pathTemp (pathTempDirectory);
                    sizeOfString = (pathTempDirectory.GetLength() + 1);
                    LPTSTR path = new TCHAR[sizeOfString];
                    _tcscpy_s(path, sizeOfString, pathTemp);
                   

                    if (File.Open(path, CFile::modeWrite | CFile::modeCreate))
                    { 
                            TCHAR pBuffer[200];
                            while (pHttpFile->ReadString(pBuffer, 200) != NULL)
                            {
                                    CStringA strLinia1(pBuffer);            }
                    }
                    pHttpFile->Close();
            }
            catch(CInternetException *e)
            {
                    e->Delete();
            }


    but does not hit the correct data to the variable strLinia1


    Thx for all help.

    PS I don't want to turn on: Use Multi-Byte Character Set in project setting.

    Sunday, November 18, 2007 11:11 AM

Answers

  • Ok, I see the problem now.

    CStdioFile::ReadString is not converting the contents from char to wide char (it just casts).

    As a first approach, if you know your data is UTF-8 or similiar, you could simply cast back:

    CStringA strLinia1((LPSTR)pBuffer);

    or use MultibyteToWideChar with CStringW, but this will put you in a trouble if your "data.xml" is ever saved as UTF-16 or UNICODE.

    A more elegant solution - though it may not be useful to you - is to simply let the XML parser (Microsoft's or whoever you use) to automatically detect that (BOM, endianess, and so on). Just download the file to your temp directory with URLDownloadToFile and open the file from there.

    Hope this helps.

    Sunday, November 18, 2007 4:41 PM

All replies

  • Czesc!

    On one hand, the original code works for me when compiled under VS 2005 - with a different URL, since the site you are pointing does not contain any "data.xml", thus giving an error message - the same you see when typing the whole URL in your browser's address box.

    On the other, I guess that you have copied "theString" to "url" and "pathTemp" to "path" because of a change in your declaration of CInternetSession::OpenURL (accidentally overwritten?) - probably an omitted "const", i.e. LPTSTR instead of LPCTSTR, or a problem in the LPCTSTR cast operator of the CString class.

    Double check your header files and the path settings; you are probably using a modified MFC version.

    Sunday, November 18, 2007 1:09 PM
  • I write URL and name of file only for example.

    When I debuging code file is open for read but reading doesn't OK. 

    Problem is on the this function:

    Code Block

    while (pHttpFile->ReadString(pBuffer, 200) != NULL)

     

     

    and declaration pBuffer.

    In MSDN I read that "pBuffer" has to be declaration: char pBuffer[200].

    How so I will do this the mistake is generated:

    Code Block
    c:\documents and settings\...\moje dokumenty\visual studio 2005\projects\...\...\updatedlg.cpp(135) : error C2664: 'LPTSTR CStdioFile::ReadString(LPTSTR,UINT)' : cannot convert parameter 1 from 'char [200]' to 'LPTSTR'

     

     

    Sunday, November 18, 2007 1:47 PM
  • I thought you were compiling under MBCS.

    What are your settings under

    Configuration Properties-> General -> Character Set

    and

    Configuration Properties-> C/C++ ->Preprocessor

    ?

    Sunday, November 18, 2007 3:35 PM
  • Configuration Properties-> C/C++ ->Preprocessor -> WIN32;_WINDOWS;_DEBUG

     

    Configuration Properties-> General -> Character Set -> Use Unicode Character Set

     

    When I set Use Multi-Byte Character Set program work OK but I want keep setting Use Unicode Character Set.

    Meybe is possibility to switch on Multi-Byte Character Set only for this part of program ?

    Sunday, November 18, 2007 4:16 PM
  • Ok, I see the problem now.

    CStdioFile::ReadString is not converting the contents from char to wide char (it just casts).

    As a first approach, if you know your data is UTF-8 or similiar, you could simply cast back:

    CStringA strLinia1((LPSTR)pBuffer);

    or use MultibyteToWideChar with CStringW, but this will put you in a trouble if your "data.xml" is ever saved as UTF-16 or UNICODE.

    A more elegant solution - though it may not be useful to you - is to simply let the XML parser (Microsoft's or whoever you use) to automatically detect that (BOM, endianess, and so on). Just download the file to your temp directory with URLDownloadToFile and open the file from there.

    Hope this helps.

    Sunday, November 18, 2007 4:41 PM
  • You are great!

     

    This is the solution of the problem:

    Code Block

    CStringA strLinia1((LPSTR)pBuffer);

     

    I thank very much

    Sunday, November 18, 2007 6:11 PM
  • Proszę bardzo!
    Sunday, November 18, 2007 7:55 PM
  • Thank you, you saved my life.
    Tuesday, August 13, 2019 11:13 AM