客户端delphi使用XML与中间层C#.net通信的例子,客户端用idhttp的post发送请求
- 客户端代码:
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
- IdHTTP, StdCtrls;
- type
- TForm1 = class(TForm)
- IdHTTP1: TIdHTTP;
- Button1: TButton;
- Button2: TButton;
- Memo1: TMemo;
- procedure Button2Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.Button2Click(Sender: TObject);
- var
- respsm:TStringStream;
- AList:TStrings;
- uri:string;
- begin
- uri:='http://localhost:4983/WebSite3/Default.aspx';//这个地址换成自己的响应地址
- AList:=TStringList.Create;
- {中文乱码问题解决方法:发送过去之前先转码UTF8Encode,接收的时候再解码UTF8Decode}
- AList.Add(UTF8Encode(Memo1.Text));
- respsm:=TStringStream.Create('');
- //IdHTTP1.Get('http://localhost:4983/WebSite3/Default.aspx?id=3',sm);
- IdHTTP1.Request.ContentType:='application/x-www-form-urlencoded';
- try
- IdHTTP1.Post(uri,AList,respsm);
- finally
- ShowMessage(UTF8Decode(respsm.DataString));//respsm.DataString为中间层返回的数据
- end;
- respsm.Free;
- AList.Free;
- end;
- end.
客户端memo1.Text的内容为(在设计界面直接编辑memo1.lines,输入以下内容):
- <?xml version="1.0" encoding="utf-8" ?>
- <erp>
- <head>
- </head>
- <body>
- <client job="RoleInfo_New">
- <main job="New" ID="" Name="测试角色3" />
- </client>
- </body>
- </erp>
中间层代码:(Default.aspx.cs)
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- //添加以下命名空间
- using System.Xml;
- using System.Data.Sql;
- using System.Data.SqlClient;
- using System.IO;
- using System.Text;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Stream sm = Request.InputStream;
- sm.Position = 0;
- byte[] buff = new byte[sm.Length];
- //char[] buff = new char[sm.Length];
- int len = (int)sm.Length;
- sm.Read(buff, 0, len);
- //StreamReader sr = new StreamReader(sm);
- //sr.Read(buff, 0, len);
- string str = Encoding.UTF8.GetString(buff);
- str = Server.UrlDecode(str);
- string ID = null;
- string[] AttrList = new string[2];
- XmlDocument dom = new XmlDocument();
- dom.LoadXml(str);
- XmlNodeList xnl = dom.GetElementsByTagName("client");
- if (xnl[0].Attributes[0].Value.ToString().StartsWith("RoleInfo_"))
- {
- if (xnl[0].Attributes[0].Value.ToString().EndsWith("New"))
- {
- for (int i = 0; i < AttrList.Length; i++)
- {
- AttrList[i] = xnl[0].ChildNodes[0].Attributes[i + 1].Value;
- }
- SqlConnection conn = new SqlConnection("server=.;database=test;uid=xj;pwd=123456");
- conn.Open();
- SqlCommand comm = new SqlCommand();
- comm.Connection = conn;
- comm.CommandText = "insert into RoleInfo(Name) values(@Name);select @@identity as id;";
- comm.Prepare();
- comm.Parameters.Add("@Name", SqlDbType.VarChar, 50);
- comm.Parameters[0].Direction = ParameterDirection.Input;
- comm.Parameters[0].Value = AttrList[1];
- //取刚插入数据库的记录的ID
- DataSet ds = new DataSet();
- SqlDataAdapter sda = new SqlDataAdapter(comm);
- sda.Fill(ds);
- comm.Dispose();
- conn.Close();
- conn.Dispose();
- ID = ds.Tables[0].Rows[0][0].ToString();
- //插入成功将ID返回给客户端
- Response.Clear();
- Response.ContentType = "text/xml";
- Response.Write("<?xml version=/"1.0/" encoding=/"utf-8/" ?>");
- Response.Write("<erp>");
- Response.Write("<head />");
- Response.Write("<body>");
- Response.Write("<server>");
- Response.Write("<main job=/"New/" ID=/"" + ID.ToString() + "/" />");
- Response.Write("</server>");
- Response.Write("</body>");
- Response.Write("</erp>");
- }
- }
- }
- }