Delphi + intraweb for WeChat development-WeChat platform access

Source: Internet
Author: User
The sample code has been released! Step 1: Use delphi + intraweb for development ~ 4. download the sample code. Although the sample code is removed from my project, it is well-encapsulated and suitable for self-extension and modification. The sample code has been released! Step 1: Use delphi + intraweb for development ~ 4. download the sample code. Although the sample code is removed from my project, it is well-encapsulated and suitable for self-extension and modification.

Iw14.0.50 is coming. what attracts me the most in the new version is the addition of the complete httphandler function: you can finally enter a url in the address bar to open the iw function page; I am free to use js frameworks such as EasyUI. the display mode dialog box is no longer annoying. I feel that iw is close to mainstream web development tools for the first time!

I'm so excited. please try it. In fact, there are still a lot of iw pitfalls. although they are close to the mainstream, I will talk about them later...

1. create an iw project and select Stand Alone Server/Service. In this mode, development is ideal and debugging is convenient, you can create a library-type project and release it.. net server. (Yes, you are not mistaken. now iw is out of the isapi mode and can be deployed like this. the. net mvc4 application is deployed on iis, which will be explained later ,. net virtual hosts can also publish iw applications! Great progress .)

2. after saving the project, add a new unit file to the project. for example, name it wxapi. pas. the code in this file will undertake the access work. The code is as follows:

Interface

Uses
Classes, IW. Content. Base, System. SysUtils, HTTPApp, IWApplication,
IW. HTTP. Request, IW. HTTP. Reply;

Type
///


/// The Class inherited from TContentBase is equivalent to httphandler in asp.net.
///
TWxApi = class (TContentBase)
Protected
Function Execute (aRequest: THttpRequest; aReply: THttpReply; const aPathname: string; aSession: TIWApplication; aParams: TStrings): Boolean; override;
Public
Constructor Create; override;
End;

Implementation

Uses
ServerController, UserSessionUnit, Crypt. SHA1;

{TWxApi}

Constructor TWxApi. Create;
Begin
Inherited;
// The file does not need to exist.
FileMustExist: = False;
End;

Function TWxApi. Execute (aRequest: THttpRequest; aReply: THttpReply;
Const aPathname: 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, which must be consistent with the 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
AReply. WriteString (echostr)
End else begin
AReply. WriteString ('if you see this prompt, this link address can be used as the interface address. ');
End;
ASession. Terminate; // release session resources. This handler does not need a session
End;

End.

As shown in the code, TContentBase is the basic class of httphandler of iw. if iwForm is not displayed, inherit from this type. If you want to use iwForm, you can also use the TContentForm base class. Wow, you can enter a url in the address bar of your browser to directly open iwform. The code in TWxApi. Execute is the access code, which is very simple. if you are not familiar with it, see Help: Access Guide.

3. Register httphandler in ServerController and paste the ServerController registration code directly:
Procedure TIWServerController. IWServerControllerBaseConfig (Sender: TObject );
Begin
// Register the Handler we defined in the ServerController. OnConfig event
// The ServerController. OnConfig event is run only once throughout the application lifecycle.
With THandlers. Add ('', 'wxapi. php', TWxApi. Create) do
Begin
CanStartSession: = True; // literally, the session can be started.
RequiresSessionStart: = False; // literally, the session needs to be started. These two attributes must be set. Otherwise, the input/wxapi. php will be switched to the main form.
// That is, if CanStartSession and RequiresSessionStart are not set, you must first run/$/start to start the session.
// 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 httphandler registration. in the browser address bar, enter http: // localhost/wxapi. php can access the controller just registered.

However, this code is not a small Pitfall. for details, see the comments in my code.

This pitfall is when the handler starts the iw application after registration, but does not input/$/start in the browser address bar to start the program, but directly input/wxapi. php fails to verify httphandler. the page is automatically redirected to the main form! After reading help, we found that you need to set TContentBase. requiresSessionStart: = false otherwise, the iw application must start the session to access the main form before using httphandler. TContentBase is set according to the help instructions. requiresSessionStart: = false although directly input/wxapi. php will no longer navigate to the main form, but a 404 code error will be prompted. the single-step tracking finds that the httphandler code has indeed been executed, so there should be no 404 error, for multi-party verification and experiment discovery, you also need to set TContentBase. canStartSession: = True, huh, this is not mentioned in The Help, it is estimated that it is a new attribute of the new version. Now, enter http: // localhost/wxapi. php in the address bar to open the page normally.

Haha, it's not easy. just a few lines of code have been tossing around for several days, but iw can finally be used for development.

I think that Delphi is powerful in that all the source code except the compiler is provided. if there is a problem, you can read the source code to solve it. However, iw is too closed and can't keep up with the help even if there is no source code, the online help is too weak. I suggest using iw's friends to work with the online help and iw's own sample project to avoid detours! However, the development of iw has become quite useful today. especially for those with delphi plot, it is really nice to use their best language and development tools for web development.

For more information about development of delphi and intraweb-related articles on platform access, refer to the Chinese PHP website!

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.