pre { /*控制代码不换行*/ white-space: pre; word-wrap: normal; }

WCF中REST使用POST方式

  1. [OperationContract]  
  2.        [WebInvoke(Method = "POST",ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json,UriTemplate = "data")]  
  3.        string PostData(User u);  

 

  1. public string PostData(User u)  
  2.       {  
  3.             
  4.          return "Received: " + u.name+" " +u.title;  
  5.       }  


客户端:

    1. private void button3_Click(object sender, EventArgs e)  
    2.       {  
    3.           string json = null;  
    4.           WebClient client = new WebClient();  
    5.           client.Headers["Content-Type"] = "application/json";  
    6.   
    7.           DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(User));  
    8.           using (MemoryStream stream = new MemoryStream())  
    9.           {  
    10.               serializer.WriteObject(stream, new User { name="suzhi",age=12,title="uu"});  
    11.               stream.Flush();  
    12.               json = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length);  
    13.   
    14.           }  
    15.           client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);  
    16.           client.UploadStringAsync(new Uri("http://192.168.15.236:7117/data"), "POST", json);  
    17.       }  
    18.       void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)  
    19.       {  
    20.           if (e.Error == null)  
    21.           {  
    22.               MessageBox.Show("OK");  
    23.           }  
    24.           else  
    25.           {  
    26.               MessageBox.Show(e.Error.Message);  
    27.           }  
    28.   
    29.       } 
    30. 转自:http://blog.csdn.net/suzhi921/article/details/7104376
posted @ 2013-11-01 18:14  monkey's  阅读(503)  评论(0)    收藏  举报