static void Main(string[] args) { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-protobuf")); var response = httpClient.GetAsync("http://localhost:60339/api/Default/GetItem").Result; //把 ProtoBuf Stream 反序列化成 集合 var obj = (RuntimeTypeModel.Default).Deserialize(response.Content.ReadAsStreamAsync().Result, null, typeof(List<Item>)) as List<Item>; Console.WriteLine(obj.Count); //設置請求頭 var content = new ObjectContent<List<Item>>(obj, new ProtoBufFormatter()); content.Headers.ContentType = new MediaTypeHeaderValue("application/x-protobuf"); var postResponse1= httpClient.PostAsync("http://localhost:60339/api/Default/PostItem", content).Result; var postResult = postResponse1.Content.ReadAsStreamAsync().Result; var intValue = (RuntimeTypeModel.Default).Deserialize(postResult, null, typeof(int)); Console.WriteLine(intValue); Console.ReadKey(); }
使用Fiddler發送Accept 分別為Json,Xml,Protobuf三種格式來請求數據
git
https://github.com/xlb378917466/WebApi_protobuf