一、首先建立Http的服务端,此示例的寄宿体为WindowsService,以下代码仅为WCF Restful服务代码,不包括服务启动和安装代码
1.服务契约
1 /// <summary> 2 /// TEST 3 /// </summary> 4 [ServiceContract(Name = "IInSideContract_EnterpriseLibrary")] 5 public interface IInSideContract_TEST 6 { 7 /// <summary> 8 /// Post获取方式测试 9 /// </summary> 10 /// <param name="num1"></param> 11 /// <returns></returns> 12 [OperationContract(Name = "ElibPostTest1")] 13 [WebInvoke(Method = "POST", UriTemplate = "ElibPostTest1", 14 BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Xml)] 15 string ElibPostTest1(string num1); 16 17 }
2.服务实现
1 /// <summary> 2 /// TEST 3 /// </summary> 4 public class EnterpriseLibrary:IInSideContract_TEST 5 { 6 /// <summary> 7 /// Post获取方式测试 8 /// </summary> 9 /// <param name="num1"></param> 10 /// <returns></returns> 11 public string ElibPostTest1(string num1) 12 { 13 return num1; 14 } 15 }
3.配置文件
配置Http的服务
1 <system.serviceModel> 2 <services> 3 <service name="Services.InSideService_Test" 4 behaviorConfiguration="GetPostBehavior"> 5 <endpoint address="" binding="webHttpBinding" bindingConfiguration="webBindingNoneSecurityPublic" 6 behaviorConfiguration="GetPostEndBehaviors" 7 contract="Contracts.IInSideContract_EnterpriseLibrary"> 8 <identity> 9 <dns value="localhost" /> 10 </identity> 11 </endpoint> 12 <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> 13 <host> 14 <baseAddresses> 15 <add baseAddress="http://localhost:9999/InSideService_EnterpriseLibrary" /> 16 </baseAddresses> 17 </host> 18 </service> 19 <bindings> 20 <webHttpBinding> 21 <binding name="webBindingNoneSecurityPublic" closeTimeout="00:10:00" openTimeout="00:10:00" 22 receiveTimeout="01:00:00" sendTimeout="01:00:00" allowCookies="false" 23 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 24 maxBufferPoolSize="999999999" maxReceivedMessageSize="999999999" 25 useDefaultWebProxy="false"> 26 <readerQuotas maxDepth="32" maxStringContentLength="999999999" maxArrayLength="999999999" 27 maxBytesPerRead="999999999" maxNameTableCharCount="999999999" /> 28 <security mode="None" /> 29 </binding> 30 </webHttpBinding> 31 </bindings> 32 <webHttpBinding> 33 <binding name="webBindingNoneSecurityPublic" closeTimeout="00:10:00" openTimeout="00:10:00" 34 receiveTimeout="01:00:00" sendTimeout="01:00:00" allowCookies="false" 35 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 36 maxBufferPoolSize="999999999" maxReceivedMessageSize="999999999" 37 useDefaultWebProxy="false"> 38 <readerQuotas maxDepth="32" maxStringContentLength="999999999" maxArrayLength="999999999" 39 maxBytesPerRead="999999999" maxNameTableCharCount="999999999" /> 40 <security mode="None" /> 41 </binding> 42 </webHttpBinding> 43 <behaviors> 44 <serviceBehaviors>
二、建立POST调用WCF Restful服务的客户端
1.客户端代码
1 static void Main(string[] args) 2 { 3 string url = "http://localhost:9999/InSideService_EnterpriseLibrary/ElibPostTest1"; 4 5 try 6 { 7 8 XmlDocument doc = new XmlDocument(); 9 doc.Load(@"D:工作目录TEST estConsoleApplication2inDebugXMLFile1.xml"); 10 byte[] bytes = Encoding.UTF8.GetBytes(doc.InnerXml); 11 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 12 request.Method = "POST"; 13 request.ContentLength = bytes.Length; 14 request.ContentType = "application/xml"; 15 Stream reqstream = request.GetRequestStream(); 16 reqstream.Write(bytes, 0, bytes.Length); 17 request.Timeout = 900000; 18 request.Headers.Set("Pragma", "no-cache"); 19 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 20 Stream streamReceive = response.GetResponseStream(); 21 Encoding encoding = Encoding.UTF8; 22 StreamReader streamReader = new StreamReader(streamReceive, encoding); 23 string strResult = streamReader.ReadToEnd(); 24 streamReceive.Dispose(); 25 streamReader.Dispose(); 26 Console.WriteLine(strResult); 27 } 28 catch (Exception ex) 29 { 30 Console.WriteLine(ex.Message); 31 } 32 Console.Read(); 33 }
2.Xml参数
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!--方法名、服务命名空间--> 3 <ElibPostTest1 xmlns="http://tempuri.org/"> 4 <!--参数名称--> 5 <num1>123456789012345678901234</num1> 6 </ElibPostTest1>
3.调用结果