若文件名含有中文,则从服务器收到以下 HTTP 状态消息:
404 File Not Found我参照http://support.microsoft.com/kb/930268/zh-cn解决
function TOaUp.HttpDownloadFile(const URL, strLocalFile: string): string;
const
Agent = 'Internet Explorer';
SEGMENT_LENGTH = 1024 * 64;
var
hFile, HInet: HINTERNET;
Buffer: array[0..SEGMENT_LENGTH] of Char;
BufRead: Cardinal;
BufSize: Cardinal;
ret: BOOL;
URL3: PChar; lpszBuffer: PChar;
lpdwBufferLength: DWORD; dwFlags: DWORD;
iFile: Integer;
begin If FileExists(strLocalFile) Then
begin
DeleteFile(strLocalFile);
Sleep(500);
end; iFile := FileCreate(strLocalFile); HInet := InternetOpen(PChar(Agent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(HInet) then
try
URL3:=PChar(URL);
lpdwBufferLength := INTERNET_MAX_URL_LENGTH;
ret:=InternetCanonicalizeUrl(URL3,lpszBuffer,lpdwBufferLength,dwFlags);
If ret Then
begin
ShowMessage(URL3+'ok'+lpszBuffer);
end;
hFile := InternetOpenUrl(HInet,lpszBuffer, nil, 0, INTERNET_FLAG_RELOAD, 0);
if Assigned(hFile) then
try
BufSize := SizeOf(Buffer);
with TStringStream.Create('') do
try
while InternetReadFile(hFile, @Buffer, BufSize, BufRead) and (BufRead > 0) do
begin
FileWrite(iFile, Buffer, BufRead);
end; finally
Free;
FileClose(iFile);
end; finally
InternetCloseHandle(hFile);
end; finally
InternetCloseHandle(hinet);
end;
end;
文中文件还是不行,并有一个错误提示:acess violation at address 6d657442f.read of address 6d65742f
404 File Not Found我参照http://support.microsoft.com/kb/930268/zh-cn解决
function TOaUp.HttpDownloadFile(const URL, strLocalFile: string): string;
const
Agent = 'Internet Explorer';
SEGMENT_LENGTH = 1024 * 64;
var
hFile, HInet: HINTERNET;
Buffer: array[0..SEGMENT_LENGTH] of Char;
BufRead: Cardinal;
BufSize: Cardinal;
ret: BOOL;
URL3: PChar; lpszBuffer: PChar;
lpdwBufferLength: DWORD; dwFlags: DWORD;
iFile: Integer;
begin If FileExists(strLocalFile) Then
begin
DeleteFile(strLocalFile);
Sleep(500);
end; iFile := FileCreate(strLocalFile); HInet := InternetOpen(PChar(Agent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(HInet) then
try
URL3:=PChar(URL);
lpdwBufferLength := INTERNET_MAX_URL_LENGTH;
ret:=InternetCanonicalizeUrl(URL3,lpszBuffer,lpdwBufferLength,dwFlags);
If ret Then
begin
ShowMessage(URL3+'ok'+lpszBuffer);
end;
hFile := InternetOpenUrl(HInet,lpszBuffer, nil, 0, INTERNET_FLAG_RELOAD, 0);
if Assigned(hFile) then
try
BufSize := SizeOf(Buffer);
with TStringStream.Create('') do
try
while InternetReadFile(hFile, @Buffer, BufSize, BufRead) and (BufRead > 0) do
begin
FileWrite(iFile, Buffer, BufRead);
end; finally
Free;
FileClose(iFile);
end; finally
InternetCloseHandle(hFile);
end; finally
InternetCloseHandle(hinet);
end;
end;
文中文件还是不行,并有一个错误提示:acess violation at address 6d657442f.read of address 6d65742f
http://webserver/考勤.doc 不行
InternetCanonicalizeUrl之后返回的内容是什么?这个我不清,我delphi没用过,看了帮助,参考vc例子,好像URL3-->lpszBufferint CHttpConnect::OpenUrl(WCHAR *Url ) // SOMETHING WRONG
{
//入参为要 OPEN 的 url BOOL ret = FALSE;
int res = 0;
WCHAR in_url[] = {0};
short out_url[512] = {0};
DWORD P_Length = 512 ; //把url修改为标准格式,包括转换一些不安全的字符
ret = InternetCanonicalizeUrl((LPCTSTR)Url,(LPWSTR)out_url,&P_Length,ICU_NO_META);
if(ret != TRUE)
{ujDebugPrint( (uj_char *)"zhangyepeng+++InternetCanonicalizeUrl++FALSE == ret" );
res = GetLastError();
return HTTP_ERROR;
}
//对于 WWW 服务器提供的资源可以直接通过 InternetOpenUrl 打开 Url,读取数据.而不用经过InternetConnect //Open_Url = InternetOpenUrl(Http_Open, (LPCWSTR)Url, NULL, 0,0, 0); Open_Url = InternetOpenUrl(Http_Open,(LPCWSTR)out_url, NULL, 0,INTERNET_FLAG_EXISTING_CONNECT,0); if(NULL == Open_Url)
{
res = GetLastError();
return HTTP_ERROR;
} return HTTP_OK;}
const
Agent = 'Internet Explorer';
SEGMENT_LENGTH = 1024 * 64;
var
hFile, HInet: HINTERNET;
Buffer: array[0..SEGMENT_LENGTH] of Char;
BufRead: Cardinal;
BufSize: Cardinal;
tempUrl: String;
ret: BOOL;
URL3: PChar; lpszBuffer: PChar;
lpdwBufferLength: DWORD; dwFlags: DWORD;
iFile: Integer;
begin If FileExists(strLocalFile) Then
begin
DeleteFile(strLocalFile);
Sleep(500);
end; iFile := FileCreate(strLocalFile); HInet := InternetOpen(PChar(Agent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(HInet) then
try
URL3:=PChar(URL);
lpdwBufferLength := INTERNET_MAX_URL_LENGTH;
SetLength(tempUrl,lpdwBufferLength );
lpszBuffer := PAnsiChar(tempUrl); ret:=InternetCanonicalizeUrl(URL3,lpszBuffer,lpdwBufferLength,dwFlags);
If ret Then
begin
SetLength(tempUrl,lpdwBufferLength );
ShowMessage(URL3+'ok'+lpszBuffer);
end;
hFile := InternetOpenUrl(HInet,lpszBuffer, nil, 0, INTERNET_FLAG_RELOAD, 0);
if Assigned(hFile) then
try
BufSize := SizeOf(Buffer);
with TStringStream.Create('') do
try
while InternetReadFile(hFile, @Buffer, BufSize, BufRead) and (BufRead > 0) do
begin
FileWrite(iFile, Buffer, BufRead);
end; finally
Free;
FileClose(iFile);
end; finally
InternetCloseHandle(hFile);
end; finally
InternetCloseHandle(hinet);
end;
end;
现在是没有报错了,若文件名含中文,还是提示:
Http Status Code: 404
Reason: File not found or unable to read file
参考:http://topic.csdn.net/u/20090121/12/6743aba0-27af-44c3-a2f1-451d236c2ffe.html