我用C#编写了一个简单的测试WebService,UTF-8编码
[WebMethod]
public string Echo(string inStr)
{
return "你传入的是:" + inStr;
}
当我用Delphi调用的时候,为何传入汉字,返回的是乱码?
即使我用 UTF8Encode('传入的测试字符串') 来编码,
为何WebService端返回的是乱码呢?那位大侠知道,请告诉我。
[WebMethod]
public string Echo(string inStr)
{
return "你传入的是:" + inStr;
}
当我用Delphi调用的时候,为何传入汉字,返回的是乱码?
即使我用 UTF8Encode('传入的测试字符串') 来编码,
为何WebService端返回的是乱码呢?那位大侠知道,请告诉我。
解决方案 »
- 帮忙翻译代码
- help!! 用AdoQuery复制Access数据库记录问题
- 谁会制做组合控件用生成新控件的方法.
- 在98下编译后拷贝到2000下运行,系统找不到qtintf.dll。如何解?如果您第一个解决,50分。
- 请问如何使用InstallShield Express - Borland Limited Edition
- 我也来谈谈:--李维&Borland技术大会有感(北京)
- 关于BATCHMOVE控件的问题!!
- 我想把上次保存的color在下次打开程序时再用,怎么保存到ini文件中?(不知道也来看看,up有分)
- 请问在console Application 中怎么实现命令行?
- 如何判断电脑是否连入Ineternet?
- rave报表 打印预览 死循环,高手门帮忙看下(在线等...)
- 如何把StringGrid所见即所得的样式直接打印出来?
[WebService(Name="SmartLinkTest",Description="xxxxxxTest", Namespace = "xxxxxx")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TestService : System.Web.Services.WebService
{
public TestService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
} [WebMethod(MessageName="GetString",Description ="Get String Hello World.")]
public string GetString()
{
return "Hello World,世界,你好";
}
[WebMethod(MessageName="GetInt",Description ="Get Int 1001.")]
public int GetInt()
{
return 1001;
}
[WebMethod(MessageName = "GetInput", Description = "Get String that u Input in Box.")]
public string GetInput(string strInput)
{
return strInput;
}
}
function GetxxxxxxTestSoap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): SmartLinkTestSoap;
const
defWSDL = 'http://localhost/xxxxxx/Service.asmx?WSDL';
defURL = 'http://localhost/xxxxxx/Service.asmx';
defSvc = 'xxxxxxTest';
defPrt = 'xxxxxxTestSoap';
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 SmartLinkTestSoap);
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(xxxxxxTestSoap), 'xxxxxx', 'utf-8');
InvRegistry.RegisterDefaultSOAPAction(TypeInfo(xxxxxxTestSoap), 'xxxxxx/%operationName%');end. procedure TFSmartLinkTestService.Button1Click(Sender: TObject);
var
sTmp1,sTmp2:Widestring;begin
showmessage(httprio1.WSDLLocation );
edit1.text:=(httprio1 as xxxxxxTestSoap).GetString ;
sTmp1:=edit1.text;
//showmessage(inttostr((httprio1 as xxxxxxTestSoap).GetInt));
//sTmp2:=(httprio1 as xxxxxxTestSoap).GetInput(sTmp1);
//showmessage(sTmp2);
end;在xp下調試通過.
InvRegistry.RegisterInterface(TypeInfo(xxxxxxTestSoap), 'xxxxxx', 'utf-8');
這句別寫錯了.
(1) THTTPRIO-------:使用Http消息来调用远程使用SOAP的接口对象 (2) THTTPReqResp---:给服务器发送一个SOAP消息, THTTPReqResp在可调用接口上执行一个方法请求. (3) TOPToSoapDomConvert ----:TOPToSoapDomConvert处理Soap方法请求的组合与分发 (4) TSoapConnection:TSoapConnection在客户端的多层数据库应用中创建并维护客户端和作为WebService 的远程应用服务器间的连接 (5) THTTPSoapDispatcher -----:通过将Soap消息转发给调用者,THTTPSoapDispatcher对Soap消息做出响应. (6) TWSDLHTMLPublish -----:TWSDLHTMLPublish发布描叙webService应用的Wsdl文档 (7) THTTPSoapPascalInvoker -----:THTTPSoapPascalInvoker解释Soap的请求信息,并执行相应的可调用接口. *********************************************************************************************** WSDL 说明: (Web Services Description Language) 规范是一个描述接口,语义以及Web服务为了响应请求需要经常处理的工作的XML文档。这将使 简单地服务方便,快速地被描述和记录。以下是一个WSDL的样例: My first service 它包含了以下的关键信息: 消息的描述和格式定义可以通过XML文档中的和 标记来传送。 标记中表示了消息传送机制。 (e.g. request-only, request-response, response-only) 。 标记指定了编码的规范 。 标记中表示服务所处的位置 (URL)。 WSDL在UDDI中总是作为一个接口描述文档。因为UDDI是一个通用的用来注册WSDL规范的地方,UDDI的规范并不限制任何类型或者格式描述文档 。这些文档可能是一个WSDL文档,或者是一个正规的包含导向文档的Web页面,也可能只是一个包含联系信息的电子邮件地址。现在Java提供了一个 Java API for WSDL (JWSDL)规范。它提供了一套能快速处理WSDL文档的方法,并且不用直接对XML文档进行操作,它会比 JAXP更方便,更快速。 ***********************************************************************************************SOAP说明 (Simple Object Access Protocal)。 我们先简单了解WebServices运作模式。服务端生成服务描述文件WSDL(Web Services Description Language,类似COM的Interface Description Language),如果接收客户端的SOAP请求消息(XML格式数据),解析其调用和参数,根据WSDL和WSML(Web Services Meta Language,Microsoft为描述WSDL里提供的方法与实现该方法的COM对象之间的映射关系而特地设立),调用相应COM对象完成指定功能,并返回 到SOAP消息遣至用户;客户端取得并解析服务端的服务描述文件从而得知服务端的服务内容及调用方式,生成SOAP请求消息(指定调用的方法 名称和参数)送往服务端。 *********************************************************************************************** TReomtable说明 : WebService中自定义类都是继承自该类 //自己定义一个继承自TRemotable的类 (注意只有属性定义,没有方法) TSpeciesInfo = class(TRemotable) private FLength: Integer; FSpeciesName: string; FCategory: string; FCommonName: string; FmoreInfo: string; published property Category: string read FCategory write FCategory; property CommonName: string read FCommonName write FCommonName; property SpeciesName: string read FSpeciesName write FSpeciesName; property Length: Integer read FLength write FLength; property MoreInfo: string read FmoreInfo write FMoreInfo; end; TSOAPAttachment说明 :一种Soap的流类型数据.跟平时的integer等等一个意思 //比如 (1):服务器端建立一个流输出(result:) GrapStream:=TMemoryStream.create; try SoapAttachment:=TSoapAttachment.create; TBolbField(frmdm.adoqury.fields[0]).SaveToStream(GrapStream); SoapAttachment.SetSourceStream(GraphStream,soReference); result:=SoapAttachment; except if assigned(GrapStream) then GrapStream.free; if assigned(SoapAttchament) then SoapAttchment.free; end (2):客户端接受一个流输入 var src:string; begin if not Assigned(SoapAttachment) then exit; Src:=SoapAttachment.CacheFile; .... 例子,基于Soap和webService的分布式网络程序实例 //编写服务器端 --ok 1:File --->New---->Other----->WebService--->Soap WebService Application 后生成3个组件 一个是调用者组件:THTTPSoapPascalInvoker:完成Soap消息和.... 一个是调度者组件:THTTPSoapDispatcher:自动响应送入的soap消息,并且把他们传递给调用者,可以使用他的 WebDispatch属性来识别应用程序响应的http请求.包括设置PathInfo属性来指明指向应用 程序的任何URL的路径部分,设置MethodType属性来指明请求消息的方法头. 一个是WSDL发布者:TWSDLHTMLPublisher:WSDL发布者发布描叙接口以及如何调用他们的wsdl文档,使用非delphi 编写的客户端程序也可以调用WebService应用程序. 2:在接口中添加方法和在实现该接口的类中实现该方法. (1):接口中自定义的类都是继承自TRemotable类,该类只能有属性,没有方法 (2): 3:将生成的Dll文件拷贝到 C:\InetPub\Scripts目录下面. //编写客户端 --ok (1):File--->New--->other---->WebSerice--->Wsdl Importer: 对话框1:WsDl Source:输入==>http://服务器名/scripts/应用程序名/wsdl/相应接口 ,然后Next下步.然后点finish (2):添加一个普通的窗体,上面放WebService中的控件:HTTPRIO,编写它的OnAfterExecute ,OnBeforeExecute事件 连接服务:http://server/script/MyWebService.dll/Soap/ImyWeb ***请注意是Soap/接口 FAttachService为WebService的接口 if FAttachService = nil then begin FAttachService := GetiMyWeb(False, EditUrl.Text); (FAttachService as IRIOAccess).RIO.OnBeforeExecute := HTTPRIO1BeforeExecute; (FAttachService as IRIOAccess).RIO.OnAfterExecute := HTTPRIO1AfterExecute; end; Result := FAttachService; ******************************************************************************* (HTTPRIO1 as ITest).TestFunction(aParam, bParam);