WCF服务端与使用HttpClient的Android客户端简单示例

WCF服务端

Contract
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web;

namespace Contract
{
    /// <summary>
    /// 服务接口
    /// </summary>
    /// <remarks>
    /// WebInvoke - 指示服务操作在逻辑上就是调用操作,而且可由Web编程模型调用
    /// WebGet - 指示服务操作在逻辑上就是检索操作,而且可由Web编程模型调用
    /// UriTemplate - 用于服务操作的统一资源标识符(URI)模版。URI模版可以将一个URI或一组URI映射到服务操作
    /// Method - 与操作关联的写作方法,默认为 POST
    /// ResponseFormat - 指定从服务操作发出的响应的格式。Xml或Json
    /// BodyStyle - 指定body里面的封装
    /// </remarks>
    [ServiceContract]    
    public interface IWcfService
    {
        /// <summary>
        /// GET - 无参数 - 返回值为字符串数组
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        [WebGet(
            ResponseFormat = WebMessageFormat.Json)]
        List<string> GetStudentNames();

        /// <summary>
        /// GET - 无参数 - 返回值为自定义类型的数组
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        [WebGet(
            ResponseFormat = WebMessageFormat.Json)]
        List<Student> GetStudents();
             
        /// <summary>
        /// GET - 一个参数 - 返回值为int
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [OperationContract]
        [WebGet(
            UriTemplate = "GetStudentAge/{name}",   
            //UriTemplate = "GetStudentAge?name={name}",    //默认形式
            ResponseFormat = WebMessageFormat.Json)]
        int GetStudentAge(string name);

        /// <summary>
        /// GET - 多个参数 - 返回值为自定义格式
        /// </summary>
        /// <param name="name"></param>
        /// <param name="birth"></param>
        /// <param name="age"></param>
        /// <returns></returns>
        [OperationContract]
        [WebGet(
            UriTemplate = "CreateStudent/{name}/{birth}/{age}", //UriTemplate中只允许字符串类型的变量
            //UriTemplate = "CreateStudent?name={name}&birth={birth}&age={age}",    //默认形式
            ResponseFormat = WebMessageFormat.Json)]
        Student CreateStudent(string name, string birth, string age);

        /// <summary>
        /// POST - 一个参数 - 返回值为bool
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [OperationContract]
        [WebInvoke(
            UriTemplate = "DeleteStudent",
            Method = "POST",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        bool DeleteStudent(string name);

        /// <summary>
        /// POST - 多个参数 - 返回值为自定义格式
        /// </summary>
        /// <param name="name"></param>
        /// <param name="birth"></param>
        /// <param name="age"></param>
        /// <returns></returns>
        [OperationContract]
        [WebInvoke(
            UriTemplate = "AddStudent",
            Method = "POST",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        Student AddStudent(string name, string birth, int age);

        /// <summary>
        /// POST - 自定义格式参数 - 返回值为string
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        [OperationContract]
        [WebInvoke(
            UriTemplate = "UpdateStudent",
            Method = "POST",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        string UpdateStudent(Student student);
    }

    [DataContract]
    public class Student 
    {
        private string name;
        [DataMember(Order = 0, Name = "name")]
        public string Name 
        {
            get { return name; }
            set 
            { 
                name = HttpUtility.UrlDecode(value, Encoding.UTF8);
            }
        }

      
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值