標籤:des http io os ar for 資料 sp div
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);
}
}
c# 和 delphi 的 base64 編碼一致問題 webservice