c# 和 delphi 的 base64 编码一致问题 webservice

c# 和 delphi 的 base64 编码一致问题 webservice

因为项目须要为包括c/s和b/s的多个平台提供统一业务逻辑,写个webservice来完成这个任务,经过webservice封装业务逻辑,为其余平台提供接口,以供调用,因而用delphi写一个webservice,起初没有任何采用任何编码,固然在调用的时候delphi客户端能够正常传输数据,c#网页部分调用倒是乱码,确定是两种语言的编码方式问题,引发的乱码,缘由有了,最终也没有想到解决的办法。因此想两端提供一种统一的编码,经过编码和解码来达到编码方式的统一,这样应该就不会有乱码的问题了吧,我选择base64编码方式,base64在网络上良好的性能,这是知足这个所须要的,又折腾了一阵子,终于完成了,各个平台数据的统一,测试代码以下:

1 delphi部分:web

unit Unit1;c#

interface网络

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,EncdDecd;性能

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;测试

var
Form1: TForm1;this

implementation编码

{$R *.dfm}spa

procedure TForm1.Button1Click(Sender: TObject);
var s:AnsiString;
begin
s:=edit1.text;
edit2.Text:= EncodeString((s));code

// 超越软件 http://www.cyhlw.com
end;orm

procedure TForm1.Button2Click(Sender: TObject);
begin
edit3.Text:=DecodeString(edit2.text)
end;

end.

2c# 部分:

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

//Response.ContentEncoding = System.Text.Encoding.GetEncoding( "GB2312" );


}
protected void Button1_Click(object sender, EventArgs e)
{

string str="";


localhost.IIHelloservice oserver = new localhost.IIHelloservice();
str=oserver.sayHello("超越软件 http://www.cyhlw.com");
//localhost.WebService1 oService = new localhost.WebService1();
//Label1.Text = oService.About();
this.TextBox1.Text = str;

byte[] bytes = System.Convert.FromBase64String(str);
str = "";
str = Encoding.Default.GetString(bytes);
TextBox2.Text = str;
//TextBox1.Text=Encoder.get
//byte[] bytes
//this.TextBox1.Text = System.Convert.FromBase64String(str);

}}

相关文章
相关标签/搜索