線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3314
推到 Plurk!
推到 Facebook!

關於https型態的xml網址下載到本機.xml檔案

尚未結案
ginochen
一般會員


發表:9
回覆:14
積分:4
註冊:2005-02-17

發送簡訊給我
#1 引用回覆 回覆 發表時間:2018-07-13 12:08:55 IP:125.227.xxx.xxx 未訂閱
各位前輩好,如題所述,我有爬文找到如下的範例,在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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2018-07-13 12:39:48 IP:59.127.xxx.xxx 未訂閱
可能是對方網站已經關閉 TLS 1.0 協定,而 XP 與 2003 只支援到 TLS 1.0
您可以試試看在 XP 或是 2003 上用 IE 能不能下載該 XML,如果不能就可以要改用 Indy 了
------
歡迎參訪 "腦殘賤貓的備忘錄" http://maolaoda.blogspot.com/
ginochen
一般會員


發表:9
回覆:14
積分:4
註冊:2005-02-17

發送簡訊給我
#3 引用回覆 回覆 發表時間:2018-07-13 13:28:27 IP:125.227.xxx.xxx 未訂閱
謝謝您,似乎是這樣的..在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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2018-07-13 15:18:56 IP:59.127.xxx.xxx 未訂閱
我對 Indy 並不熟悉,也許久沒有寫 Delphi 程式了,不知版上有沒有高手接著回答這個問題?
------
歡迎參訪 "腦殘賤貓的備忘錄" http://maolaoda.blogspot.com/
GrandRURU
站務副站長


發表:240
回覆:1680
積分:1874
註冊:2005-06-21

發送簡訊給我
#5 引用回覆 回覆 發表時間:2018-07-13 15:36:11 IP:1.200.xxx.xxx 未訂閱
D7 可以升級 indy 10.6以上 或使用 ICS 元件亦可
Jasonwong
版主


發表:49
回覆:931
積分:581
註冊:2006-10-27

發送簡訊給我
#6 引用回覆 回覆 發表時間:2018-07-14 17:01:58 IP:125.227.xxx.xxx 未訂閱

[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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2018-07-16 10:40:38 IP:59.120.xxx.xxx 未訂閱
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

發送簡訊給我
#8 引用回覆 回覆 發表時間:2018-07-16 10:43:04 IP:000.000.xxx.xxx 未訂閱
提問者您好:


以上回應是否已得到滿意的答覆?


若已得到滿意的答覆,請在一週內結案,否則請在一週內回覆還有什麼未盡事宜,不然,
將由版主(尚無版主之區域將由副站長或站長)自由心證,選擇較合適之解答予以結案處理,
被選上之答題者同樣會有加分獎勵同時發問者將受到扣 1 分的處分。不便之處,請見諒。


有問有答有結案,才能有良性的互動,良好的討論環境需要大家共同維護,感謝您的配合。

------
我是機器人,我不接受簡訊.
ginochen
一般會員


發表:9
回覆:14
積分:4
註冊:2005-02-17

發送簡訊給我
#9 引用回覆 回覆 發表時間:2018-07-16 11:14:26 IP:125.227.xxx.xxx 未訂閱
謝謝您的指導,但有出現這樣的錯誤訊息↓
'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

發送簡訊給我
#10 引用回覆 回覆 發表時間:2018-07-16 11:45:43 IP:125.227.xxx.xxx 未訂閱
謝謝您的回覆,我是使用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

發送簡訊給我
#11 引用回覆 回覆 發表時間:2018-07-16 13:25:23 IP:125.227.xxx.xxx 未訂閱
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

發送簡訊給我
#12 引用回覆 回覆 發表時間:2018-08-04 19:16:21 IP:119.137.xxx.xxx 未訂閱
1. 用 TIdHttp 肯定可以下载;
2. 至于 https,需要为 TIdHTTP 增加一些东西,具体如何使用,你可以上网搜;
3. 下载下来的 UTF8 字符串,在 D7 底下应该有办法可以解码。我记得 D7 底下有个函数:UTF8Encode 和 UTF8Decode。或者也有其它办法,搜索的话,肯定能搜到答案。
pcplayer99
尊榮會員


發表:146
回覆:790
積分:632
註冊:2003-01-21

發送簡訊給我
#13 引用回覆 回覆 發表時間:2018-08-04 19:31:01 IP:119.137.xxx.xxx 未訂閱
搜了一下,搜到这里:
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

發送簡訊給我
#14 引用回覆 回覆 發表時間:2018-08-19 11:44:13 IP:111.184.xxx.xxx 未訂閱
● 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

發送簡訊給我
#15 引用回覆 回覆 發表時間:2018-08-19 23:30:02 IP:111.184.xxx.xxx 未訂閱
供其他程式語言使用, 以抓取 HTTPS URL 上檔案(如 *.xml)的 dll 函式(含 Lazarus 源碼)
http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=110202
系統時間:2024-04-29 15:33:51
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!