web api HttpResponseMessage的简单使用

本文详细介绍了在C# Web API中如何使用HttpResponseMessage来构建和返回HTTP响应,包括设置状态码、内容类型以及传输JSON数据等。适合前端开发者和C#后端开发者了解API交互过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using Lemon.Common;
using Lemon.WeChat.Model;
using Lemon.WeChat.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Senparc.Weixin.MP;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.Http;
using System.Xml;

namespace Lemon.WeChat.WebApi
{
    /// <summary>
    /// 微信回调
    /// </summary>
    [RoutePrefix("wechat/WeCallback")]
    public class WeCallbackController : ApiController
    {
        private readonly string Token = "lemon95";
        /// <summary>
        /// 构造函数
        /// </summary>
        public WeCallbackController()
        {
            Token = WebConfigurationManager.AppSettings["WeixinToken"] ?? Token;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <param name="echostr"></param>
        [Route("")]
        public HttpResponseMessage Get(string signature, string timestamp, string nonce, string echostr)
        {
            Logger.Info(string.Format("参数:signature:{0},timestamp:{1},nonce:{2},echostr:{3}", signature, timestamp, nonce, echostr));
            string returnstr = string.Empty;
            if (CheckSignature.Check(signature, timestamp, nonce, Token))
            {
                returnstr = echostr;
            }
            else
            {
                returnstr = "验证失败";
            }

            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(returnstr, Encoding.UTF8, "application/x-www-form-urlencoded")
            };
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        [Route("")]
        public HttpResponseMessage Post(string signature, string timestamp, string nonce)
        {
            Logger.Info(string.Format("参数:signature:{0},timestamp:{1},nonce:{2}", signature, timestamp, nonce));

            string returnstr = string.Empty;
            try
            {
                if (!CheckSignature.Check(signature, timestamp, nonce, Token))
                {
                    returnstr = "验证失败";
                }
                else
                {
                    var requestContent = Request.Content.ReadAsStreamAsync().Result;
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(requestContent);
                    Logger.Info(xmlDoc.InnerXml);
                    WeCalbackService svr = new WeCalbackService();
                    returnstr = svr.Callback(xmlDoc);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("WeCallbackController[Post]发生异常。", ex);
            }

            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(returnstr, Encoding.UTF8, "application/x-www-form-urlencoded")
            };
            return result;
        }

        /// <summary>
        /// 回调通知
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("Notify")]
        public HttpResponseMessage Notify()
        {
            string returnstr = string.Empty;
            try
            {
                var requestContent = Request.Content.ReadAsStreamAsync().Result;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(requestContent);
                WeCalbackService svr = new WeCalbackService();
                returnstr = svr.Notify(xmlDoc);
            }
            catch (Exception ex)
            {
                Logger.Error("WeCallbackController[Notify]发生异常。", ex);
            }
            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(returnstr, Encoding.UTF8, "application/x-www-form-urlencoded")
            };
            return result;

        }

       
    }
}

 

转载于:https://www.cnblogs.com/zoro-zero/p/5420974.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值