The client uses XML to communicate with the intermediate layer C #. net. The client uses the idhttp post to send the request.

Source: Internet
Author: User
  1. Client code:
  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 '; // replace this address with your own response address.
  30. Alist: = tstringlist. Create;
  31. {Chinese garbled problem solution: transcoding utf8encode before sending, decoding utf8decode when receiving}
  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 is the data returned by the middle layer.
  40. End;
  41. Respsm. Free;
  42. Alist. Free;
  43. End;
  44. End.

The content of the client memo1.text is (edit memo1.lines directly on the design page and enter the following content ):

  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 = "test role 3"/>
  8. </Client>
  9. </Body>
  10. </ERP>

Middle Layer Code: (default. aspx. CS)

  1. UsingSystem;
  2. UsingSystem. Data;
  3. UsingSystem. configuration;
  4. UsingSystem. Web;
  5. UsingSystem. Web. Security;
  6. UsingSystem. Web. UI;
  7. UsingSystem. Web. UI. webcontrols;
  8. UsingSystem. Web. UI. webcontrols. webparts;
  9. UsingSystem. Web. UI. htmlcontrols;
  10. // Add the following namespace
  11. UsingSystem. xml;
  12. UsingSystem. Data. SQL;
  13. UsingSystem. Data. sqlclient;
  14. UsingSystem. IO;
  15. UsingSystem. text;
  16. PublicPartialClass_ Default: system. Web. UI. Page
  17. {
  18. Protected VoidPage_load (ObjectSender, 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. IntLen = (Int) Sm. length;
  25. SM. Read (buff, 0, Len );
  26. // Streamreader sr = new streamreader (SM );
  27. // Sr. Read (buff, 0, Len );
  28. StringSTR = encoding. utf8.getstring (buff );
  29. STR = server. urldecode (STR );
  30. StringId =Null;
  31. String[] Attrlist =New String[2];
  32. Xmldocument dom =NewXmldocument ();
  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(IntI = 0; I <attrlist. length; I ++)
  40. {
  41. Attrlist [I] = xnl [0]. childnodes [0]. attributes [I + 1]. value;
  42. }
  43. Sqlconnection conn =NewSqlconnection ("Server =.; database = test; uid = XJ; Pwd = 123456 ");
  44. Conn. open ();
  45. Sqlcommand comm =NewSqlcommand ();
  46. Comm. Connection = conn;
  47. Comm. commandtext = "insert into roleinfo (name) values (@ name); select @ identity as ID ;";
  48. Comm. Prepare ();
  49. Comm. Parameters. Add ("@ name", sqldbtype. varchar, 50 );
  50. Comm. Parameters [0]. Direction = parameterdirection. input;
  51. Comm. Parameters [0]. value = attrlist [1];
  52. // Retrieve the ID of the record just inserted into the database
  53. Dataset DS =NewDataset ();
  54. Sqldataadapter SDA =NewSqldataadapter (Comm );
  55. SDA. Fill (DS );
  56. Comm. Dispose ();
  57. Conn. Close ();
  58. Conn. Dispose ();
  59. Id = Ds. Tables [0]. Rows [0] [0]. tostring ();
  60. // Return the ID to the client after successful insertion
  61. Response. Clear ();
  62. Response. contenttype = "text/XML ";
  63. Response. Write ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> ");
  64. Response. Write ("<ERP> ");
  65. Response. Write ("
  66. Response. Write ("<body> ");
  67. Response. Write ("<Server> ");
  68. Response. Write ("<main job =/" New/"id =/" "+ id. tostring () +"/"/> ");
  69. Response. Write ("</Server> ");
  70. Response. Write ("</body> ");
  71. Response. Write ("</ERP> ");
  72. }
  73. }
  74. }
  75. }

Default. aspx code (add validaterequest = "false "):

  1. <% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" validaterequest = "false" %>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.