- Client code:
- 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 '; // replace this address with your own response address.
- Alist: = tstringlist. Create;
- {Chinese garbled problem solution: transcoding utf8encode before sending, decoding utf8decode when receiving}
- 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 is the data returned by the middle layer.
- End;
- Respsm. Free;
- Alist. Free;
- End;
- End.
The content of the client memo1.text is (edit memo1.lines directly on the design page and enter the following content ):
- <? XML version = "1.0" encoding = "UTF-8"?>
- <ERP>
- <Head>
- </Head>
- <Body>
- <Client job = "roleinfo_new">
- <Main job = "new" id = "" name = "test role 3"/>
- </Client>
- </Body>
- </ERP>
Middle Layer Code: (default. aspx. CS)
- UsingSystem;
- UsingSystem. Data;
- UsingSystem. configuration;
- UsingSystem. Web;
- UsingSystem. Web. Security;
- UsingSystem. Web. UI;
- UsingSystem. Web. UI. webcontrols;
- UsingSystem. Web. UI. webcontrols. webparts;
- UsingSystem. Web. UI. htmlcontrols;
- // Add the following namespace
- UsingSystem. xml;
- UsingSystem. Data. SQL;
- UsingSystem. Data. sqlclient;
- UsingSystem. IO;
- UsingSystem. text;
- PublicPartialClass_ Default: system. Web. UI. Page
- {
- Protected VoidPage_load (ObjectSender, eventargs E)
- {
- Stream Sm = request. inputstream;
- SM. Position = 0;
- Byte[] Buff =New Byte[Sm. Length];
- // Char [] buff = new char [Sm. Length];
- IntLen = (Int) Sm. length;
- SM. Read (buff, 0, Len );
- // Streamreader sr = new streamreader (SM );
- // Sr. Read (buff, 0, Len );
- StringSTR = encoding. utf8.getstring (buff );
- STR = server. urldecode (STR );
- StringId =Null;
- String[] Attrlist =New String[2];
- Xmldocument dom =NewXmldocument ();
- 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(IntI = 0; I <attrlist. length; I ++)
- {
- Attrlist [I] = xnl [0]. childnodes [0]. attributes [I + 1]. value;
- }
- Sqlconnection conn =NewSqlconnection ("Server =.; database = test; uid = XJ; Pwd = 123456 ");
- Conn. open ();
- Sqlcommand comm =NewSqlcommand ();
- 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];
- // Retrieve the ID of the record just inserted into the database
- Dataset DS =NewDataset ();
- Sqldataadapter SDA =NewSqldataadapter (Comm );
- SDA. Fill (DS );
- Comm. Dispose ();
- Conn. Close ();
- Conn. Dispose ();
- Id = Ds. Tables [0]. Rows [0] [0]. tostring ();
- // Return the ID to the client after successful insertion
- Response. Clear ();
- Response. contenttype = "text/XML ";
- Response. Write ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> ");
- Response. Write ("<ERP> ");
- Response. Write ("
- Response. Write ("<body> ");
- Response. Write ("<Server> ");
- Response. Write ("<main job =/" New/"id =/" "+ id. tostring () +"/"/> ");
- Response. Write ("</Server> ");
- Response. Write ("</body> ");
- Response. Write ("</ERP> ");
- }
- }
- }
- }
Default. aspx code (add validaterequest = "false "):
- <% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" validaterequest = "false" %>