客户端delphi使用XML与中间层C#.net通信的例子,客户端用idhttp的post发送请求

  1. 客户端代码:
  1. unit Unit1;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  6.   IdHTTP, StdCtrls;
  7. type
  8.   TForm1 = class(TForm)
  9.     IdHTTP1: TIdHTTP;
  10.     Button1: TButton;
  11.     Button2: TButton;
  12.     Memo1: TMemo;
  13.     procedure Button2Click(Sender: TObject);
  14.   private
  15.     { Private declarations }
  16.   public
  17.     { Public declarations }
  18.   end;
  19. var
  20.   Form1: TForm1;
  21. implementation
  22. {$R *.dfm}
  23. procedure TForm1.Button2Click(Sender: TObject);
  24. var
  25.   respsm:TStringStream;
  26.   AList:TStrings;
  27.   uri:string;
  28. begin
  29.   uri:='http://localhost:4983/WebSite3/Default.aspx';//这个地址换成自己的响应地址
  30.   AList:=TStringList.Create;
  31.   {中文乱码问题解决方法:发送过去之前先转码UTF8Encode,接收的时候再解码UTF8Decode}
  32.   AList.Add(UTF8Encode(Memo1.Text));
  33.   respsm:=TStringStream.Create('');
  34.   //IdHTTP1.Get('http://localhost:4983/WebSite3/Default.aspx?id=3',sm);
  35.   IdHTTP1.Request.ContentType:='application/x-www-form-urlencoded';
  36.   try
  37.     IdHTTP1.Post(uri,AList,respsm);
  38.   finally
  39.     ShowMessage(UTF8Decode(respsm.DataString));//respsm.DataString为中间层返回的数据
  40.   end;
  41.   respsm.Free;
  42.   AList.Free;
  43. end;
  44. end.

客户端memo1.Text的内容为(在设计界面直接编辑memo1.lines,输入以下内容):

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <erp>
  3. <head>
  4. </head>
  5. <body>
  6. <client job="RoleInfo_New">
  7. <main job="New" ID="" Name="测试角色3" />
  8. </client>
  9. </body>
  10. </erp>

中间层代码:(Default.aspx.cs)

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. //添加以下命名空间
  11. using System.Xml;
  12. using System.Data.Sql;
  13. using System.Data.SqlClient;
  14. using System.IO;
  15. using System.Text;
  16. public partial class _Default : System.Web.UI.Page 
  17. {
  18.     protected void Page_Load(object sender, EventArgs e)
  19.     {
  20.         Stream sm = Request.InputStream;
  21.         sm.Position = 0;
  22.         byte[] buff = new byte[sm.Length];
  23.         //char[] buff = new char[sm.Length];
  24.         int len = (int)sm.Length;
  25.         sm.Read(buff, 0, len);
  26.         //StreamReader sr = new StreamReader(sm);
  27.         //sr.Read(buff, 0, len);
  28.         string str = Encoding.UTF8.GetString(buff);
  29.         str = Server.UrlDecode(str);
  30.         string ID = null;
  31.         string[] AttrList = new string[2];
  32.         XmlDocument dom = new XmlDocument();
  33.         dom.LoadXml(str);
  34.         XmlNodeList xnl = dom.GetElementsByTagName("client");
  35.         if (xnl[0].Attributes[0].Value.ToString().StartsWith("RoleInfo_"))
  36.         {
  37.             if (xnl[0].Attributes[0].Value.ToString().EndsWith("New"))
  38.             {
  39.                 for (int i = 0; i < AttrList.Length; i++)
  40.                 {
  41.                     AttrList[i] = xnl[0].ChildNodes[0].Attributes[i + 1].Value;
  42.                 }
  43.                 SqlConnection conn = new SqlConnection("server=.;database=test;uid=xj;pwd=123456");
  44.                 conn.Open();
  45.                 SqlCommand comm = new SqlCommand();
  46.                 comm.Connection = conn;
  47.                 comm.CommandText = "insert into RoleInfo(Name) values(@Name);select  @@identity as id;";
  48.                 comm.Prepare();
  49.                 
  50.                 comm.Parameters.Add("@Name", SqlDbType.VarChar, 50);
  51.                 comm.Parameters[0].Direction = ParameterDirection.Input;
  52.                 comm.Parameters[0].Value = AttrList[1];
  53.                 //取刚插入数据库的记录的ID
  54.                 DataSet ds = new DataSet();
  55.                 SqlDataAdapter sda = new SqlDataAdapter(comm);
  56.                 sda.Fill(ds);
  57.                 comm.Dispose();
  58.                 conn.Close();
  59.                 conn.Dispose();
  60.                 ID = ds.Tables[0].Rows[0][0].ToString();
  61.                 //插入成功将ID返回给客户端
  62.                 Response.Clear();
  63.                 Response.ContentType = "text/xml";
  64.                 Response.Write("<?xml version=/"1.0/" encoding=/"utf-8/" ?>");
  65.                 Response.Write("<erp>");
  66.                 Response.Write("<head />");
  67.                 Response.Write("<body>");
  68.                 Response.Write("<server>");
  69.                 Response.Write("<main job=/"New/" ID=/"" + ID.ToString() + "/" />");
  70.                 Response.Write("</server>");
  71.                 Response.Write("</body>");
  72.                 Response.Write("</erp>");
  73.                     
  74.             }
  75.         }        
  76.     }
  77. }
posted @ 2011-10-27 15:32  许明吉博客  阅读(2199)  评论(0编辑  收藏  举报