none
Does InternetWriteFile returning 1 mean that the file was uploaded?

    Question

  • I m using wininet to upload file to a server.

    The function function InternetWriteFile returns 1.

    I m using the Http method .

    I have a silly doubt. I am just giving the IP address of the HTTP server in the InternetConnect(). How can I know where this file was uploaded?

    Wednesday, October 06, 2010 6:45 AM

Answers

  • I m using wininet to upload file to a server.

    The function function InternetWriteFile returns 1.

    I m using the Http method .

    I have a silly doubt. I am just giving the IP address of the HTTP server in the InternetConnect(). How can I know where this file was uploaded?


    The return value of InternetWriteFile is BOOL not int.

    Returns TRUE when the function Writes data to an open Internet file successfully, FALSE otherwise. To get extended error information, call GetLastError. An application can also use InternetGetLastResponseInfo when necessary.

     

     


    Knowledge is like light; It spreads only when you have clear and transparent mind. - Sreedhar
    • Marked as answer by Sheryl_23 Thursday, October 07, 2010 6:51 AM
    Wednesday, October 06, 2010 6:54 AM
  • I m using wininet to upload file to a server.

    The function function InternetWriteFile returns 1.

    I m using the Http method .

    I have a silly doubt. I am just giving the IP address of the HTTP server in the InternetConnect(). How can I know where this file was uploaded?

    Returning 1 ( TRUE) means it was - purportedly - accepted by the HTTP server.
    Strictly speaking you don't know where it went, only that a webserver (or webserver proxy) responding on that IP took charge of it.

    You can do better if you use HTTPS and require that the server has a valid certificate with a name you trust.


    Answering policy: see profile.
    • Marked as answer by Sheryl_23 Thursday, October 07, 2010 6:50 AM
    Wednesday, October 06, 2010 11:34 AM

All replies

  • I m using wininet to upload file to a server.

    The function function InternetWriteFile returns 1.

    I m using the Http method .

    I have a silly doubt. I am just giving the IP address of the HTTP server in the InternetConnect(). How can I know where this file was uploaded?


    The return value of InternetWriteFile is BOOL not int.

    Returns TRUE when the function Writes data to an open Internet file successfully, FALSE otherwise. To get extended error information, call GetLastError. An application can also use InternetGetLastResponseInfo when necessary.

     

     


    Knowledge is like light; It spreads only when you have clear and transparent mind. - Sreedhar
    • Marked as answer by Sheryl_23 Thursday, October 07, 2010 6:51 AM
    Wednesday, October 06, 2010 6:54 AM
  • I have assigned the return value to a BOOL value. But it shows the value 1.
    Wednesday, October 06, 2010 7:12 AM
  • I have assigned the return value to a BOOL value. But it shows the value 1.


    Because BOOL is typedefed as int in WinDef.h  typedef int BOOL;

     Did you check InternetGetLastResponseInfo?

    Post your code to get clear responses. Remember silly and half completed queries, will not get proper answers.

    Wednesday, October 06, 2010 7:40 AM
  • Here is the code snippet:
    hOpen = InternetOpen(TEXT("TestApp"),dwAccessType,NULL,NULL,0);
    
    hConnect = InternetConnect(hOpen,L"xxx.xx.xx.xx",INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    
    HINTERNET hRequest = HttpOpenRequest (hConnect, L"PUT",L"test.txt", NULL, NULL, NULL, 0, 0);
    
    if (!hRequest)
    {
    printf("Failed to open request handle: %lu\n", GetLastError ());
    return FALSE;
    }
    HANDLE hFile = CreateFile (L"F:\\test.txt", GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    
    if (hFile == INVALID_HANDLE_VALUE)
    {
    printf("\nFailed to open local file %s.", "F:\\test.txt");
    return FALSE;
    }
      ZeroMemory(&BufferIn, sizeof(INTERNET_BUFFERS));   
      BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);  
      BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);   
    	
    printf ("File size is %d\n", BufferIn.dwBufferTotal );
    
    if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
    {
    

    m_dwError = GetLastError();

     

    if (m_dwError==ERROR_INTERNET_EXTENDED_ERROR)

    {

    DWORD dwErr;

    DWORD len =

    sizeof(m_errInfo)/sizeof(m_errInfo[0]);

    InternetGetLastResponseInfo(&dwErr, m_errInfo, &len);

    }

    } DWORD sum = 0; do { if (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),&dwBytesRead,NULL))) { break; } if ((bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,&dwBytesWritten))==FALSE) { break; } sum += dwBytesWritten; } while (sum < BufferIn.dwBufferTotal) ; CloseHandle (hFile); printf ("Actual written bytes: %d\n", sum); if(!HttpEndRequest(hRequest, NULL, HSR_ASYNC, 0)) { if (hFile) { CloseHandle (hFile); hFile = NULL; } if (hRequest) { InternetCloseHandle (hRequest); hRequest = NULL; } return FALSE; }
    Wednesday, October 06, 2010 8:57 AM
  • If I m not wrong ,I think we specify the url containing the page where the receiving action is to be done ,in the InternetOpen function itself?

    Wednesday, October 06, 2010 11:15 AM
  • I m using wininet to upload file to a server.

    The function function InternetWriteFile returns 1.

    I m using the Http method .

    I have a silly doubt. I am just giving the IP address of the HTTP server in the InternetConnect(). How can I know where this file was uploaded?

    Returning 1 ( TRUE) means it was - purportedly - accepted by the HTTP server.
    Strictly speaking you don't know where it went, only that a webserver (or webserver proxy) responding on that IP took charge of it.

    You can do better if you use HTTPS and require that the server has a valid certificate with a name you trust.


    Answering policy: see profile.
    • Marked as answer by Sheryl_23 Thursday, October 07, 2010 6:50 AM
    Wednesday, October 06, 2010 11:34 AM
  • My doubt is 'If I havent specified the page which does the receiving action anywhere in the code,How could InternetWriteFile return 1?'

    I have just given the IP address in the InternetConnect function.

    Please also tell me where to specify the page name which does the receiving action.

    Suppose the actual url to the page is something like http://xxx.xx.xx.xx/Somename/actionpage.do ,If I give the url as such in the InternetConnect function it returns 12006(Invalid Url) error code.

    Wednesday, October 06, 2010 11:47 AM
  • I also tried giving page name as the third parameter in HttpOpenRequest...but not working
    Wednesday, October 06, 2010 12:34 PM
  • You are performing a PUT method, so it should go where you tell it to put it in the Request-URI, in your case "test.txt".  This is a relative URI, and therefore probably invalid for PUT unless heuristics are applied by the server. 

    You can control the location of the resource you are PUTting by specifying a valid abs_path value instead, e.g. /foo/bar/test.txt


    Answering policy: see profile.
    Wednesday, October 06, 2010 3:50 PM
  • Does a JBOSS server support HTTP -PUT method?
    Thursday, October 07, 2010 3:59 AM
  • Does a JBOSS server support HTTP -PUT method?

    It can - it depends on the configuration of the specific server.
    Answering policy: see profile.
    Thursday, October 07, 2010 8:56 AM
  • I am thinking of using the POST method as of now,since there is actually a java page on server which performs the upload action.

    Considering this is the url of the page which does the action at the receiving side,

     http://xxx.xx.xx.xx/Somename/actionpage.do

    I have written the code for HTTP POST method as below:

    HINTERNET hRequest = HttpOpenRequest (hConnect, L

    "POST",L"/Somename/actionpage.do ",HTTP_VERSION, NULL, NULL, 0, 0);

    and in the InternetConnect function I just gave the IP address as below:

    hConnect = InternetConnect(hOpen,L

    "xxx.xx.xx.xx",INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);

    All other code remain the same.I think I have written it correctly..But all I could see now too is InternetWriteFile return 1.

    plz tell me how to fetch the response codes....

    Thursday, October 07, 2010 10:54 AM
  • Have a look at HttpQueryInfo Function - call it after you have sent the request.
    Answering policy: see profile.
    Thursday, October 07, 2010 11:05 AM
  • Dont know whether written correct.

    However,if I give a code like this :

    char

    data[1000];

    DWORD dwresstatus=1000;

    DWORD dwindex=0;

     

    int resstatus=HttpQueryInfo(hRequest,HTTP_QUERY_STATUS_CODE,data,&dwresstatus,&dwindex);

    It returns 0. Here HRequest is the handle returned by HttpOpenRequest

    Thursday, October 07, 2010 11:40 AM
  • One of these days I'm going to write an article on what to do before posting on a forum when a windows function returns a failure status ...

    In the meantime: it's important that this function is called after the request has completed but before the handle is closed.  Immediately after a successful HttpEndRequest() call is probably best for you.


    Answering policy: see profile.
    Thursday, October 07, 2010 3:46 PM
  • I have given HttpQueryInfo after a HttpEndRequest.And it is returns status as 500.

     

    Monday, October 11, 2010 6:53 AM
  • HTTP status code 500 is bad news - it means Internal Server Error. It might be something wrong with your request, or it might mean something happened which your request has no influence over. You need to talk to the administrator of the web server to find out what went wrong.
    Answering policy: see profile.
    Monday, October 11, 2010 8:13 AM
  • Few more doubts..

    File I want to upload is a .txt file . So what should be the request  header  added ?Plz  give me a code snippet. Maybe I went wrong here..

    Here is what I have given.

     if(!HttpAddRequestHeaders(hRequest,L"Content-Type: application/x-www-form-urlencoded",-1,HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD)) {

    exit(-1);

    }

     

     

    if(!HttpAddRequestHeaders(hRequest,L"Content-length: 63",-1,HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD))

    {

    exit(-1);

    }

    Monday, October 11, 2010 8:53 AM
  • if(!HttpAddRequestHeaders(hRequest,L"Content-length: 63",-1,HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD))

    Is there any particular reason why you are setting the Content-Length: header this way?  This is almost certainly incorrect.  Leave the calculation of the content length to HttpSendRequest, making sure you pass the correct values for lpOptional and dwOptionalLength .


    Answering policy: see profile.
    Monday, October 11, 2010 5:34 PM
  • I have given HttpSendRequest like this now:

    HttpSendRequest( hRequest, L

    "Content-Type: application/x-www-form-urlencoded",-1L,L"F:\\test.txt",GetFileSize (hFile, NULL) )

    I had earlier used HttpSendRequestEx . So if headers are given through HttpSendRequest,Is there a need to give HttpAddRequestHeaders too?

    I have given the filename of the file to be uploaded in the  lpOptional parameter...But I m getting error 10035 and http response still as 500 after the HttpEndRequest function

    Tuesday, October 12, 2010 5:39 AM
  • I have given HttpSendRequest like this now:

    HttpSendRequest( hRequest, L

    "Content-Type: application/x-www-form-urlencoded",-1L,L"F:\\test.txt",GetFileSize (hFile, NULL) )

    The 4th argument should be a pointer to a buffer containing the content in the appropriate form for the Content-Type.  You have passed it the name of a file, and then told it that the length of the buffer is the length of file associated with hFile, assuming GetFileSize() didn't return an error.

    I had earlier used HttpSendRequestEx . So if headers are given through HttpSendRequest,Is there a need to give HttpAddRequestHeaders too?

    Not unless you have more headers to add programmatically.

    I have given the filename of the file to be uploaded in the  lpOptional parameter...But I m getting error 10035 and http response still as 500 after the HttpEndRequest function


    System error 10035 is WSAEWOULDBLOCK, "A non-blocking socket operation could not be completed immediately."


    Answering policy: see profile.
    Tuesday, October 12, 2010 2:15 PM
  • The 4th argument should be a pointer to a buffer containing the content in the appropriate form for the Content-Type.  You have passed it the name of a file, and then told it that the length of the buffer is the length of file associated with hFile, assuming GetFileSize() didn't return an error.

    Does this mean I have given wrong inputs to the headers? I have changed MIME type to "text/plain"...

    Winsock error 10035- What are the possible programmatic mistakes that can cause this error?

    Tuesday, October 19, 2010 7:01 AM
  • The 4th argument should be a pointer to a buffer containing the content in the appropriate form for the Content-Type.  You have passed it the name of a file, and then told it that the length of the buffer is the length of file associated with hFile, assuming GetFileSize() didn't return an error.

    Does this mean I have given wrong inputs to the headers?

    Yes.
    Answering policy: see profile.
    Tuesday, October 19, 2010 12:38 PM
  • I am getting http response code as 200 but InternetWriteFile returns false with error  code 12019..I gave

     INTERNET_FLAG_NO_CACHE_WRITE in HttpOpenRequest as suggested in msdn,but no use...

    The headers added is now like this:

    ZeroMemory(&BufferIn, sizeof(INTERNET_BUFFERS)); 
    BufferIn.dwBufferTotal = GetFileSize (hFile, NULL); 
    BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS); 
     if(!HttpSendRequest( hRequest, L"Content-Type: text/plain",-1L,&BufferIn,BufferIn.dwBufferTotal ))
    

    The execution breaks when it comes InternetCloseHandle function.

     

     

    Thursday, October 21, 2010 9:27 AM
  • I think you are not giving all the information needed.

    Status 200 is a GOOD response, indicating that the request worked.

    However, Error code 12019 is

    ERROR_INTERNET_INCORRECT_HANDLE_STATE,

    "The requested operation cannot be carried out because the handle supplied is not in the correct state."

    (See WinInet Error Messages )

    Please ensure that you are reporting only what is actually happening in the case you are stuck on.


    Answering policy: see profile.
    Thursday, October 21, 2010 3:23 PM
  • I am in confusion...

    if(HttpEndRequest(hRequest,NULL,HSR_INITIATE,0))

    {

    m_dwError=GetLastError();

    int resstatus=HttpQueryInfo(hRequest,HTTP_QUERY_STATUS_CODE,data,&dwresstatus,&dwindex);

    ----

    }

    GetLastError() gives 2 and HttpQueryInfo shows a status of 200. I m not getting error 12019 now...

    • Edited by Sheryl_23 Monday, October 25, 2010 5:17 AM
    Friday, October 22, 2010 11:00 AM
  • I am in confusion..

    if

     

    {

    m_dwError = GetLastError();

    (HttpEndRequest(hRequest, NULL,HSR_INITIATE, 0)) 

    int resstatus1=HttpQueryInfo(hRequest,HTTP_QUERY_STATUS_CODE,data,&dwresstatus,&dwindex);

    MessageBox(NULL,(LPCWSTR)data,L

    "status",MB_OK);

    -----

    ----

    }

    Here , GetLastError() returns 2 and HttpQueryInfo shows a status of 200. I m not getting 12019 now..


    It looks like something strange happened to the code on the way to the forum! Could you please ensure that it's clear which function call GetLastError() relates to?
    Answering policy: see profile.
    Friday, October 22, 2010 12:43 PM
  • I have edited the code in old reply itself...
    Monday, October 25, 2010 5:19 AM
  • You are calling GetLastError() after HttpEndRequest() has returned TRUE (i.e. success), which makes any value returned by GetLastError() irrelevant.


    Answering policy: see profile.
    Monday, October 25, 2010 8:39 AM
  • I havent specified filename in the headers,so the file is not created at server...

    Suppose  my file name is mytest.txt,have i written( the one given below) the correct way of specifying the filename in the headers?

    if

    (!HttpAddRequestHeaders(hRequest,L"filename:\"mytest.txt\"",-1L,HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD))

     

    I think it is because of the "\" used for specifying file name,that the server is showing "the filename, directory name, or volume label syntax is incorrect" error.

    How can I prevent this? The filename has to be specified in quotes.

    Tuesday, October 26, 2010 11:03 AM
  • Filename: isn't a recognised HTTP request header.  I suspect you are thinking of the Content-Disposition: header with the filename= attribute.

    It may be possible directly to POST the content of a file to a web server without pretending it's part of a form submission, but if so I don't know what it is.  Therefore, I will try to help you set it up as a form submission file transfer (i.e. as if you had pressed an 'Upload file' button on a web page.)

    This involves adding a single line to the request headers, and also requires additional work in formatting the content suitable for the MIME-type you use for the file's content.  Sorry to have to send you down this particular route, as it involves getting to grips with some more technology.

    This is based on the relevant part of the HTML spec: 17.13.4 Form content types 

    You need to be constructing valid multipart/form-data content and use the header

       Content-Type: multipart/form-data; boundary=foo

    where foo is some string which is guaranteed not to appear in the file content.  In the example below, it has the value AaB03x . 

      Content-Type: multipart/form-data; boundary=AaB03x
    

    You specify this header using HttpAddRequestHeaders(), but none of the rest of what follows.  That's the header; now for the content, which is a MIME body-part according to RFC 2045 and RFC 2046.

      --AaB03x
      Content-Disposition: form-data; name="files"; filename="file1.bin"
      Content-Type: application/octet-stream
    Content-Transfer-Encoding: binary
    ... contents of file1.bin ... --AaB03x--

    There are several important points to note:

    • The blank line after Content-Transfer-Encoding: is important, and must consist of two carriage-return,line-feed pairs.
    • Pay particular attention to the additional pairs of hyphens on the first and last lines.
    • Replace the line ... contents of file1.bin ... with the content of the file you are sending. This must be sent exactly as it exists on the disk, so open the file in Binary mode.

    The filename= value will be whatever you need it to be on the server.  The name="files" attribute may or may not be signficant to your web server, but something dshould be provided here; it's the name of the HTML form field/control associated with the uploaded file.


    Answering policy: see profile.
    Tuesday, October 26, 2010 4:47 PM