iw14.0.50 come, finally can be directly in the address bar to enter the URL to open the IW function page, you can freely use the Easyui and other JS frame; the display Mode dialog box is no longer seven around eight around the annoying;

Source: Internet
Author: User
Tags sha1

the Sample code has been released! please move forward using delphi+intraweb for the development of the 1~4 code sample for download, Although the sample code is removed from my project, the package is well suited for self-scaling and Modification.

iw14.0.50, in the new version of the most attractive to me is to add a perfect HttpHandler function: finally can be directly in the address bar to enter the URL to open the IW function page, you can freely use the Easyui and other JS frame; the display Mode dialog box is no longer seven around the eight around the annoying; hehe, I felt the first time IW approached The mainstream web development tool!

Get excited and try it. In fact, the pit of the IW is still quite a lot, although it is close to the mainstream, but the back slowly will be said ...

1, A new IW project, choose Stand Alone server/service, This mode of development is the most ideal, debugging is very convenient, the official release can be set up a library type of project published to The. net Server. (yes, Everyone is right, now that IW is out of ISAPI mode and can be deployed to IIS just like A. Net Mvc4 app, It is explained later That. net virtual hosts can also publish IW apps!) Great Progress. )

2, After saving the project in the project to add a new unit file, such as named wxapi.pas, the code in this file will assume access to Work. The code is as Follows:

Interface

uses
Classes,IW.Content.Base,System.Sysutils,Httpapp,Iwapplication,
IW.HTTP.Request,IW.HTTP.Reply;

type
// <summary>
///the class inherited from Tcontentbase is equivalent to the HttpHandler in ASP .
// </summary>
Twxapi=class(Tcontentbase)
protected
functionExecute(Arequest:Thttprequest;Areply:Thttpreply;ConstApathname:string;Asession:Tiwapplication;Aparams:TStrings):Boolean;Override;
public
ConstructorCreate;Override;
End;

Implementation

uses
Servercontroller,Usersessionunit,Crypt.SHA1;

{twxapi}

ConstructorTwxapi.Create;
begin
inherited;
//file does not need to exist in real life
Filemustexist:=False;
End;

functionTwxapi.Execute(Arequest:Thttprequest;Areply:Thttpreply;
ConstApathname:string;Asession:Tiwapplication;
Aparams:TStrings):Boolean;
var
Signature:string;
Timestamp:string;
Nonce:string;
Echostr:string;
STRs:Tstringlist;
Tmpstr:string;
begin
Result:=True;

Signature:=Aparams.Values[' Signature '];
Timestamp:=Aparams.Values[' Timestamp '];
Nonce:=Aparams.Values[' Nonce '];
Echostr:=Aparams.Values[' Echostr '];

STRs:=Tstringlist.Create;
STRs.Add(' Mytesttoken ');//Token, to align with interface configuration information
STRs.Add(Timestamp);
STRs.Add(Nonce);
STRs.Sort;

Tmpstr:=STRs[0]+STRs[1]+STRs[2];
Tmpstr:=SHA1(Ansistring(Tmpstr));

if Tmpstr=Signature then
begin
Areplywritestring (echostr    end   else   begin  
    areply. Writestring (    end
  asession. Terminate;  //release session resource, This handler no session  
End ; 

end .

As the code shows, tcontentbase is the base class for the IW version of httphandler, and if you do not need to display iwform, inherit from this Type. If you need to use Iwform also have a tcontentform base class can be used, wow, in the browser address bar to enter the URL can also be opened directly iwform Oh. Twxapi. The code in execute is an access code that is very simple and does not understand to look for help: access Guide.

3.Register this HttpHandler in Servercontroller and post the Servercontroller registration code directly:
procedureTiwservercontroller.Iwservercontrollerbaseconfig(Sender:TObject);
begin
//register our defined handler in the Servercontroller.onconfig event
the //servercontroller.onconfig event is only run once throughout the application life cycle
withThandlers.Add(‘‘,' Wxapi.php ',Twxapi.Create) do
begin
Canstartsession:=True;//literally understood to be able to start the session
Requiressessionstart:=False;       //literal understanding is the need to start the session, both properties must be set, otherwise input/ Wxapi.php will turn to the main form  
                                           // That is, if you do not set canstartsession and requiressessionstart, you must perform A/$/start startup session before  
                                           //can access The/wxapi.php page normally, This is obviously not what we need.  
   end
End

As shown in the code, the red thandlers. Add(' ', ' wxapi.php ', twxapi. Create) This code completes the HttpHandler registration, in the browser address bar input http://localhost/wxapi.php can access the newly registered Controller.

But there's a pit in this code that's a medium-sized hole, see comments in my Code.

This pit is handler. after registering to start the IW app but not first enter The/$/start launcher in the browser address bar, but instead directly enter/wxapi.php to verify HttpHandler failure, the page automatically navigates to the main form! After reading the help found, you need to set Tcontentbase. Requiressessionstart:=false otherwise, The IW app must start the session to access the main form before using httphandler, and set the tcontentbase as described in the HELP. Requiressessionstart:=false Although direct input/ Wxapi.php no longer navigates to the main form, but prompts for a 404 code error, and one-step trace discovers that the HttpHandler code has actually been executed, so there should be no 404 errors, and multi-party verification and experimental discovery will also need to set up Tcontentbase. canstartsession : = True, oh, This is not mentioned in the help, it is estimated that the new version of the newly added properties. ok, now in the Address bar input http://localhost/wxapi.php can open the page normally.

4, Copy the completed IW application to the host for testing, actual access

, actually prompted the configuration failed! This is what the case, I use the code is written from someone else, a Delphi version of the Access interface code copy, the same program code without any problems, then the first idea is that the page encoding is not correct, ok, i change, Iw's handler default encoding is Utf-8 format, So I tried a number of gbk,iso-8859-1 and other encoding format, all prompt the above Error. Helpless had to write a log to see exactly handler code execution did not, the result is shocking, put on the real server, verify that handler code actually did not execute, and in my native debugging and real server browser when browsing is good without any problems. Huge pit ah, connected for several days of various tests, various modifications, are ready to give up, hehe, the result looked at the next IW own HttpHandler example, found in its servercontroller implemented an event:Onbrowsercheck, So try to add the same event code in your own code to test, wow, you can ...

procedureTiwservercontroller.Iwservercontrollerbasebrowsercheck(
Asession:Tiwapplication;varRBrowser:Tbrowser);
begin
//this event code is very important, I have been stuck here for several days!
//
//when This event is not implemented, the/wxapi.php can respond successfully to any browser input, except for
//in The display configuration failed, later in the code to use the log output to find the IW can receive the request, but
//twxapi.execute method did not execute, and then went to the official website to read the relevant help, only to find that IW only supported by the
//viewer to respond properly to the output, and the Web request that was made is clearly not part of any known browser
if RBrowser is tother then begin
RBrowser. Free;
rbrowser : = tinternetexplorer.  Create(8); //for page content output with compatible IE8 page views
end;
end;

, oh, it is not easy, just a few lines of code toss for several days, but IW can finally be used for Development.

I think the power of Delphi is in addition to the compiler all the source code is provided, so that problems can be solved by reading the source, but IW is too closed, no source even, help can not keep up, the online help is too weak, It is recommended to use IW friends to combine online help with Iw's own example project to see together, less detours! however, the development of IW has been very useful today, especially for people with Delphi plot, can use their best language and development tools for web development is a very cool thing.

Not finished, to be continued ...

The next time you talk about the latest deployment of iw, deploy it in. net Mvc4 mode on iis, hehe, if only see IW online Help on the MVC approach to deployment go to practice, there are small pits oh!

Http://www.cnblogs.com/dpower/p/5138080.html

iw14.0.50 come, finally can be directly in the address bar to enter the URL to open the IW function page, you can freely use the Easyui and other JS frame; the display Mode dialog box is no longer seven around eight around the annoying;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.