WebService 不能传中文 ( UseUTF8InHeader := True 也没用 ) 问题有详细说明(200分)

  • 主题发起人 主题发起人 kgen
  • 开始时间 开始时间
K

kgen

Unregistered / Unconfirmed
GUEST, unregistred user!
C#写的Web Service服务器端
Delphi客户端调用,代码如下:
代码:
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, InvokeRegistry, Rio, SOAPHTTPClient, WSCADB, StdCtrls;
type
  TForm1 = class(TForm)
    HTTPRIO: THTTPRIO;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
  IWS: WSCADBSoap;
begin
  HTTPRIO.HTTPWebNode.UseUTF8InHeader := True;
  IWS := HTTPRIO as WSCADBSoap;
  ShowMessage(IntToStr(IWS.ValidateUser('用户一号', '1')));
end;
end.

这个是接口文件:
unit WSCADB;
interface
uses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;
type
  WSCADBSoap = interface(IInvokable)
  ['{59350E49-A8BA-30DC-6695-B9B1E330B759}']
    function  ValidateUser(const strRegName: WideString;
const strPwd: WideString): Integer;
stdcall;
  end;

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

implementation
function GetWSCADBSoap(UseWSDL: Boolean;
Addr: string;
HTTPRIO: THTTPRIO): WSCADBSoap;
const
  defWSDL = 'http://p4pe/WSCADB/WSCADB.asmx?wsdl';
  defURL  = 'http://p4pe/WSCADB/WSCADB.asmx';
  defSvc  = 'WSCADB';
  defPrt  = 'WSCADBSoap';
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 WSCADBSoap);
    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(WSCADBSoap), 'http://p4pe/', 'utf-8');
  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(WSCADBSoap), 'http://p4pe/ValidateUser');
  InvRegistry.RegisterInvokeOptions(TypeInfo(WSCADBSoap), ioDocument);
end.
使用中文的时候,一调用就会出现这个错误:
Exception class EDOMParseError with message 'End tag 'ValidateUser'do
es not match the start tag 'strRegName'.
Line: 2
<SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http:'. Process TestWS.exe (3144)
但是如果把上面的用户名改成英文的,比如aabb就不会出错了。
[blue]而且服务端下断点,英文的时候会发现跟踪到了,但是使用中文的时候,根本没跟踪到,说明客户端在封包的时候就出错了。[/blue]
工具版本如下:
C# 2003 @ Windows 2000
Delphi 2006 @ Windows XP
请熟悉这方面的高手帮忙
 
把数据转成 Base64传呢!
 
怎么转成Base64,
Delphi有这样的函数吗?
我试过AnsiToUTF8('用户名');
但是这样传到C#里面就是乱码了。
如果转成Base64,C#读取的时候要不要做什么修改?
 
大家帮帮忙啊~
 
Base64 网上可以找到函数,C#也应该用Base64编码
 
非常感谢,问题解决了。
Delphi这边,我用Indy的Base64编码控件;
C#那边用Encoding.Default.GetString(Convert.FromBase64String(str))取得字符串。
这样就搞定了。
真是太感谢了!
 
后退
顶部