c# 和 delphi 的 base64 编码一致问题 webservice
由于项目需要为包括c/s和b/s的多个平台提供统一业务逻辑,写个webservice来完成这个任务,通过webservice封装业务逻辑,为其他平台提供接口,以供调用,于是用delphi写一个webservice,起初没有任何采用任何编码,当然在调用的时候delphi客户端可以正常传输数据,c#网页部分调用却是乱码,肯定是两种语言的编码方式问题,引起的乱码,原因有了,最终也没有想到解决的办法。所以想两端提供一种统一的编码,通过编码和解码来达到编码方式的统一,这样应该就不会有乱码的问题了吧,我选择base64编码方式,base64在网络上良好的性能,这是满足这个所需要的,又折腾了一阵子,终于完成了,各个平台数据的统一,测试代码如下:
1 delphi部分:
unit Unit1;
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;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var s:AnsiString;
begin
s:=edit1.text;
edit2.Text:= EncodeString((s));
// 超越软件 http://www.cyhlw.com
end;
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);
}
}