CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    InternetWriteFile write 0 Byte file

    Hello all
    i'm trying to upload a file from my local system to a http web server (currently localhost) . the code is based on
    http://support.microsoft.com/kb/184352/

    This caused error and didn't create a file , then i added HttpSendRequest (hRequest, NULL, 0, NULL, 1); before the HttpSendRequestEx , and this create a file in the server . the HttpEndSession returns ERROR_INTERNET_FORCE_RETRY again and again .
    How to make it work correct ?

    I have seen many threads in this site regarding this problem , but didn't saw any solution to this .

  2. #2
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: InternetWriteFile write 0 Byte file

    You mean added a code to Send request of 0 bytes ? Thats wrong, you have sent optional data as NULL and specified it's size as 1 byte, it should not be used that way.

    Also what was the problem with original code on M$ support site ? what line does it fail ? What's last error(GetLastError()) ?
    Regards,
    Ramkrishna Pawar

  3. #3
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: InternetWriteFile write 0 Byte file

    ohh..ok .. i changed that code..well still now nothing positive happens
    it just create a zero sized file . the problem with M$ code was ..it doesn't create anything .
    all functions succeed except the endsession returns error code 12032 (ERROR_INTERNET_FORCE_RETRY ) , and if retried the next endsession function timesout .

  4. #4
    Join Date
    May 2005
    Location
    Oradea, Romania
    Posts
    190

    Re: InternetWriteFile write 0 Byte file

    Hello, I remember a while ago I tried to get that example to work and didn't manage so I wrote a wrapper for InternetWriteFile (in a slightly different manner) to handle the upload. Here's the code, maybe it'll help you
    Code:
    class WinInternetWrapper
    {
    public:
    	WinInternetWrapper() 	{}
    
            //url is the path ("www.myserver.com/myfile.txt")
            //bytes is the size of the file to be uploaded
    	bool Open(const char* url, DWORD bytes)
    	{
    		const	BufferLength = 255;
    		char	Host[BufferLength + 1];
    		char	Path[BufferLength + 1];
    
    		URL_COMPONENTS uc;
    		memset(&uc, 0, sizeof uc);
    		uc.dwStructSize = sizeof uc;
    		uc.lpszHostName = Host;	uc.dwHostNameLength = BufferLength;
    		uc.lpszUrlPath = Path; uc.dwUrlPathLength = BufferLength;
    		InternetCrackUrl(url, static_cast<DWORD>(strlen(url)), ICU_DECODE, &uc);
    
    		mInet = InternetOpen(_T("InetUploader"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
    		mIcon = InternetConnect(mInet,Host,INTERNET_DEFAULT_HTTP_PORT,0,0,INTERNET_SERVICE_HTTP,0,0);
    
    		if (!mInet)
    		{
    			return false;
    		}
    
    		mFile = HttpOpenRequest(mIcon,"PUT",Path,"",0,0,INTERNET_FLAG_NO_AUTO_REDIRECT,0);
    
    		INTERNET_BUFFERS BufferIn = {0};
    		BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
    
    		if (bytes)
    		{
    			BufferIn.dwBufferTotal = bytes;
    			if (!HttpSendRequestEx(mFile, &BufferIn, 0, HSR_INITIATE, 0))
    				return false;
    		}
    		else
    		{
    			if (!::HttpSendRequest(mFile, "", 0, 0, 0))
    				return false;
    			mReadMode = true;
    		}
    
    		return true;
    	}
            bool Write(BYTE* buf, DWORD size, DWORD* written)
    	{
    		return (InternetWriteFile(mFile, buf, size, written) == TRUE);
    	}
            void Close()
    	{
    		InternetCloseHandle(mFile);
    		InternetCloseHandle(mInet);
    	}
    protected:
    	HINTERNET	mInet;
    	HINTERNET	mIcon;
    	HINTERNET	mFile;
    }

  5. #5
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: InternetWriteFile write 0 Byte file

    Thanks .. i'll check with ur code

  6. #6
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: InternetWriteFile write 0 Byte file

    Sorry EoF ..it didn't work..
    is there any (special) settings to be done in IIS , i have given the write permissions.

  7. #7
    Join Date
    May 2005
    Location
    Oradea, Romania
    Posts
    190

    Re: InternetWriteFile write 0 Byte file

    You might have to register your file MIME type, I don't remember if you have to do this or not. In IIS expand your site, select the directory in which you want to save the file and open the Properties dialog. Go to the HTTP Headers tab and click on the "MIME Types" button. Now add a new MIME. I hope this works for you.

    P.S. It doesn't work as in it doesn't create anything or still only creates a 0-length file?

  8. #8
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: InternetWriteFile write 0 Byte file

    i have added .txt and text as mime types, and it didn't help.
    it creates the file , without any content , eventhough the internetwritefile succeeds!!.

  9. #9
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: InternetWriteFile write 0 Byte file

    the complete code
    Code:
    	DWORD ptr = 0x1001;
    	HINTERNET hi_io = InternetOpen("vc_agent",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
    	HINTERNET hi_ic = InternetConnect(hi_io,"localhost",INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,ptr);
    	UseHttpSendReqEx(hi_ic,"c:\\readme.txt");
    	InternetCloseHandle(hi_io);
    Code:
    BOOL UseHttpSendReqEx(HINTERNET hConnect, TCHAR *upFile)
       {
         INTERNET_BUFFERS BufferIn = {0};
         DWORD dwBytesRead;
         DWORD dwBytesWritten;
         BYTE pBuffer[1024]; // Read from file in 1K chunks
         BOOL bRead, bRet;
    
         BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
    
         HINTERNET hRequest = HttpOpenRequest (hConnect, "PUT",
             "/test/test.txt", NULL, NULL, NULL,  0, 0);
    
         HANDLE hFile = CreateFile (upFile, GENERIC_READ, FILE_SHARE_READ,
             NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    
         BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
    
         HttpSendRequest (hRequest, NULL, 0, NULL, 0);
    
    Again:
         if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
         {
           TRACE( "Error on HttpSendRequestEx %lu\n",GetLastError() );
           return FALSE;
         }
    
         DWORD sum = 0;
         do
         {
           if  (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
               &dwBytesRead, NULL)))
           {
             TRACE ("\nReadFile failed on buffer %lu.",GetLastError());
             break;
           }
           if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
               &dwBytesWritten)))
           {
             TRACE ("\nInternetWriteFile failed %lu", GetLastError());
             break;
           }
           sum += dwBytesWritten;
         }
         while (dwBytesRead == sizeof(pBuffer)) ;
    
         
         TRACE ("Actual written bytes: %d\n", sum);
    
    	
         if(!HttpEndRequest(hRequest, NULL, 0, 0))
         {
           TRACE( "Error on HttpEndRequest %lu \n", GetLastError());
    	   goto Again;
         
         }
               CloseHandle (hFile);
    	InternetCloseHandle(hRequest);
    
         return TRUE;
       }

  10. #10
    Join Date
    May 2005
    Location
    Oradea, Romania
    Posts
    190

    Re: InternetWriteFile write 0 Byte file

    I have made a small error in the code I have previously posted. I have attached a .h file that contains the code for the Wrapper. I have tested it and it works.
    Attached Files Attached Files

  11. #11
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: InternetWriteFile write 0 Byte file

    i tried with this code
    Code:
    	WininetWrapper wp;
    	wp.Open("http://localhost/test/text.txt",1);
    	BYTE buf[]={1};
    	DWORD wr=0;
    	wp.Write(buf,1,&wr);
    	wp.Close();
    with ur new .h file

    result .. no file created .. anything wrong in the usage ?
    thanks

  12. #12
    Join Date
    May 2005
    Location
    Oradea, Romania
    Posts
    190

    Re: InternetWriteFile write 0 Byte file

    The usage seems correct. I have retested it and it works...I am uploading the test project.
    Attached Files Attached Files

  13. #13
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: InternetWriteFile write 0 Byte file

    let me check out

  14. #14
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: InternetWriteFile write 0 Byte file

    still not working . creates a zero byte file.
    i'm running ur code in winxp with iis5.1 . is there any settings u have done ? .
    after/during the the httpsendrequestex call i get some exceptions
    Code:
    'InternetConnect.exe': Loaded 'C:\WINDOWS\system32\oleaut32.dll', No symbols loaded.
    'InternetConnect.exe': Loaded 'C:\WINDOWS\system32\dnsapi.dll', No symbols loaded.
    First-chance exception at 0x7c812a5b in InternetConnect.exe: 0x000006BA: The RPC server is unavailable.
    First-chance exception at 0x7c812a5b in InternetConnect.exe: 0x000006BA: The RPC server is unavailable.
    First-chance exception at 0x7c812a5b in InternetConnect.exe: 0x000006BA: The RPC server is unavailable.
    First-chance exception at 0x7c812a5b in InternetConnect.exe: 0x000006BA: The RPC server is unavailable.
    'InternetConnect.exe': Loaded 'C:\WINDOWS\system32\hnetcfg.dll', No symbols loaded.
    'InternetConnect.exe': Loaded 'C:\WINDOWS\system32\wshtcpip.dll', No symbols loaded.
    the rpc server is running as per service list

  15. #15
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    Re: InternetWriteFile write 0 Byte file

    ok.. i changed my server , now i'm using a win2000 server running iis , and now it works.
    i think the problem was with my server/machine .

    thanks EoF for helping me
    btw if anyone knows why the exception is thrown pls inform me

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured