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
    May 2010
    Posts
    38

    Problem With Internet WriteFile

    Hi,
    I have problems with below code:
    Code:
    HINTERNET	Initialize = InternetOpen(0, //Name of Application Calling (Agent)
    INTERNET_OPEN_TYPE_DIRECT, //Access Type
    NULL, //Proxy Name
    NULL, //Proxy Bypass
    0); //Flags
    
    HINTERNET hConnect = InternetConnect(Initialize, // InternetOpen handle
    server, // Server name
    80, // HTTPS port
    user, // User name
    pass, // User password
    INTERNET_SERVICE_HTTP, // Service
    0, // Flags
    0); // Context
    
    FtpSetCurrentDirectoryW(hConnect, path);
    HINTERNET hReq = HttpOpenRequest(hConnect, //InternetConnect handle
    L"POST", //Method
    r1, //Object name
    NULL, //Version
    NULL, //Referrer
    NULL, //Extra headers
    INTERNET_FLAG_KEEP_CONNECTION &&
    INTERNET_FLAG_NO_CACHE_WRITE, //Flags
    0); //Context
    
    HANDLE hFile = CreateFile(upFile, //File name
    GENERIC_READ, //Desired Access
    FILE_SHARE_READ, //Share Mode
    NULL, //Security Attribute
    OPEN_EXISTING, //Creation Disposition
    FILE_ATTRIBUTE_NORMAL, //Flags and Attributes
    NULL); //Template File
    
    if(hFile == INVALID_HANDLE_VALUE)
    {
    printf("\nFailed to open local file %s.", upFile);
    return FALSE;
    }
     
    //DWORD BufferIn.dwBufferTotal = GetFileSize(hFile, NULL);
    //printf("File size is %d\n", BufferIn.dwBufferTotal);
    BOOL bRead;
    BOOL bRead1;
    BYTE pBuffer[1024];
    BYTE pBuffer1[1024];
    DWORD dwBytesRead;
    BOOL bRet;
    bool result;
    DWORD dwBytesWritten;
     
    if(HttpSendRequestEx(hReq, NULL, 0, NULL,0))
    {
    DWORD sum = 0;
    do
    {
    if(!(bRead = ReadFile(hFile, pBuffer, sizeof(pBuffer), &dwBytesRead, NULL)))
    {
    printf("\nReadFile failed on buffer %lu.", GetLastError());
    result = FALSE;
    break;
    }
     
    
     
    if(!(bRet=InternetWriteFile(hReq, pBuffer, dwBytesRead, &dwBytesWritten)))
    {
    printf("\nInternetWriteFile failed %lu", GetLastError());
    if (GetLastError() == 12019)
    {
    cout << "\nERROR_INTERNET_INCORRECT_HANDLE_STATE\n" << endl;
    }
     result = FALSE;
    break;
    }
    sum += dwBytesWritten;
    }
    while(dwBytesRead == sizeof(pBuffer));
     
    CloseHandle(hFile);
    printf("\nActual written bytes: %d\n", sum);
    }
     
    /*close file , terminate server connection and
    deinitialize the wininet library*/
    InternetCloseHandle(hReq);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(Initialize);
    It runs and not shows any errors.
    But my problem is that not make any file and write on that in my server.
    Other my problem is how define path in my code?
    for example my file is in www.www.com/reza
    how make that read my file in "reza" folder(path).
    Regards,
    Last edited by rezame; September 18th, 2011 at 01:14 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem With Internet WriteFile

    Unfortunately it's very hard to read/understand the code snippet you have posted.
    The reason is very simple: you got rid of using Code tags.
    Last edited by VictorN; September 18th, 2011 at 11:39 AM.
    Victor Nijegorodov

  3. #3
    Join Date
    May 2010
    Posts
    38

    Re: Problem With Internet WriteFile

    Quote Originally Posted by VictorN View Post
    Unfortunately it very hard to read/understand the code snippet you have posted.
    The reason is very simple: you got rid of using Code tags.

    Hi,
    You mean I repost my code include tags?
    Regards,

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem With Internet WriteFile

    Quote Originally Posted by rezame View Post
    Hi,
    You mean I repost my code include tags?
    No, you have to edit your OP adding Code tags.
    Victor Nijegorodov

  5. #5
    Join Date
    May 2010
    Posts
    38

    Re: Problem With Internet WriteFile

    Code:
    INTERNET_BUFFERS BufferIn = {0};
        BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
    
        HINTERNET   hSession, hConnection, hRequest;
        wchar_t     hdr_buf[1024], error_buf[128];
        wchar_t     query_buf[1024] = {0,};
        BOOL        result;
    
        DWORD dwBytesRead=0;
        DWORD dwBytesWritten=0;
        BYTE pBuffer[1024];
    
        hSession = InternetOpen( 0, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
        if( hSession == NULL )
        {
            MessageBox( 0, L"HTTP Open failed!", L"HTTP", MB_OK );
            return FALSE;
        }
    
        hConnection = InternetConnect( hSession, server,
                                    INTERNET_DEFAULT_HTTP_PORT, user, pass,
                                    INTERNET_SERVICE_HTTP, INTERNET_FLAG_PASSIVE, 0 );
        if( hConnection == NULL )
        {
            wsprintf( error_buf, L"HTTP Connection failed (&#37;d)", GetLastError() );
            MessageBox( 0, error_buf, L"HTTP", MB_OK );
            InternetCloseHandle( hSession );
            return FALSE;
        }
    	FtpSetCurrentDirectory(hConnection,path);
        hRequest = HttpOpenRequest( hConnection, L"POST", r1,
                                    HTTP_VERSION, NULL, 0, INTERNET_FLAG_KEEP_CONNECTION &&
                                    INTERNET_FLAG_NO_CACHE_WRITE, 0 );
        if( hRequest == NULL )
        {
            wsprintf( error_buf, L"HTTP OpenRequest(POST) failed (%d)", GetLastError() );
            MessageBox( 0, error_buf, L"HTTP", MB_OK );
            InternetCloseHandle( hConnection );
            InternetCloseHandle( hSession );
            return FALSE;
        }
    
    
        HANDLE hFile = CreateFile(upFile, //File name
                                    GENERIC_READ, //Desired Access
                                    FILE_SHARE_READ, //Share Mode
                                    NULL, //Security Attribute
                                    OPEN_EXISTING, //Creation Disposition
                                    FILE_ATTRIBUTE_NORMAL, //Flags and Attributes
                                    NULL); //Template File
    
        if(hFile == INVALID_HANDLE_VALUE)
        {
            wsprintf( error_buf, L"file open failed (%d)", GetLastError() );
            MessageBox( 0, error_buf, L"HTTP", MB_OK );
            InternetCloseHandle( hConnection );
            InternetCloseHandle( hSession );
            return FALSE;
        }
    
        BufferIn.dwBufferTotal = GetFileSize(hFile, NULL);
        printf("File size is %d\n", BufferIn.dwBufferTotal);
    
    	
        if(HttpSendRequestEx(hRequest, NULL, 0, NULL,0))
        {
            DWORD sum = 0;
            do
            {
                if(!(ReadFile(hFile, pBuffer, sizeof(pBuffer), &dwBytesRead, NULL)))
                {
                    MessageBox( 0, L"Error Read File", L"HTTP", MB_OK );
                    break;
                }
    			
                if(!(InternetWriteFile(hRequest, pBuffer, dwBytesRead, &dwBytesWritten)))
                {
                    MessageBox( 0, L"Error Write File", L"HTTP", MB_OK );
                    break;
                }
    			
                sum += dwBytesWritten;
    			cout<<pBuffer<<"\n";
    			cout<<sum;
    			
            } while(dwBytesRead == sizeof(pBuffer));
    
        }
    
        InternetCloseHandle( hRequest );
        InternetCloseHandle( hConnection );
        InternetCloseHandle( hSession );
    	cout<<"\n";
    	 cout<<"Finished";
    Last edited by rezame; September 18th, 2011 at 12:57 PM.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem With Internet WriteFile

    Did you ever read the Announcement: Before you post....
    Don't you know what Code tag is?
    Victor Nijegorodov

  7. #7
    Join Date
    May 2010
    Posts
    38

    Re: Problem With Internet WriteFile

    Sorry,
    How add code Tag to my code?
    Regards,

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem With Internet WriteFile

    I've already posted you a link...
    Victor Nijegorodov

  9. #9
    Join Date
    May 2010
    Posts
    38

    Re: Problem With Internet WriteFile

    Quote Originally Posted by VictorN View Post
    Did you ever read the Announcement: Before you post....
    Don't you know what Code tag is?
    Hi,
    Thanks. sorry I am new in this forum. I done by php.

    Code:
    INTERNET_BUFFERS BufferIn = {0};
        BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
    
        HINTERNET   hSession, hConnection, hRequest;
        wchar_t     hdr_buf[1024], error_buf[128];
        wchar_t     query_buf[1024] = {0,};
        BOOL        result;
    
        DWORD dwBytesRead=0;
        DWORD dwBytesWritten=0;
        BYTE pBuffer[1024];
    
        hSession = InternetOpen( 0, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
        if( hSession == NULL )
        {
            MessageBox( 0, L"HTTP Open failed!", L"HTTP", MB_OK );
            return FALSE;
        }
    
        hConnection = InternetConnect( hSession, server,
                                    INTERNET_DEFAULT_HTTP_PORT, user, pass,
                                    INTERNET_SERVICE_HTTP, INTERNET_FLAG_PASSIVE, 0 );
        if( hConnection == NULL )
        {
            wsprintf( error_buf, L"HTTP Connection failed (&#37;d)", GetLastError() );
            MessageBox( 0, error_buf, L"HTTP", MB_OK );
            InternetCloseHandle( hSession );
            return FALSE;
        }
    	FtpSetCurrentDirectory(hConnection,path);
        hRequest = HttpOpenRequest( hConnection, L"POST", r1,
                                    HTTP_VERSION, NULL, 0, INTERNET_FLAG_KEEP_CONNECTION &&
                                    INTERNET_FLAG_NO_CACHE_WRITE, 0 );
        if( hRequest == NULL )
        {
            wsprintf( error_buf, L"HTTP OpenRequest(POST) failed (%d)", GetLastError() );
            MessageBox( 0, error_buf, L"HTTP", MB_OK );
            InternetCloseHandle( hConnection );
            InternetCloseHandle( hSession );
            return FALSE;
        }
    
    
        HANDLE hFile = CreateFile(upFile, //File name
                                    GENERIC_READ, //Desired Access
                                    FILE_SHARE_READ, //Share Mode
                                    NULL, //Security Attribute
                                    OPEN_EXISTING, //Creation Disposition
                                    FILE_ATTRIBUTE_NORMAL, //Flags and Attributes
                                    NULL); //Template File
    
        if(hFile == INVALID_HANDLE_VALUE)
        {
            wsprintf( error_buf, L"file open failed (%d)", GetLastError() );
            MessageBox( 0, error_buf, L"HTTP", MB_OK );
            InternetCloseHandle( hConnection );
            InternetCloseHandle( hSession );
            return FALSE;
        }
    
        BufferIn.dwBufferTotal = GetFileSize(hFile, NULL);
        printf("File size is %d\n", BufferIn.dwBufferTotal);
    
    	
        if(HttpSendRequestEx(hRequest, NULL, 0, NULL,0))
        {
            DWORD sum = 0;
            do
            {
                if(!(ReadFile(hFile, pBuffer, sizeof(pBuffer), &dwBytesRead, NULL)))
                {
                    MessageBox( 0, L"Error Read File", L"HTTP", MB_OK );
                    break;
                }
    			
                if(!(InternetWriteFile(hRequest, pBuffer, dwBytesRead, &dwBytesWritten)))
                {
                    MessageBox( 0, L"Error Write File", L"HTTP", MB_OK );
                    break;
                }
    			
                sum += dwBytesWritten;
    			cout<<pBuffer<<"\n";
    			cout<<sum;
    			
            } while(dwBytesRead == sizeof(pBuffer));
    
        }
    
        InternetCloseHandle( hRequest );
        InternetCloseHandle( hConnection );
        InternetCloseHandle( hSession );
    	cout<<"\n";
    	 cout<<"Finished";
    I think now it is good.
    Regards,
    Last edited by rezame; September 18th, 2011 at 12:57 PM.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem With Internet WriteFile

    Quote Originally Posted by rezame View Post
    Hi,
    Thanks. sorry I am new in this forum.
    No, you are not a "new".
    You have already got a similar request in this post about 3 months back. And you could change your post adding proper Code (not PHP since we discuss C++, not PHP) tags.
    Victor Nijegorodov

  11. #11
    Join Date
    May 2010
    Posts
    38

    Re: Problem With Internet WriteFile

    Quote Originally Posted by VictorN View Post
    No, you are not a "new".
    You have already got a similar request in this post about 3 months back. And you could change your post adding proper Code (not PHP since we discuss C++, not PHP) tags.
    Thanks for your advise.
    I updated code by Code Tag.
    I hope anybody can help me for solve this code problem
    Regards,

  12. #12
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: Problem With Internet WriteFile

    If this function fails you do show a messagebox alerting the user to an error...but do not show/log the error...

    Try using GetLastError() in the failure logic then insert that error into a string and displaying the exact error and not just a general error...just like you did for InternetConnect()
    You can also lookup the function InternetGetLastResponseInfo() as this might be of some help in debugging the actual error.



    Code:
    if(!(InternetWriteFile(hRequest, pBuffer, dwBytesRead, &dwBytesWritten)))
    {
       MessageBox( 0, L"Error Write File", L"HTTP", MB_OK );
       break;
    }
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  13. #13
    Join Date
    May 2010
    Posts
    38

    Re: Problem With Internet WriteFile

    Quote Originally Posted by Vanaj View Post
    If this function fails you do show a messagebox alerting the user to an error...but do not show/log the error...

    Try using GetLastError() in the failure logic then insert that error into a string and displaying the exact error and not just a general error...just like you did for InternetConnect()
    You can also lookup the function InternetGetLastResponseInfo() as this might be of some help in debugging the actual error.



    Code:
    if(!(InternetWriteFile(hRequest, pBuffer, dwBytesRead, &dwBytesWritten)))
    {
       MessageBox( 0, L"Error Write File", L"HTTP", MB_OK );
       break;
    }
    Hi,
    InternetWriteFile not shows any error.
    I checked that by GetLastError() and not shows any error.
    Regards,

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem With Internet WriteFile

    You are using HttpSendRequestEx. But did you read the HttpSendRequestEx function documentation? Such as
    Remarks

    HttpSendRequest performs both the send and the receive for the response. This does not allow the application to send any extra data beyond the single buffer that was passed to HttpSendRequest. ...
    After the call to HttpSendRequestEx, send the remaining data by calling InternetWriteFile. Finally, follow up with a call to HttpEndRequest.

    ...
    Victor Nijegorodov

  15. #15
    Join Date
    May 2010
    Posts
    38

    Re: Problem With Internet WriteFile

    Quote Originally Posted by VictorN View Post
    You are using HttpSendRequestEx. But did you read the HttpSendRequestEx function documentation? Such as
    Thanks Victor,
    I used HttpEndRequest after InternetWriteFile too, but not works again too and not write on file.
    Regards,

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