一直在為安卓和蘋果寫服務,剛開始的時候全用的Get請求用url傳所有的參數,由於url長度的限制大的數據量
無法傳到服務器,提交圖片什么的就更不用說了,后來用的POST請求,記錄一下,做了一個很簡單的小例子:
c#源代碼提供大家下載 10Solution4.rar
ios代碼 IOSHttpPostDemo.rar
開始做吧!
1建一個空工程Solution4,添加一個類庫WcfSerDemo引入
System.ServiceModel 和System.ServiceModel.Web兩個類庫
建一個接口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的長度
那個Stream類型的台數就是Post過來的數據
建一個類實現這個接口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;
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 = "小妞,給大爺笑一個,不笑,那大爺給你笑一個,我和超人唯一的區別就是我把內褲穿里邊了." +
"我的飯量你是知道的,而且我也不愛吃烤鴨,所以吃了四只我就吃不下去了,我就說:實在" +
"不能吃了,待會兒回家還要吃飯呢。天堂這兒還有一牌子:天堂周圍四百米嚴禁擺攤!上帝" +
"坐那正抽煙呢。上帝說,必須好好招待,這么些年好容易有個說相聲的上了天堂了。一巴掌" +
"寬護心毛,還紋一條帶魚。《金 梅》里唐僧取經那會兒…… 三十多歲沒結婚,北京的媒婆" +
"界就轟動了!騎着脖子拉屎,拉干的我撥下去,拉稀的我擦干了,可是他,騎着脖子拉痢疾!" +
"說相聲講究說學逗唱,當你有口吃就不能說相聲。,比如你報時,如果你有口吃就出現這樣情況" +
"。現在播報北....北...北京...時.. 時....時間...七....七..七點,靠,我一看表都八點半了 ";
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了
c#代碼:10Solution4.rar
ios代碼 IOSHttpPostDemo.rar