日期:2014-05-17 浏览次数:20833 次
[ServiceContract(NameSpace="UserInfo")]
public interface IUser
{
[OperationContract]
[WebInvoke(UriTemplate="/AddUser",Method="POST",ResponseFormat=WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedRequest)]
string Add(UserAccount account);
}
[DataContract]
public class UserAccount
{
[DataMember]
public int userId{get;set;}
......
}
public string Add(UserAccount account)
{
return account.name+"-"+account.age;
}
<system.ServiceModel>
<services>
<service name="Services1" behaviorConfiguration="webHttpBehaviors">
<endpoint contract="IServices1" binging="webHttpBinding" />
</service>
</services>
</system.ServiceModel>
DataContractJsonSerializer serializer=new DataContractJsonSerializer(typeof(UserAccount));
using(MemoryStream stream=new MemoryStream())
{
UserAccount account=new UserAccount{userId=1,......};
serializer.WriteObject(stream,account);
stream.Flush();
json=Encoding.UTF8.GetString(stream.toArray(),0,stream.Length);
client.Headers["Content-Type"]="application/json";
client.Encoding=Encoding.UTF8;
try
{
string res=client.UploadString(url,"POST",json);
}
catch
{
...
}
}