若文件名含有中文,则从服务器收到以下 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

解决方案 »

  1.   

    Delphi是什么版本?Delphi2009还是其它版本?
      

  2.   

    源URL的值能否给一个示例?InternetCanonicalizeUrl调用是否成功?InternetCanonicalizeUrl之后返回的内容是什么?
      

  3.   

    http://webserver/test.doc 这样就行
    http://webserver/考勤.doc  不行
      

  4.   

    InternetCanonicalizeUrl调用是否成功?原来我没有这转换,直接把url给InternetCanonicalizeUrl
    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;}
      

  5.   

    上面的代码当中应该是lpszBuffer没有指向有效的内存,这个内存是需要应用程序自己管理的。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;
      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; 
      

  6.   

    谢谢楼上
    现在是没有报错了,若文件名含中文,还是提示:
    Http Status Code: 404
    Reason: File not found or unable to read file
      

  7.   

    这个可能是编码的问题,可以试一下通过UrlEncode,然后再发送。
    参考:http://topic.csdn.net/u/20090121/12/6743aba0-27af-44c3-a2f1-451d236c2ffe.html
      

  8.   

    我没有传参给控件,写死在里面,弹出的url是正常,没有乱码