關於https型態的xml網址下載到本機.xml檔案 |
尚未結案
|
ginochen
一般會員 發表:9 回覆:14 積分:4 註冊:2005-02-17 發送簡訊給我 |
各位前輩好,如題所述,我有爬文找到如下的範例,在win7以上平台也運作成功,但在xp及2003 server的作業系統就會失敗,先貼上code:
function Download(URL, User, Pass, FileName: string): Boolean; const BufferSize = 1024; var hSession, hURL: HInternet; Buffer: array[1..BufferSize] of Byte; BufferLen: DWORD; F: File; begin Result := False; hSession := InternetOpen('', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ; // Establish the secure connection InternetConnect ( hSession, PChar(URL), INTERNET_DEFAULT_HTTPS_PORT, PChar(User), PChar(Pass), INTERNET_SERVICE_HTTP, 0, 0 ); try hURL := InternetOpenURL(hSession, PChar(URL), nil, 0, 0, 0) ; if hURL = nil then exit; try AssignFile(f, FileName); Rewrite(f,1); try repeat InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) ; BlockWrite(f, Buffer, BufferLen) until BufferLen = 0; finally CloseFile(f) ; Result := True; end; finally InternetCloseHandle(hURL) end finally InternetCloseHandle(hSession) end; end; 以上的code其中這句↓ hURL := InternetOpenURL(hSession, PChar(URL), nil, 0, 0, 0) ; 在win7以上作業系統都OK,但是xp或2003 server執行時hURL就會得到nil,請問有何解決方式嗎,或有其它的function可供使用,謝謝了~ |
sryang
尊榮會員 發表:39 回覆:762 積分:920 註冊:2002-06-27 發送簡訊給我 |
|
ginochen
一般會員 發表:9 回覆:14 積分:4 註冊:2005-02-17 發送簡訊給我 |
謝謝您,似乎是這樣的..在xp直接用IE開那個網址(https://www.cwb.gov.tw/rss/forecast/36_04.xml)確實開不起來...
請教可否題供Indy相關範例,我的目的就是想將如上網址的內容存到本機(ex->C:\sample.xml),我用的是Delphi7,感恩.. ===================引 用 sryang 文 章=================== 可能是對方網站已經關閉 TLS 1.0 協定,而 XP 與 2003 只支援到 TLS 1.0 您可以試試看在 XP 或是 2003 上用 IE 能不能下載該 XML,如果不能就可以要改用 Indy 了 |
sryang
尊榮會員 發表:39 回覆:762 積分:920 註冊:2002-06-27 發送簡訊給我 |
|
GrandRURU
站務副站長 發表:240 回覆:1680 積分:1874 註冊:2005-06-21 發送簡訊給我 |
|
Jasonwong
版主 發表:49 回覆:931 積分:581 註冊:2006-10-27 發送簡訊給我 |
[code delphi] var MS: TMemoryStream; begin MS:= TMemoryStream.Create; try IdHTTP1.Request.CacheControl := 'no-cache'; IdHTTP1.Get(URL, MS); MS.SaveToFile('C:\123.xml'); finally FreeAndNil(MS); end; end; [/code] 試試
------
聰明的人,喜歡猜心;雖然每次都猜對了,卻失去了自己的心 傻氣的人,喜歡給心;雖然每次都被笑了,卻得到了別人的心 |
GrandRURU
站務副站長 發表:240 回覆:1680 積分:1874 註冊:2005-06-21 發送簡訊給我 |
var EncodeURL: string; ARspStreamData: TStringStream; begin Memo1.Clear; ARspStreamData := TStringStream.Create('', TEncoding.UTF8); EncodeURL := 'https://www.cwb.gov.tw/rss/forecast/36_04.xml'; SslHttpCli1.RcvdStream := ARspStreamData; SslHttpCli1.URL := EncodeURL; SslHttpCli1.Get(); Memo1.Lines.Add(FormatXMLData(ARspStreamData.DataString)); |
Ktop_Robot
站務副站長 發表:0 回覆:3511 積分:0 註冊:2007-04-17 發送簡訊給我 |
|
ginochen
一般會員 發表:9 回覆:14 積分:4 註冊:2005-02-17 發送簡訊給我 |
謝謝您的指導,但有出現這樣的錯誤訊息↓
'IOHandler value is not valid' ===================引 用 Jasonwong 文 章=================== [code delphi] var MS: TMemoryStream; begin MS:= TMemoryStream.Create; try IdHTTP1.Request.CacheControl := 'no-cache'; IdHTTP1.Get(URL, MS); MS.SaveToFile('C:\123.xml'); finally FreeAndNil(MS); end; end; [/code] 試試 |
ginochen
一般會員 發表:9 回覆:14 積分:4 註冊:2005-02-17 發送簡訊給我 |
謝謝您的回覆,我是使用Delphi7,您的版本是否較新,SslHttpCli1元件是ICS?可能我一樣要再研究才行,或許很惰性的想
請教,有沒有其它更直接用於D7原型的解法了,謝謝 ===================引 用 GrandRURU 文 章=================== var EncodeURL: string; ARspStreamData: TStringStream; begin Memo1.Clear; ARspStreamData := TStringStream.Create('', TEncoding.UTF8); EncodeURL := 'https://www.cwb.gov.tw/rss/forecast/36_04.xml'; SslHttpCli1.RcvdStream := ARspStreamData; SslHttpCli1.URL := EncodeURL; SslHttpCli1.Get(); Memo1.Lines.Add(FormatXMLData(ARspStreamData.DataString)); |
Jasonwong
版主 發表:49 回覆:931 積分:581 註冊:2006-10-27 發送簡訊給我 |
IOHandler value is not valid 的問題是因為你要下載 HTTPS 的檔案, 所以需多加 INDY SSL 元件 及 OPEN SSL 相關的 DLL 檔
但 XP 只支援到 TLS 1.0,所以你還要手動升級 INDY 或者另外使用 ICS 元件組 最後,由於檔案下載下來是 UTF-8 格式 D7 不支援,如果在不升級 DLEPHI 的情況下,你還要另外把 UTF-8 轉成 ANSI 以上
------
聰明的人,喜歡猜心;雖然每次都猜對了,卻失去了自己的心 傻氣的人,喜歡給心;雖然每次都被笑了,卻得到了別人的心 |
pcplayer99
尊榮會員 發表:146 回覆:790 積分:632 註冊:2003-01-21 發送簡訊給我 |
|
pcplayer99
尊榮會員 發表:146 回覆:790 積分:632 註冊:2003-01-21 發送簡訊給我 |
搜了一下,搜到这里:
https://stackoverflow.com/questions/16367842/indy-10-and-openssl ndy does not implement SSL natively. What it does is implements a flexible IOHandler architecture that allows for any SSL implementation to be plugged into Indy. Indy itself implements its own IOHandler class that is based on OpenSSL (MS CryptoAPI support is planned for the future). SecureBlackbox, for example, provides an Indy IOHandler class for its own SSL engine. On most platforms, OpenSSL is used via external DLLs, which cannot be used from resources. Indy does not ship with OpenSSL DLLs, because OpenSSL encryption is restricted by international import/export laws, so either OS vendors that ship internationally have special licenses to ship OpenSSL DLLs, or end-users have to download/compile OpenSSL themselves locally. That is what makes Indy "portable" when it comes to OpenSSL - Indy uses whatever OpenSSL DLLs are pre-installed outside of Indy, either in the OS or in your app's own installation folder. The only exception to this that concerns Indy is OpenSSL on iOS, which requires OpenSSL to be used statically because third-party dynamic libraries are not allowed on iOS devices. If you want to use OpenSSL statically with Indy on other platforms, you have to compile/obtain the static version of OpenSSL yourself and add it to your project, then recompile Indy to enable its STATICLOAD_OPENSSL define (which is currently only defined for iOS), and finally include the IdSSLOpenSSLHeaders_static.pas unit in your code's uses clause to hook up the necessary support code. Note that this is supported only in the recently released Indy 10.6 version.If that is too much work for you, then use a third-party SSL implementation that is compatible with Indy, such as SecureBlackbox, or write your own IOHandler class that does what you need. As for your other questions: 1) Indy does raise exceptions if it cannot load OpenSSL correctly during socket operations. So chances are those exceptions are getting caught and swallowed before you have a chance to report them to your users. If you don't want to rely on that, then you can manually call Indy's Load() function in the IdSSLOpenSSLHeaders.pas unit before starting your socket work. Load() will load OpenSSL into memory if it is not already loaded. If Load() fails, you can then call the WhichFailedToLoad() function in that same unit to find out why Load() failed.2) No, it is not possible to use OpenSSL from resources (without some really serious low-level trickery). |
lazarus
一般會員 發表:14 回覆:38 積分:20 註冊:2018-05-12 發送簡訊給我 |
● Lazarus 插花一下 (Lazarus 直接支援 UTF-8)
本篇同另一篇討論 http://delphi.ktop.com.tw/board.php?cid=168&fid=912&tid=110196 將下圖下載連結修改為以下即可(我有實際測過, 可成功下載 UTF-8 格式 XML 檔) fstream:=TFileStream.Create('c:\_temp\36_04.xml', fmCreate); IdHTTP1.Get('https://www.cwb.gov.tw/rss/forecast/36_04.xml', fstream); ● indy 當漏 (for lazarus) http://indy.fulgan.com/ZIP/ 解壓後目錄 ..\Lib\indylaz.lpk 就是 Lazarus 的元件安裝包 ● SSL 當漏 (TLS V1.2 加密協定) "Could not load SSL library.”之問題要到 http://indy.fulgan.com/SSL/ 下載 openssl-1.0.2o-i386-win32.zip 解壓後, 將 libeay32.dll & ssleay32.dll兩個檔案放到執行檔同目錄, 就可做 https 協定之處理 |
lazarus
一般會員 發表:14 回覆:38 積分:20 註冊:2018-05-12 發送簡訊給我 |
供其他程式語言使用, 以抓取 HTTPS URL 上檔案(如 *.xml)的 dll 函式(含 Lazarus 源碼)
http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=110202 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |