Link to home
Start Free TrialLog in
Avatar of rexona
rexona

asked on

easy pts (HTTP file upload)

Hello Experts !
i am developing an app. which will monitor the user's surfing habits and record the info. in a text file say USER.TXT
i want that my app. should be able to upload this file periodically to a web server in the background. as i have no experience in this area i am expecting some  code which can do this thing (i.e. upload this file using HTTP protocol).
i had already seen previously posted question in this area and had gone through various links and all. but they seems to be very complex keeping in mind this small functionality i need. so please avoid posting links.

if the code is good and runs, i will not mind increase the points too.

Thanks
Rexona

 
Avatar of tchalkov
tchalkov

the easiest way to do this is to use FTP not HTTP.
There are a couple of functions called the WinInet API.
You can use them from VBasic and Visual C++ and what you want to do is as easy as few lines of code.

If you tell me what language do you use I'll give you a working example.
Here is a short description of what you shoud do:

1. Create a folder on the FTP server which has permission for some account to create files. Give permissions to create files, not to read files

2. create a subroutine in your program which does the following:

call InternetOpen function to initialize the WinInet api.
then call InternetConnect to connect to your ftp server.
then call FtpPutFile to upload your file.
then close the handle you recieved.
that's all

so tell me if you like this approach and what example do you need - VC++ or VB
thanx tchalkov !
my main aim is just to upload the files no matter by what method. also, if through ftp then my app. should work even if a person is accessing the web through a proxy server ( means if the person is on LAN and accesses the web through proxy).
i will be happy if you send me an working sample in VC++. either u can post it here or if the sample is large then u can send me at techieguy77@yahoo.com
--Rexona--
Avatar of rexona

ASKER

--- Hi !
me and sun307 both are working on the same app. so pls accept his comments as equivalent to mine.
ASKER CERTIFIED SOLUTION
Avatar of tchalkov
tchalkov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forgot to tell you that this examples uploads the fiole e:\sys.mdb into sys.mdb in the root folder of the ftp server.
The ftp server can be referenced with dns name as well.

don't try the address i used - it will not work - try it with your ftp server.

So before running the example - customize it
Avatar of rexona

ASKER

>>Also there is an option which allows InternetOpen to read the proxy settings from the configuration of IE.

>>Generally if internet explorer is installed then these functions work with more proxies.

can u please send some code related to that too like how to get those proxy settings from IE.

-thanks and regards
Rexona...
in the noproxy function change the INTERNET_OPEN_TYPE_DIRECT to INTERNET_OPEN_TYPE_PRECONFIG
and don't specify a proxy server name.
This will  read the proxy configuration from the registry.

Or you can change it with INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY - this way it is not trying to autodetect the proxy.
Avatar of rexona

ASKER

>>Also there is an option which allows InternetOpen to read the proxy settings from the configuration of IE.

>>Generally if internet explorer is installed then these functions work with more proxies.

can u please send some code related to that too like how to get those proxy settings from IE.

-thanks and regards
Rexona...
Rexona,
do you need more help?
do you have any problems?
Avatar of rexona

ASKER

Sorry Tchalkov !
for not posting any comment here for so long..
Your code is working fine when run directly from a computer without proxy.

but with proxy it's giving some trouble:
might be our proxy server is not configured for doing ftp.

so. i want to know from u if i change the 3 rd parameter in InternetConnect to INTERNET_DEFAULT_HTTP_PORT
and 6th parameter to INTERNET_SERVICE_HTTP, then what will be it's impact on the program.

will it uses the HTTP port 80 in this case or ftp port 21.

plus which parameter from below do u think will be more suitable in InternetOpen() considering the wide range of users using the app.

INTERNET_OPEN_TYPE_PRECONFIG or INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY

Also, if i set one of the above options in my app. and a user is not using proxy server (i.e. using direct connection) then will he be able to upload the file or i have to first determine whether the user is using proxy or not ???

-sorry for troubling u so much... i will increase the points too which i think u deserves.

-thanx and regards
Rexona
INTERNET_DEFAULT_HTTP_PORT & INTERNET_SERVICE_HTTP - if you use this options then you tell WinInet api that you are using HTTP protocol (both the protocl and the 80 port) - i'm not quite familiar with uploading files using HTTP but i can check this if you want.


I don't know more about INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY  - only what is stated in MSDN - that this does not execute startup JScript or internetnet setup files(*.ins).

if the user has direct connection and the INTERNET_OPEN_TYPE_PRECONFIG option is used then if he is able to access ftp site from internet explorer your code will work as well (even if he has direct connection it is still possible that he has configured som proxy in IE - then you'll code will use that proxy).



>>INTERNET_DEFAULT_HTTP_PORT & INTERNET_SERVICE_HTTP - if you use this options then you tell WinInet api that you are using HTTP protocol (both the protocl and the 80 port) - i'm not quite familiar with uploading files using HTTP but i can check this if you want.

I will be glad if you can checkup this and send me the updated code. I know that i am trubling u a lot. sorry for that. but the problem is that our proxy server here is not configured well.

-Thanks and Regards
Rexona

Here is a code I used from MSDN - I strongly suggest to read KB article ID: Q184352 - I took the code from there and there are also discussed possible problems. This tries direct connection - you will have to modify the code to try connection trhough proxy. Try it and let me know if it works.

To run this just make a console application in VC++ and run it.

// Code starts
// inetapi.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Wininet.h"
#include "stdio.h"

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",
            "/page.htm", NULL, NULL, NULL,  0, 0);
      if (!hRequest)
      {
            printf("Failed to open request handle: %lu\n", GetLastError ());
            return FALSE;
      }
      
      HANDLE hFile = CreateFile (upFile, GENERIC_READ, FILE_SHARE_READ,
            NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
      if (hFile == INVALID_HANDLE_VALUE)
      {
            printf("\nFailed to open local file %s.", upFile);
            return FALSE;
      }
      
      BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
      printf ("File size is %d\n", BufferIn.dwBufferTotal );
      
      if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
      {
            printf( "Error on HttpSendRequestEx %lu\n",GetLastError() );
            return FALSE;
      }
      
      DWORD sum = 0;
      do
      {
            if  (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
                  &dwBytesRead, NULL)))
            {
                  printf ("\nReadFile failed on buffer %lu.",GetLastError());
                  break;
            }
            if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
                  &dwBytesWritten)))
            {
                  printf ("\nInternetWriteFile failed %lu", GetLastError());
                  break;
            }
            sum += dwBytesWritten;
      }
      while (dwBytesRead == sizeof(pBuffer)) ;
      
      CloseHandle (hFile);
      printf ("Actual written bytes: %d\n", sum);
      
      if(!HttpEndRequest(hRequest, NULL, 0, 0))
      {
            printf( "Error on HttpEndRequest %lu \n", GetLastError());
            return FALSE;
      }
      return TRUE;
}

int main(int argc, char* argv[])
{
      HINTERNET hi=InternetOpen("MyApplication",INTERNET_OPEN_TYPE_DIRECT,
            NULL,NULL,0);
      HINTERNET conn=InternetConnect(hi,"192.168.1.75",INTERNET_DEFAULT_HTTP_PORT,
            NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
      UseHttpSendReqEx(conn,"c:\\vlado\\rise.zip");
      InternetCloseHandle(conn);
      InternetCloseHandle(hi);
      return 0;
}
Avatar of rexona

ASKER

Answer accepted
Avatar of rexona

ASKER

Hi tchalkov !
thanx for ur great help
I am now using your FTP method to upload the file on the web server. I had tried HTTP too but it didn't work.
I am sorry too for taking so long in accepting ur answer.
Thanx again
-Rexona
Probably it did not work because the sample i gave you is configured for direct connection and must be configured to use a proxy.
Another possible problem is misconfiguration of your web server - permissions or something else.
Avatar of rexona

ASKER

i am again trying on that and would try to find the real problem behind. Had u tried that code and seen it working ? because then i would get a more clear picture of the problem.
-Thanks & Regards
Yes. I tried it and it worked. I used Windows 2000 Advanced Server and IIS 5.0
One of the problems I had was that I forgot to turn on the right to upload files.

After I fixed that it worked perfectly.
Avatar of rexona

ASKER

I too tried to figure out the problem and then found from the w3c.com site (from the specifications there) that one has to enable PUT functionality on their web server and also had to provide the path of a script file on their web server which handles the files. So, I guess your Web Server had all that configured beforehand. But, a lot of servers are not and have to be configured for PUT functionality, which was the case with our web server and thus the files were not getting uploaded. I will now try the HTTP POST method to upload the files.