首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

delphi 调用c#写关于a+b的webservice,返回值不对?该如何处理

2012-02-29 
delphi 调用c#写关于a+b的webservice,返回值不对?调用helloWorld的返回值是正确的,调用add的时候,传入(1,2

delphi 调用c#写关于a+b的webservice,返回值不对?
调用helloWorld的返回值是正确的,调用add的时候,传入(1,2)时,返回值却为0,不知什么原因,这个问题困惑了很久,请高手们指教~


以下是源码
//c#写的webservice的源码
[WebService(Namespace   =   "http://tempuri.org/ ")]
[WebServiceBinding(ConformsTo   =   WsiProfiles.BasicProfile1_1)]
public   class   servTest   :   System.Web.Services.WebService   {

        public   servTest   ()   {

                //Uncomment   the   following   line   if   using   designed   components  
                //InitializeComponent();  
        }

        [WebMethod]
        public   string   HelloWorld()   {
                return   "Hello   World ";
        }
        [WebMethod]
          public   int   add(int   a,   int   b)
        {
                return   a   +   b;
        }
}


//*****************************************
//**************************************
//wdsl导入的代码
//   ************************************************************************   //
//   The   types   declared   in   this   file   were   generated   from   data   read   from   the
//   WSDL   File   described   below:
//   WSDL           :   http://10.20.10.224/servTest.asmx?wsdl
//   Encoding   :   utf-8
//   Version     :   1.0
//   (2007-7-27   下午   04:48:35   -   1.33.2.5)
//   ************************************************************************   //

unit   servTest;

interface

uses   InvokeRegistry,   SOAPHTTPClient,   Types,   XSBuiltIns;

type

    //   ************************************************************************   //
    //   The   following   types,   referred   to   in   the   WSDL   document   are   not   being   represented
    //   in   this   file.   They   are   either   aliases[@]   of   other   types   represented   or   were   referred
    //   to   but   never[!]   declared   in   the   document.   The   types   from   the   latter   category
    //   typically   map   to   predefined/known   XML   or   Borland   types;   however,   they   could   also  
    //   indicate   incorrect   WSDL   documents   that   failed   to   declare   or   import   a   schema   type.
    //   ************************************************************************   //
    //   !:string                     -   "http://www.w3.org/2001/XMLSchema "
    //   !:int                           -   "http://www.w3.org/2001/XMLSchema "




    //   ************************************************************************   //
    //   Namespace   :   http://tempuri.org/
    //   soapAction:   http://tempuri.org/%operationName%
    //   transport   :   http://schemas.xmlsoap.org/soap/http
    //   binding       :   servTestSoap
    //   service       :   servTest
    //   port             :   servTestSoap
    //   URL               :   http://10.20.10.224/servTest.asmx
    //   ************************************************************************   //
    servTestSoap   =   interface(IInvokable)
    [ '{957BE824-F977-FA3E-7C45-3850D8EF663E} ']
        function     HelloWorld:   WideString;   stdcall;
        function     add(const   a:   Integer;   const   b:   Integer):   Integer;   stdcall;
    end;

function   GetservTestSoap(UseWSDL:   Boolean=System.False;   Addr:   string= ' ';   HTTPRIO:   THTTPRIO   =   nil):   servTestSoap;


implementation

function   GetservTestSoap(UseWSDL:   Boolean;   Addr:   string;   HTTPRIO:   THTTPRIO):   servTestSoap;
const
    defWSDL   =   'http://10.20.10.224/servTest.asmx?wsdl ';
    defURL     =   'http://10.20.10.224/servTest.asmx ';
    defSvc     =   'servTest ';
    defPrt     =   'servTestSoap ';
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;
    RIO.HTTPWebNode.UseUTF8InHeader   :=   True;
    try
        Result   :=   (RIO   as   servTestSoap);
        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;


initialization
    InvRegistry.RegisterInterface(TypeInfo(servTestSoap),   'http://tempuri.org/ ',   'utf-8 ');


    InvRegistry.RegisterDefaultSOAPAction(TypeInfo(servTestSoap),   'http://tempuri.org/%operationName% ');

end.  

//*******************************************************************//
//delphi客户端调用的代码写在一个按钮的单击事件里
//*******************************************************************//
procedure   TForm1.Button1Click(Sender:   TObject);
var
      ser   :servTestSoap;
      rStr   :wideString;
      rInt   :integer;
begin
    ser   :=   HTTPRIO1   as   servTestSoap;
    rStr   :=ser.HelloWorld;
      rInt   :=   ser.add(1,   2);
    self.Label1.Caption   :=   IntToStr(rInt);
    ShowMessage(rStr);
end;

[解决办法]
你的HTTPRIO1指定WSDLLocation了没有?


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WebService
{
/// <summary>
/// Service1 的摘要说明。
/// </summary>
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}

#region 组件设计器生成的代码

//Web 服务设计器所必需的
private IContainer components = null;

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}

#endregion

// WEB 服务示例
// HelloWorld() 示例服务返回字符串 Hello World
// 若要生成,请取消注释下列行,然后保存并生成项目
// 若要测试此 Web 服务,请按 F5 键

[WebMethod]
public string HelloWorld()
{
return "Hello World ";
}

[WebMethod]
public string add(int a, int b)
{
int i;
i = a+b;
return i.ToString();
}
[WebMethod]
public int add1(int a, int b)
{
int i;
i = a+b;
return i;
}
}
}

导入wsdl................


--
var
ss:Service1Soap;
RIO : THTTPRIO;
begin
RIO:= THTTPRIO.Create( Owner );
ss:=RIO as Service1Soap;
RIO.WSDLLocation:= 'http://localhost/WebService/Service1.asmx?wsdl ';
showmessage( ss.add(1,2) );
showmessage( IntToStr( ss.add1(1,2) ) );
end;


正常的一塌糊涂!~~~~~~~~~
[解决办法]
建议用D2007的 WSDL IMPORTER装入 VS写的WEB SERVICE
D2006以下WSDL貌似都有乱码 和传入数据出问题的BUG
我以前有个程序就是折腾了半天

热点排行