Rest wcf allows us to use HttpRequest for simple interaction when calling Web Services. Can the old version of WebService be implemented? Through HttpContext. Current. Rquest/Response in WebService, we can also modify WebMethod.
Client:
Copy codeThe Code is as follows:
// Create a DataTable
DataTable data = new DataTable ("Project ");
Data. Columns. Add ("Name ");
Data. Columns. Add ("Birthday ");
Data. Rows. Add (new object [] {"Wendy", "1978/03/11 "});
Data. Rows. Add (new object [] {"Philip", "2000/11/05 "});
Data. Rows. Add (new object [] {"Felix", "maid "});
Using (var MS = new MemoryStream ())
{
// Write the DataTable into the stream in Xml format
Data. WriteXml (MS, XmlWriteMode. WriteSchema );
Var client = new WebClient ();
// Define Content-Type (xml, json, etc.) of HttpRequest)
Client. Headers. Add ("Content-Type", "text/xml ");
Var url = "http: // localhost: 2609/Service1.asmx/SendXml ";
// Send HttpRequest
Var resp = client. UploadData (url, "POST", ms. ToArray ());
Var strResp = System. Text. Encoding. UTF8.GetString (resp );
MessageBox. Show (strResp );
}
Server:
Copy codeThe Code is as follows:
[WebMethod]
Public void SendXml ()
{
// Obtain the RAW HttpRequest Client
Var inputStream = HttpContext. Current. Request. InputStream;
// Define the format returned by Response: Json
Var response = HttpContext. Current. Response;
Response. ContentType = "text/json ";
// Var strXml = "";
// Using (var sr = new StreamReader (inputStream ))
// StrXml = sr. ReadToEnd ();
Try
{
DataTable data = new DataTable ();
Using (var xr = XmlReader. Create (inputStream ))
Data. ReadXml (xr );
// Return the number of rows of the DataTable that reads Xml to the client
String count = "/" "+ data. Rows. Count + "/"";
Response. BinaryWrite (System. Text. Encoding. UTF8.GetBytes (count ));
}
Catch (Exception ex)
{
Response. BinaryWrite (System. Text. Encoding. UTF8.GetBytes (ex. Message ));
}
}
Client output "" 3 ""
PS: if the client's HttpRequest meets the SOAP serialization format, WebService deserializes the message into a parameter for WebMethod. The client proxy class is also used by deserializing messages into objects.