wcf webHttpBinding Post

wcf webHttpBinding Post 大數據量提交 ios c#客戶端

 

一直在爲安卓和蘋果寫服務,剛開始的時候全用的Get請求用url傳全部的參數,因爲url長度的限制大的數據量html

沒法傳到服務器,提交圖片什麼的就更不用說了,後來用的POST請求,記錄一下,作了一個很簡單的小例子:ios

c#源代碼提供你們下載 10Solution4.rarweb

ios代碼 IOSHttpPostDemo.rarc#

 開始作吧!服務器

1建一個空工程Solution4,添加一個類庫WcfSerDemo引入app

System.ServiceModel  和System.ServiceModel.Web兩個類庫post

建一個接口IMyWcfServerMethod大數據

?
[ServiceContract]
public  interface  IMyWcfServerMethod
{    
     [OperationContract, WebInvoke(Method = "POST" , UriTemplate = "postMostStr/{streamLength}" , BodyStyle = WebMessageBodyStyle.Bare)]
     string  postMostStr( string  streamLength, Stream getStream);
 
     //實驗一下接收
     [OperationContract, WebInvoke( Method= "GET" , UriTemplate = "postText" )]
     string  postText();
}

咱們只用 postMostStr這個方法它是以POST形式接收的Url只要一個參數就是傳過來的byte的長度url

那個Stream類型的臺數就是Post過來的數據spa

建一個類實現這個接口MyWcfServerMethodFace

?
public  class  MyWcfServerMethodFace:IMyWcfServerMethod
     {
         public  string  postMostStr( string  streamLength, System.IO.Stream getStream)
         {
             try
             {  
                 int  byLength=Convert.ToInt32(streamLength);
                 byte [] resultByte = new  byte [byLength];
                 getStream.Read(resultByte, 0, resultByte.Length); //把接收的stream寫到resultByte
                 Console.WriteLine(Encoding.UTF8.GetString(resultByte)); //把收到的數據輸出到控制檯
 
             }
             catch  (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
             Console.WriteLine( "success!" );
             return  "success!" ;
         }
 
         public  string  postText()
         {
             Console.WriteLine( "text!" );
             return  "text success" ;
            
         }
     }

好了契約就寫好了,宿主程序就用控制檯程序

加一個控制程序

引入System.ServiceModel 和 WcfSerDemo

配製App.config

?
<?xml version= "1.0"  encoding= "utf-8"  ?>
<configuration>
   <system.serviceModel>
     <bindings>
       <webHttpBinding><!--這個是接收大數據加的-->
         <binding name= "webBinding"  maxBufferPoolSize= "2147483647"  maxBufferSize= "2147483647"  maxReceivedMessageSize= "2147483647" >
           <readerQuotas maxDepth= "2147483647"  maxStringContentLength= "2147483647"
                         maxArrayLength= "2147483647"  maxBytesPerRead= "2147483647"
                         maxNameTableCharCount= "2147483647" />
         </binding>
       </webHttpBinding>     
     </bindings>
 
     <behaviors>
       <serviceBehaviors>
         <behavior name= "mySerBeh" >                  
           <serviceMetadata httpGetUrl= "mex"  httpGetEnabled= "true" />                      
           <serviceDebug httpHelpPageEnabled= "true"  includeExceptionDetailInFaults= "false" />
         </behavior>
       </serviceBehaviors>
 
       <endpointBehaviors>
         <behavior name= "webHttpendBehavior" >
           <webHttp></webHttp>
         </behavior>
       </endpointBehaviors>
     </behaviors>
 
     <services>
       <service name= "WcfScrverDemo.MyWcfServerMethodFace"  behaviorConfiguration= "mySerBeh" >
         <host>
           <baseAddresses>
             <add baseAddress= "http://LocalHost:7789/MySericeDemo" />
           </baseAddresses>
         </host>
         <endpoint address= ""  binding= "webHttpBinding"  bindingConfiguration= "webBinding"  contract= "WcfScrverDemo.IMyWcfServerMethod"  behaviorConfiguration= "webHttpendBehavior" ></endpoint>
       </service>
     </services>
 
   </system.serviceModel>
</configuration>

在定app.config的時候要注意大小寫

讓這個服務跑起來吧在控件臺應用程序 的Main里加上

using System.ServiceModel;
using WcfScrverDemo;

?
<br><br><br><br><br> static  void  Main( string [] args)
         {
             
             try
             {
                 using  (ServiceHost host = new  ServiceHost( typeof (MyWcfServerMethodFace)))
                 {
                     host.Opened += delegate
                     {
                         Console.WriteLine( "server running...." );
                     };
                     host.Open();
                     Console.ReadLine();
 
                 }
             }
             catch  (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
                 Console.ReadLine();
             }
         }

服務就行了,運行一下吧

下面就開使定客戶端了

咱們也用控件臺應用程序來作客戶端:

建一個客戶端程序WcfwebHttpClient

回爲用的是webHttpBinding咱們能夠直接傳url就ok了,不用配製app.config

很簡單的在Main里加上

?
static  void  Main( string [] args)
         {
             try
             {
                 string  strPost = "小妞,給大爺笑一個,不笑,那大爺給你笑一個,我和超人惟一的區別就是我把內褲穿裏邊了."  +
                                  "個人飯量你是知道的,並且我也不愛吃烤鴨,因此吃了四隻我就吃不下去了,我就說:實在"  +
                                  "不能吃了,待會兒回家還要吃飯呢。天堂這兒還有一牌子:天堂周圍四百米嚴禁擺攤!上帝"  +
                                  "坐那正抽菸呢。上帝說,必須好好招待,這麼些年好容易有個說相聲的上了天堂了。一巴掌"  +
                                  "寬護心毛,還紋一條帶魚。《<span>金 梅</span>》裏唐僧取經那會兒…… 三十多歲沒結婚,北京的媒婆"  +
                                  "界就轟動了!騎着脖子拉屎,拉乾的我撥下去,拉稀的我擦乾了,但是他,騎着脖子拉痢疾!"  +
                                  "說相聲講究說學逗唱,當你有口吃就不能說相聲。,好比你報時,若是你有口吃就出現這樣狀況"  +
                                  "。如今播報北....北...北京...時.. 時....時間...七....七..七點,靠,我一看錶都八點半了  " ;
                 byte [] buffer = Encoding.UTF8.GetBytes(strPost);
                 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create( "http://LocalHost:7789/MySericeDemo/postMostStr/"  + buffer.Length);
                 request.Method = "POST" ;
                 request.ContentType = "text/plain" ;
 
                 request.ContentLength = buffer.Length;
                 Stream requestStram = request.GetRequestStream();
                 requestStram.Write(buffer, 0, buffer.Length);
                 requestStram.Close();
 
                 Stream getStream = request.GetResponse().GetResponseStream();
 
                 byte [] resultByte = new  byte [200];
                 getStream.Read(resultByte, 0, resultByte.Length);
 
                 Console.WriteLine(Encoding.UTF8.GetString(resultByte));
             }
             catch  (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
             Console.ReadLine();
         }

  跑一下看看什麼效果吧

看一看服務有輸出 嗎

 

看一看客戶端有沒有反回

再看一下ios的代碼

?
NSString *myString= @"郭:新年新氣象,在這裏咱們表明512的全體室員向你們拜年了,新年快樂,謝謝各位的捧場,支持於:過年好郭:小孩兒們喜歡過年,都盼着過年,歲數大了就這麼回事,那會兒來講穿新衣服、吃好吃的如今對咱們來講沒這個概念於:如今不盼着這個了郭:小時候盼着給紅包,給錢,十塊二十塊錢,樂的跟什麼似的於:那就很多啦郭:那天在寢室聊天,他們家親戚多於:人多郭:一到春節對他來講收穫的季節到了,家裏人來了,親戚來了,朋友來了,謙兒呢?於:都找我郭:呦呦呦,有長個兒了,掏出來了郭:給呀,碎錢於:我靜弄點零錢了合着,還挨一句罵,您着把它連起來講成不成啊" ;
     NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat: @"http://10.0.3.5:7789/MySericeDemo/postMostStr/%d" ,[[myString dataUsingEncoding:NSUTF8StringEncoding] length]]]];
     [request setHTTPMethod: @"POST" ];
     [request setValue: @"text/plain"  forHTTPHeaderField: @"Content-type" ];
     
     
     
     [request setValue:[NSString stringWithFormat: @"%d" ,[myString length]]  forHTTPHeaderField: @"Content-length" ];
     
     [request setHTTPBody:[myString dataUsingEncoding:NSUTF8StringEncoding]];
     
     [NSURLConnection connectionWithRequest:request delegate :self];

  

 

 

好了,簡單的小例子就ok了

相關文章
相關標籤/搜索