Title: WebService usage and debugging Summary
Author: jrq
Link: http://blog.csdn.net/jrq/archive/2006/05/24/752174.aspx
Content:
Record the use and debugging of WebService. Memo.
Because jb2005 + axis is used to publish WebService, there are always some strange problems, so jb9 is used.
(1) Use jb9 + axis to publish the WebService
1. Create a project -- Project, and create a web -- web application;
2. Create a Web Service -- web services configuration;
3. Select the name of the newly created web application for the webapp;
4. Create a WebService server class (test. Java ).
-----------------------------
Public class test {
Private Static int;
Private Static int B;
Public test (){
}
Public void Seta (int x ){
This. A = X;
}
Public void SETB (INT y ){
This. B = y;
}
Public int getsum (){
Int c = A + B;
// System. Out. println ("A:" + );
// System. Out. println ("B:" + B );
// System. Out. println ("C:" + C );
Return C;
}
}
-----------------------------
5. Publish the WebService
(1) Right-click the class to be published and select "export as a Web service ";
(2) If "generate client stub" is selected, the client class and Interface Test class are generated at the same time after the release. You can skip this option;
(3) In "deploy scope", you can select this option as "session". Other Default values are as follows;
(4) Select "allow selected methods" in "selection mode" and select the interface method to be released in "methods in. You can select multiple.
(5) by default, click "finish" to generate the Web Service Description Language file.
6. Note
Class attributes use the Private Static modifier. For example, "Private Static int ".
Class methods cannot use static modifiers, such as "Public int getsum ()", because static class methods cannot be published as WebService interfaces.
(2) Use Delphi7 to compile the WebService Client
1. Create a project & Application;
2. New-other-WebService-WSDL importer, select the WSDL description file of the WebService Service, or select the URL of the WSDL file;
3. Click "finish" to generate the *. Pas unit file corresponding to the WSDL file;
The content is roughly as follows:
------------------------------------------------------
Test = interface (iinvokable)
['{787ce852-ef00-40ba-5802-9bf9dfbf79f0}']
Procedure Seta (const A: integer); stdcall;
Procedure SETB (const B: integer); stdcall;
Function getsum: integer; stdcall;
End;
Function gettest (usewsdl: Boolean = system. False; ADDR: String = ''; httprio: thttprio = nil): test;
Implementation
Function gettest (usewsdl: Boolean; ADDR: string; httprio: thttprio): test;
Const
Defwsdl = 'HTTP: // 127.0.0.1: 8088/test/services/test? WSDL ';
Defurl = 'HTTP: // 127.0.0.1: 8088/test/services/test ';
Defsvc = 'testservice ';
Defprt = 'test ';
VaR
Rio: thttprio;
Begin
Result: = nil;
If (ADDR = '') then
Begin
If usewsdl then
ADDR: = defwsdl
Else
ADDR: = defurl;
End;
If httprio = nil then
Rio: = thttprio. Create (NiL)
else
Rio: = httprio;
try
result: = (Rio as test);
If usewsdl then
begin
Rio. wsdllocation: = ADDR;
Rio. service: = defsvc;
Rio. port: = defprt;
end else
Rio. URL: = ADDR;
finally
If (result = nil) and (httprio = nil) Then
Rio. free;
end;
Initialization
Invregistry. registerinterface (typeinfo (test), 'HTTP: // 127.0.0.1: 8088/test/services/test', 'utf-8 ');
Invregistry. registerdefaultsoapaction (typeinfo (test ),'');
End.
------------------------------------------------------
4. Suggestion: Rename the *. Pas unit file generated by the WSDL file (webtest. Pas). It is best not to use the default name;
5. Call the WebService client compiled by Delphi7Code:
-----------------------------
Uses webtest;
VaR atest: Test; // test class
Sum: integer;
Begin
Atest: = gettest (); // The *. Pas unit file corresponding to the uses WSDL.
Atest. Seta (1 );
Atest. SETB (2 );
Sum: = atest. getsum ();
Showmessage (inttostr (SUM ));
End;
-----------------------------
6. Note
If the static description is not added to the properties of the server class, the client written in Delphi7 cannot get the correct result when it uses Seta () and SETB () and then getsum.
Because WebService is connectionless, a new object application is obtained every time class methods are used. The getsum () operation after Seta () and SETB () operation is not for the same object.
However, after static is added, all objects share the same value of the class. In this case.
In addition, if "session" is selected in "deploy scope" When jb9 + axis releases WebService, instead of the default "request ",
If the attribute of the server class does not use the static constraint, you can use Seta () and SETB () on the Delphi client and then getsum () to obtain the correct return value.
Magic. Pai_^
7. Suggestions
This example only tests and solves the problem. If you want to transmit data to the WebService server, it is best to use parameters in interface methods.
For example, if you change it to getsum (int x, int y) and then release the interface, you can use an interface method to complete the operation, instead of using the set method to pass the parameter data.
(3) Use jb9 to compile the WebService Client
1. Create a project -- project;
2. Create a Web Service -- import a web service;
3. Select the URL of the WSDL file in the wsdl url or the WSDL description file of the WebService Service;
4. Because it is a WebService client, you can select "generate server-side classes;
5. In "package options", you can change "name" to a proper name;
6. Click "finish" to generate the jajva class file of the client. Generally, there are 6 files with their own naming rules. See JB.
7. WebService client test class written in jb9
-----------------------------
Public class client {
Public client (){
}
Public static void main (string [] ARGs) {
try {
testservice service = new testservicelocator ();
test aclient = service. gettest ();
aclient. seta (1);
aclient. SETB (2);
int sum = aclient. getsum ();
system. out. println ("sum is:" + sum);
}< br> catch (exception e) {
system. err. println ("execution failed. exception: "+ E);
}< BR >}
-----------------------------
8. Note
If the attribute of the server class is not added with the static description, the client compiled by JB cannot get the correct result when it uses Seta (), SETB (), and then getsum.
We recommend that you use an interface with parameters to transmit parameter data. Avoid using the set method.
(4) use Delphi7 to call the WebService written in C #
When a client compiled by Delphi7 calls the WebService compiled by C #, garbled characters may occur if the parameters passed in the function are Chinese characters.
Search for some materials online and find a solution: Change the useutf8inheader attribute of httprio to true.
Edit the *. Pas unit file generated by the WSDL file. Add the following sentence to the get function: Rio. httpwebnode. useutf8inheader: = true.
The code snippets are roughly as follows:
------------------
Try
Rio. httpwebnode. useutf8inheader: = true; // Add a sentence and modify the encoding scheme.
Result: = (Rio as test );
If usewsdl then
Begin
Rio. wsdllocation: = ADDR;
Rio. Service: = defsvc;
Rio. Port: = defprt;
End else
Rio. url: = ADDR;
Finally
If (result = nil) and (httprio = nil) then
Rio. Free;
End;
----------------
[-- End --]
By jrq
AM on Sui