HttpQueryInfo报错

jhldelphi 2012-06-28 04:31:08
file1.php
    $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):
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include<wininet.h>
#include<iostream.h>
#pragma comment(lib,"wininet.lib")

#define DATA_CACHE_SIZE 1024*10
int 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;
}


问题:
1、环境windows xp + php5 + apache2.2 + vc6.0。
2、当file1.php读取的文件大于8000kb时,输出就会失败。
...全文
112 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

不明了,怎么解决呢
[/Quote]

改用其他的php函数
jhldelphi 2012-06-28
  • 打赏
  • 举报
回复
不明了,怎么解决呢
  • 打赏
  • 举报
回复
fread() 从文件指针 file 读取最多 length 个字节。该函数在读取完最多 length 个字节数,或到达 EOF 的时候,或(对于网络流)当一个包可用时,或(在打开用户空间流之后)已读取了 8192个字节时就会停止读取文件,视乎先碰到哪种情况。
返回所读取的字符串,如果出错返回 false。

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧