HttpQueryInfo報錯,該怎麼處理

來源:互聯網
上載者:User
HttpQueryInfo報錯
file1.php
PHP code
    $file = "D:\\ReadFileTo.txt";    $handle = fopen($file, 'r');    $sContent = '';    while(false != ($a = fread($handle, 8080))){//返回false表示已經讀取到檔案末尾        $sContent .= $a;    }        fclose($handle);    echo "$sContent";


vc(輸出file1.php):
C/C++ code
#include "stdafx.h"#include #include #include  #include  #include  #pragma comment(lib,"wininet.lib")  #define DATA_CACHE_SIZE 1024*10int main(int argc, char* argv[]){    printf("Hello World!\n");    int i = 0;    char pulr[] = "file1.php";    HINTERNET hinternet=::InternetOpen(                 "http generic",                 INTERNET_OPEN_TYPE_PRECONFIG,                 NULL,                     NULL,                 0);    if (!hinternet)    {        printf("InternetOpen\n");        system("pause");        return 0;    }    i = 1;    HINTERNET hconnect= ::InternetConnect(                          hinternet,                          "144.20.80.27",                          8080,                          NULL,                                //lpszUsername                          NULL,                                //lpszPassword                          INTERNET_SERVICE_HTTP,                          0,                                //dwFlags                          0                                    //dwContext                          );    if (!hconnect)    {        printf("InternetConnect\n");        system("pause");        return 0;    }            HINTERNET hHttpOpenRequest=::HttpOpenRequest(                        hconnect,                        "POST",                        pulr,                        "HTTP/1.1",                        NULL,                        0,                        INTERNET_FLAG_RELOAD,                        0            );    if (!hHttpOpenRequest)    {        printf("HttpOpenRequest\n");        system("pause");        return 0;    }    char psHeader[] = "Content-Type: application/x-www-form-urlencoded \n\r";    if (!(HttpAddRequestHeaders(        hHttpOpenRequest,         psHeader,         strlen(psHeader),         HTTP_ADDREQ_FLAG_REPLACE|HTTP_ADDREQ_FLAG_ADD        )) )    {        printf("HttpAddRequestHeaders\n");        system("pause");        return 0;    }    char *strRet=""; //"Yjr|fc|zf|cym|wjf||role1|role2|role3|role4";    char pstrContent[] = "UserName=admin&Password=-1";    if (! (HttpSendRequest(        hHttpOpenRequest,        NULL,        0,        (LPVOID)(LPCSTR)pstrContent,        strlen(pstrContent)                    )) )    {        printf("HttpSendRequest\n");        system("pause");        return 0;    }    // Get the length of the file.                DWORD dwFileSize=0;    DWORD dwLengthBufQuery = sizeof(dwFileSize);    DWORD dwIndex=0;        BOOL bQuery = ::HttpQueryInfo(hHttpOpenRequest,        HTTP_QUERY_CONTENT_LENGTH        |HTTP_QUERY_FLAG_NUMBER,         (LPVOID)&dwFileSize/*bufQuery*/,         &dwLengthBufQuery,        &dwIndex) ;    if (!bQuery)    {        printf("HttpQueryInfo\n");        system("pause");        return 0;    }    // Allocate a buffer for the file.       char* buffer = new char[dwFileSize+1] ;            //由於不允許HTTP寫緩衝,所以無需迴圈調用InternetReadFile    DWORD dwLength=0;    DWORD dwLengthOne=1;    BOOL bRead=TRUE;    char bufferOne[DATA_CACHE_SIZE+10];    strcpy(buffer,"");    while (bRead && dwLengthOne>0)    {        strcpy(bufferOne,"");        if (InternetReadFile(                hHttpOpenRequest,                bufferOne,                DATA_CACHE_SIZE+1,                &dwLengthOne                )            )         {            if (dwLengthOne>0)            {                memcpy(&buffer[dwLength],bufferOne,dwLengthOne);                dwLength+=dwLengthOne;                //接收資料包的大小和與總資料包大小比較                if (dwLength > dwFileSize)                {                    printf("while\n");                    system("pause");                    return 0;                }                buffer[dwLength]='\0';            }        }    }    buffer[dwLength]='\0';    //to widerchar    DWORD dwSize = MultiByteToWideChar (CP_ACP, 0, buffer, -1, NULL, 0);      WCHAR *wchr = new WCHAR [dwSize];    ::MultiByteToWideChar(CP_ACP,MB_ERR_INVALID_CHARS,buffer,-1,                 wchr,dwSize);    printf(buffer);//    wprintf(wchr);    delete [] wchr;    delete [] buffer;            ::InternetCloseHandle(hHttpOpenRequest);hHttpOpenRequest=NULL;    ::InternetCloseHandle(hconnect);hconnect=NULL;    ::InternetCloseHandle(hinternet);hinternet=NULL;    printf("\n");    system("pause");    return 0;}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.