[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

easy pts (HTTP file upload)

Posted on 2000-05-11
19
Medium Priority
?
338 Views
Last Modified: 2013-11-20
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

 
0
Comment
Question by:rexona
  • 9
  • 8
  • 2
19 Comments
 
LVL 7

Expert Comment

by:tchalkov
ID: 2800138
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
0
 
LVL 2

Expert Comment

by:sun307
ID: 2800170
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--
0
 

Author Comment

by:rexona
ID: 2800241
--- Hi !
me and sun307 both are working on the same app. so pls accept his comments as equivalent to mine.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 7

Accepted Solution

by:
tchalkov earned 4000 total points
ID: 2800471
Here is a simple example how to do what you want .
Theree are two functions - with and without a proxy - you should change the name of the proxy with yours.
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.

So this code assumes then anonymous user has write access to root folder in the ftp server.
If the user is not anonymous then you should change the username and password(the two NULLs in InternetConnect)

Also you may want to change the folder where the file is uploaded - you can do this with the FtpSetCurrentDirectory functions - it is an easy one - but if you need help about it i'll help you with another example. Remember that this folder must have write access.

The error handling is removed for simplicity, but you should check the return value from every function call.

If you have any questions - just call.

#include "stdafx.h"
#include "stdafx.h"
#include "wininet.h"


void noproxyftp(void)
{
      HINTERNET hi=InternetOpen("MyApplication",INTERNET_OPEN_TYPE_DIRECT,
                                                NULL,NULL,0);
      HINTERNET conn=InternetConnect(hi,"212.50.4.197",INTERNET_DEFAULT_FTP_PORT,
                              NULL,NULL,INTERNET_SERVICE_FTP,0,0);
      FtpPutFile(conn,"e:\\sys.mdb","sys.mdb",FTP_TRANSFER_TYPE_BINARY,0);
      InternetCloseHandle(conn);
      InternetCloseHandle(hi);
}

void proxyftp(void)
{
      HINTERNET hi=InternetOpen("MyApplication",INTERNET_OPEN_TYPE_PROXY ,
                                                "192.168.1.60",NULL,0);
      HINTERNET conn=InternetConnect(hi,"212.50.4.197",INTERNET_DEFAULT_FTP_PORT,
                              NULL,NULL,INTERNET_SERVICE_FTP,0,0);
      FtpPutFile(conn,"e:\\sys.mdb","sys.mdb",FTP_TRANSFER_TYPE_BINARY,0);
      InternetCloseHandle(conn);
      InternetCloseHandle(hi);
}

int main(int argc, char* argv[])
{
      proxyftp();
      return 0;
}
0
 
LVL 7

Expert Comment

by:tchalkov
ID: 2800489
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
0
 

Author Comment

by:rexona
ID: 2803353
>>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...
0
 
LVL 7

Expert Comment

by:tchalkov
ID: 2803485
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.
0
 

Author Comment

by:rexona
ID: 2803711
>>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...
0
 
LVL 7

Expert Comment

by:tchalkov
ID: 2813451
Rexona,
do you need more help?
do you have any problems?
0
 

Author Comment

by:rexona
ID: 2813806
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
0
 
LVL 7

Expert Comment

by:tchalkov
ID: 2816773
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).



0
 
LVL 2

Expert Comment

by:sun307
ID: 2817747
>>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

0
 
LVL 7

Expert Comment

by:tchalkov
ID: 2820002
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;
}
0
 

Author Comment

by:rexona
ID: 2952071
Answer accepted
0
 

Author Comment

by:rexona
ID: 2952072
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
0
 
LVL 7

Expert Comment

by:tchalkov
ID: 2952846
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.
0
 

Author Comment

by:rexona
ID: 2953031
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
0
 
LVL 7

Expert Comment

by:tchalkov
ID: 2960432
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.
0
 

Author Comment

by:rexona
ID: 2987775
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.

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question