Click here to Skip to main content
15,440,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have problems with below code:
C++
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,
Posted
Updated 17-Sep-11 7:16am
v2
Comments
mbue 18-Sep-11 14:25pm     CRLF
Please, please use "improve question" or "have a question or comment" links to do them, why?: to enshure the answerer gets a response mail. Or for a better understanding of your question. Therefor you should never post a statement as solution. Have a nice time on cp. Regards.
Ali Fakoor 19-Sep-11 1:22am     CRLF
I see an error [may be typo] in the code regarding InternetConnect. You say you are connecting to https (normally port 443) in the line's comment , while actually you entered 80 (http port) and also mentioned service type of http. Collapse | Copy Code 80, // HTTPS port ... INTERNET_SERVICE_HTTP, // Service Moreover, you specified a read access type while creating the file (CreateFile) which also seems to be an error: GENERIC_READ, //Desired Access

1 solution

Im afraid your expectations cannot be fullfilled by HTTP protocol.
You cannot create or direct write any file with HTTP. You can write the post data to the servers request site and the server can do something with that data (possible create a real file - upload on some servers, the server decides where to store the data). HTTP has no directory service the directory is hidden for the client. Sometimes a database based site layout.
If you want to upload files (create, delete, enumerate), you should use FTP protocol.
Regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900