0

I want to upload "D:\er.txt" to webserver using HTTP, when I am running program, i got HttpSendRequest 12005 as an error. i used a PHP script on my webserver that will accept the file and stores it in a pre-made directory named as "upload".. here is my tiny program

int main()
{
    static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"D:\\er.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents  here\r\n-----------------------------7d82751e2bc0858--\r\n";
    static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858";

    HINTERNET hSession = InternetOpen("MyAgent",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    if(!hSession)
    {
        cout<<"Error: InternetOpen";
    }


    HINTERNET hConnect = InternetConnect(hSession, _T("http://jobnews.netii.net/upload.php"),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
    if(!hConnect)
    {
        cout<<"Error: InternetConnect";
    }

    HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T("upload.php"), NULL, NULL, (const char**)"*/*\0", 0, 1);
    if(hRequest==NULL)
    {
        cout<<"Error: HttpOpenRequest";
    }

    BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
    if(!sent)
    {
        cout<<"Error: HttpSendRequest "<<GetLastError();
    }


    //close any valid internet-handles
    InternetCloseHandle(hSession);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hRequest);
    getchar();
    return 0;
}

and my PHP script is

<?php
$uploaddir = 'upload/'; // Relative Upload Location of data file

if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {
$uploadfile = $uploaddir . basename($_FILES['uploadedfile']['name']);
echo "File ". $_FILES['uploadedfile']['name'] ." uploaded successfully. ";
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully moved. ";
}

else
print_r($_FILES);
}

else {
echo "Upload Failed!!!";
print_r($_FILES);
}
?>
3
  • is this some sort of assignment? stackoverflow.com/questions/1985345/… Commented Aug 19, 2013 at 6:58
  • no no.. not at all. i am a beginner in development field, and i am try to understand the concepts by going through some working examples. this is just for study purpose only Commented Aug 19, 2013 at 7:03
  • The error message you quote is saying "invalid URL". If what other people are saying about mis-use of string references is correct, that would make sense. Commented Aug 19, 2013 at 7:17

1 Answer 1

1

According to the docs for HttpOpenRequest, the lplpszAcceptTypes argument should change from

(const char**)"*/*\0"

to

{_T("*/*"), NULL}

You can also remove the \0 from the end of the string. You don't need to manually insert a nul terminator to a string literal.

In other words, you need to change

HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",
                                     _T("upload.php"), NULL, NULL,
                                     (const char**)"*/*\0", 0, 1);

to

LPCTSTR rgpszAcceptTypes[] = {_T("*/*"), NULL};
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",
                                     _T("upload.php"), NULL, NULL,
                                     rgpszAcceptTypes, 0, 1);
Sign up to request clarification or add additional context in comments.

5 Comments

@user2682006 He's saying pass a LPCTSTR* like the documentation he linked indicates is required. Your cast is not only incorrect, it is invalid. LPCTSTR arAccentTypes[] = {_T("*/*"), NULL};. Pass that (by-value, no indirection, casts, etc. required).
@user2682006 I've updated my answer to be more explicit about the problem.
sir i am using DEV C++ and after putting your lines in it, it's showing `PCTSTR' undeclared (first use this function) error
I might have copied from msdn too liberally, try using LPCTSTR instead. And see this previous post for some details on the two types.
after replacing some lines suggested by you, my program compiles successfully but on debugging it still showing the same error "HttpSendRequest 12005" as an error :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.