您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Delphi >> Delphi调用C#写的WebService的老问题

Delphi调用C#写的WebService的老问题

来源:网络整理     时间:2016/8/29 2:52:53     关键词:

关于网友提出的“ Delphi调用C#写的WebService的老问题”问题疑问,本网通过在网上对“ Delphi调用C#写的WebService的老问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: Delphi调用C#写的WebService的老问题
描述:

我在Delphi调用C#写的Webservice的时候出现了这样一个问题,就是我传汉字参数到服务端的时候会变成乱码.
比如我传入:快快乐乐500年快快乐乐500年,服务端写进数据库里的竟然是:快快乐乐500年快快乐?00年
注:
1:以前我发现传汉字的过去的时候会变成乱码所以我在传参数的时候会有首尾各加一个字符,到服务端再去掉,就上面那个参数我传进去的其实是'c快快乐乐500年快快乐乐500年c'.
2:我已经设置了HttpRIO.HTTPWebNode.UseUTF8InHeader:=True;
3:在接口文件中我也填加了InvRegistry.RegisterInvokeOptions(TypeInfo(WebServiceSoap),ioDocument);不填加好像传过去的参数都成空值了.
4:我试了把HttpRio.Converter.Encoding设置成utf-8,gb2312也一样不行.
5:发现有人汉字后面跟数字就不会变成乱码,而有的会.
希望高手们能帮帮我,谢谢.


解决方案1:

看看web端的字符编码格式

解决方案2:

刚才测试了一下可能是服务程序(C#程序)没有处理好。
C# Web Service程序代码

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Text;
namespace WebService3
{
    /// 
    /// Summary description for Service1
    /// 

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World,其中有中文字7个,ha ha!";
        }
        [WebMethod]
        public void  UploadString(String Value )
        {
            
            FileStream F = new FileStream("c:\\test.txt", FileMode.CreateNew );
            Byte[] Bs;
            Bs = Encoding.GetEncoding(0).GetBytes(Value);
            F.Write(Bs, 0, Bs.GetLength(0));
            F.Close();
        }
    }
}

Delphi SOAP单元代码

unit Service1;
interface
uses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;
const
  IS_OPTN = $0001;
  IS_REF  = $0080;
type
  Service1Soap = interface(IInvokable)
  ['{CFE467D4-A39E-2BD2-5836-7685A9E27F8D}']
    function  HelloWorld: WideString; stdcall;
    procedure UploadString(const Value: WideString); stdcall;
  end;
function GetService1Soap(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): Service1Soap;
implementation
  uses SysUtils;
function GetService1Soap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): Service1Soap;
const
  defWSDL = 'http://localhost:21583/Service1.asmx?wsdl';
  defURL  = 'http://localhost:21583/Service1.asmx';
  defSvc  = 'Service1';
  defPrt  = 'Service1Soap';
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 Service1Soap);
    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(Service1Soap), 'http://tempuri.org/', 'utf-8');
  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(Service1Soap), 'http://tempuri.org/%operationName%');
  InvRegistry.RegisterInvokeOptions(TypeInfo(Service1Soap), ioDocument);
end.

Delphi调用代码
procedure TForm1.Button1Click(Sender: TObject);
begin
GetService1Soap.UploadString('快快乐乐500年快快乐乐500年');
end;

Web Service调用结果(c:\test.txt的内容)
[code=BatchFile]快快乐乐500年快快乐乐500年[/code] 解决方案3:

编码的问题


以上介绍了“ Delphi调用C#写的WebService的老问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3649755.html

相关图片

相关文章