WCF Restful Post調用

1、首先創建Http的服務端,此示例的寄宿體爲WindowsService,如下代碼僅爲WCF Restful服務代碼,不包括服務啓動和安裝代碼web

1.服務契約app

 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.服務實現ide

 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>

 

2、創建POST調用WCF Restful服務的客戶端url

1.客戶端代碼spa

 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\test\ConsoleApplication2\bin\Debug\XMLFile1.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參數code

1 <?xml version="1.0" encoding="utf-8" ?>
2 <!--方法名、服務命名空間-->
3 <ElibPostTest1 xmlns="http://tempuri.org/">
4 <!--參數名稱-->
5   <num1>123456789012345678901234</num1>
6 </ElibPostTest1>

3.調用結果
orm

相關文章
相關標籤/搜索