Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Upload file using wininet in c++.

128 views
Skip to first unread message

Azeem Haider

unread,
Apr 3, 2015, 9:00:53 AM4/3/15
to
I'm going to upload text file using c++. I'm using this code but this is not working and there is no error in this code. can you give me suggestion why this is not working?
Here is code.

#include "stdafx.h"
#include "iostream"
#include "windows.h"
#include "wininet.h"
#include "tchar.h"
#include "iostream"
#include "string"
#include "sstream"

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "Wininet.lib")

using namespace std;
// Wininet.lib
// ws2_32.lib
int main(){

TCHAR hdrs[] = _T("Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858");
TCHAR frmdata[] = _T("-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents here\r\n-----------------------------7d82751e2bc0858--\r\n");
//char frmdata[] = "...";

LPCTSTR accept[] = {_T("*/*"), NULL};

HINTERNET hSession = InternetOpen(_T("MyAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnect = InternetConnect(hSession, _T("localhost"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"), _T("cpp/uploadfile.php"), NULL, NULL, accept, 0, 1);
BOOL sent = HttpSendRequest(hRequest, hdrs, _tcslen(hdrs), frmdata, _tcslen(frmdata));
//BOOL sent = HttpSendRequest(hRequest, hdrs, _tcslen(hdrs), frmdata, strlen(frmdata));

if(hSession==NULL)
{
cout<<"Error: InternetOpen";
}
if(hConnect==NULL)
{
cout<<"Error: InternetConnect";
}
if(hRequest==NULL)
{
cout<<"Error: HttpOpenRequest";
}
if(!sent)
{
cout<<"Error: HttpSendRequest";
}

DWORD status;
DWORD statusLength = sizeof(status);
HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &statusLength, nullptr);
if (status != HTTP_STATUS_OK) {
cout << "HTTP status: " << status << endl;
}

//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);


return 0;

}

Here is php file code.

<?php
move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "/FileFolder/");
?>
Thanks for your time.

Victor Bazarov

unread,
Apr 3, 2015, 9:07:55 AM4/3/15
to
On 4/3/2015 9:00 AM, Azeem Haider wrote:
> I'm going to upload text file using c++. I'm using this code but this is not working and there is no error in this code. can you give me suggestion why this is not working?
> Here is code.
>
> [...]

Very little of that code is standard C++. Most of it is
Windows-specific, some is VC++ specific. As such they ought to be
discussed in the forums dedicated to them (please google for a newsgroup
or a web site). Besides, you don't even say what it is "not working",
how can we even begin to help you?

So, no, sorry, I cannot give you a suggestion *why* it's not working,
but here is my suggestion: find a better place to ask OS-specific and
compiler-specific questions.

V
--
I do not respond to top-posted replies, please don't ask

Melzzzzz

unread,
Apr 3, 2015, 10:55:01 PM4/3/15
to
On Fri, 3 Apr 2015 06:00:24 -0700 (PDT)
Azeem Haider <abclogi...@gmail.com> wrote:

> I'm going to upload text file using c++. I'm using this code but this
> is not working and there is no error in this code. can you give me
> suggestion why this is not working? Here is code.

I guess you have to read file and send it, no?

0 new messages