全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:6921
推到 Plurk!
推到 Facebook!

請問如何用 Delphi 7 做 HTTPS POST ? (方法不限)

缺席
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-02-01 15:57:27 IP:61.219.xxx.xxx 未訂閱
請問如何用 Delphi 7 做 HTTPS POST ? (方法不限)
找過舊文章了
用 Google 搜尋 "IdHTTP TIdSSLIOHandlerSocket https" 的文章也都看過了, 相關資訊如下

期待自己可以測試出結果 OR 有好心人士幫忙 提供範例
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-02-01 17:06:24 IP:61.219.xxx.xxx 未訂閱

http://www.tek-tips.com/viewthread.cfm?qid=419856
找到一點資訊
You will need 2 ".dll"s for SSL to work with the Indy components:
"libeay32.dll" and "ssleay32.dll"
I downloaded them from
http://www.intelicom.si/download.php?op=viewdownload&cid=1
Do not forget to put the ".dll"s in the same directory as your executable.
here is the code I used:
<textarea class="delphi" rows="10" cols="60" name="code"> unit SSL_http_App; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, OleCtrls, SHDocVw, StdCtrls, ComCtrls, IdAntiFreezeBase, IdAntiFreeze, IdIOHandler, IdIOHandlerSocket, IdSSLOpenSSL, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, Idhttp; type TForm1 = class(TForm) Idhttp: TIdhttp; IdSSLIOHandlerSocket: TIdSSLIOHandlerSocket; Edit1: TEdit; btnPostURL: TButton; RichEdit1: TRichEdit; WebBrowser1: TWebBrowser; procedure btnPostURLClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.btnPostURLClick(Sender: TObject); var sURL : string; sFPR : string; SFPRvalue : string; sRspncFileName : string; RspncStream : TMemoryStream; begin //setup idhttp -if using the same values all the time just set in the component //should the IP that you are connecting to idhttp.Host := '127.0.0.1'; //should be 80 for http and 443 for https idhttp.Port := 443; //SSL ver 2 or 3 idSSLIOHandlerSocket.SSLOptions.Method := TIdSSLVersion(sslvSSLv23); //location of the SSL certificate idSSLIOHandlerSocket.SSLOptions.RootCertFile := 'ssl_cert.pem'; //Tells the client to use SSL idhttp.IOHandler := idSSLIOHandlerSocket; //location of CGI,PERL,Servlet,... you are posting to sURL :='https://www.blahblah.com/Apps/client/page'; //field you are posting, I just liked it seperated out sFPR :='?MyName='; //the value of the field, remember to encode it sFPRvalue := idhttp.URL.ParamsEncode('Billy Blah Bob'); //Create Stream RspncStream := TMemoryStream.Create(); RspncStream.Clear; RspncStream.Seek(0,soFromBeginning); //connect idhttp.Connect; (* use a try and except here to catch the "EIdConnClosedGracefully" error that is generated when the server disconnects, the comments in unit.procedure "TIdTCPConnection.CheckForDisconnect" state to do this. *) try //post idhttp.Post(sURL SFPR SFPRvalue,idhttp.Request.RawHeaders,RspncStream); except on e : exception do begin Raise Exception.Create(e.ClassName ' : ' e.Message); end; end; //disconnect idhttp.Disconnect; //display Rspnc RspncStream.Seek(0,soFromBeginning); //reset your stream position RichEdit1.Lines.LoadFromStream(RspncStream); //Free Memory RspncStream.Free; //Display HTML sRspncFileName := ExtractFilePath(Application.ExeName) //tried generate a unique filename each time sRspncFileName := sRspncFileName IntToStr(DateTimeToFileDate(Now())) '.html'; RichEdit1.PlainText := True; RichEdit1.Lines.SaveToFile(sRspncFileName); WebBrowser1.Navigate(sRspncFileName); end; end; </textarea>
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-02-01 17:13:50 IP:61.219.xxx.xxx 未訂閱
如果使用 idhttp IdSSLIOHandlerSocket 出現 cannot load ssl library 錯誤
可以去下載下面檔案
http://indy.fulgan.com/SSL/indy_OpenSSL096m.zip

------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-02-01 17:20:07 IP:61.219.xxx.xxx 未訂閱
Cheers,
Phil.

Chris Veale wrote:
In my application I have the following components...

IdConnectionIntercept
IdIPWatch
IdHttp
IdSSLIOHandlerSocket

and all I changed in the IdSSLIOHandlerSocket was to set up the method and the mode properties appropriately
Set the IOHandler property of the IdHTTP component to the IdSSLIOHandlerSocket component
Set the Intercept property of the IdHTTP component to the IdConnectionIntercept component
Set the Port of the IdHTTP component to 443
Set the appropriate Host method in the IdHTTP component
Make sure the Request-Content Type property is set to "application/x-www-form-urlencoded"

I also have the following function to retrieve the systems proxy settings that is called before the following code, I was also playing around with retrieving the username and password from the proxy server to resolve the need for getting my users to log into the system again, but didnt quite get that working, then time constraints and other projects...Im sure you know the story...
Anyways hope this helps for your issue...
Chris

<textarea class="delphi" rows="10" cols="60" name="code"> {*****************************************************************} {* *} {* Procedure retrieves the current proxy server and port (from *} {* the current connection) and loads the idhttp component with *} {* these values *} {* *} {*****************************************************************} procedure GetDefaultProxyServer(var Server, ByPass: string; var Port: Integer); const BUF_SIZE: DWORD = 500; var ProxyInfo: PInternetProxyInfo; Buffer: Pointer; httpspos,equalspos,colonpos,semipos:integer; proxyname,proxyport,tmp:string; begin //allocate some memory GetMem(Buffer, BUF_SIZE); //try and retrieve the required data try //query the proxy information for the connetion if not InternetQueryOption(nil, INTERNET_OPTION_PROXY, Buffer, BUF_SIZE) then begin //something went wrong so signal the error RaiseLastWin32Error; end; //assign the structure with the result buffer ProxyInfo := Buffer; //set the bypass structure also ByPass := ProxyInfo^.lpszProxyBypass; //make sure we have proxy info in the structure if (ProxyInfo^.lpszProxy = '') then begin Server := ''; Port := 0; end else begin //retrieve the https proxy name httpspos := ansipos('https',ProxyInfo^.lpszProxy); if httpspos <> 0 then begin tmp := copy(ProxyInfo^.lpszProxy,httpspos,1000); semipos := ansipos(' ',tmp); tmp := copy(tmp,1,semipos-1); equalspos := ansipos('=',tmp); colonpos := ansipos(':',tmp); proxyname := copy(tmp,equalspos 1,colonpos-7); proxyport := copy(tmp,colonpos 1,10); //load the components bits form2.IdHTTP1.ProxyParams.ProxyPort := strtoint(proxyport); form2.IdHTTP1.ProxyParams.ProxyServer := proxyname; end; end; finally //free the memory FreeMem(Buffer); end; end;{getdefaultproxyserver} </textarea>

Then do the other code based tasks as follows


<textarea class="delphi" rows="10" cols="60" name="code"> IdHTTP1.Request.Username := myUsername; IdHTTP1.Request.Password := myPassword; try //authenticate to the server IdHTTP1.Connect; //do your other tasks except on e: EIdException do begin //I handle the response code here but just used showmessage to give you an idea showmessage(e.message); end; on e: exception do begin showmessage(e.message); end; end; </textarea>
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
系統時間:2024-04-27 7:20:52
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!