当前位置: 首页>>代码示例>>C#>>正文


C# HttpListenerResponse.AppendHeader方法代码示例

本文整理汇总了C#中System.Net.HttpListenerResponse.AppendHeader方法的典型用法代码示例。如果您正苦于以下问题:C# HttpListenerResponse.AppendHeader方法的具体用法?C# HttpListenerResponse.AppendHeader怎么用?C# HttpListenerResponse.AppendHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.HttpListenerResponse的用法示例。


在下文中一共展示了HttpListenerResponse.AppendHeader方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Process

        public override void Process(HttpListenerRequest request, HttpListenerResponse response)
        {
            string[] actionPath = request.RawUrl.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            string action = actionPath[1].ToLower();

            ISongPlayer songPlayer = SongPlayerFactory.GetSongPlayer();
            string requester = GetRequester(request);

            response.AppendHeader("Cache-Control", "no-cache");

            switch (action)
            {
                case "queue":
                    switch (request.HttpMethod)
                    {
                        case "GET":
                            response.ContentType = "application/json";
                            WriteUtf8String(response.OutputStream, JsonConvert.SerializeObject(
                                new
                                {
                                    Queue = songPlayer.PlayQueue.ToList(),
                                    PlayerStatus = songPlayer.PlayerStatus,
                                    Self = requester
                                }
                            ));
                            break;
                        case "POST":
                            if (!ClientAllowed(requester))
                                return;

                            using (var reader = new StreamReader(request.InputStream))
                            {
                                songPlayer.Enqueue(reader.ReadToEnd(), requester);
                            }
                            break;
                        case "DELETE":
                            if (!ClientAllowed(requester))
                                return;

                            using (var reader = new StreamReader(request.InputStream))
                            {
                                songPlayer.Dequeue(reader.ReadToEnd(), requester);
                            }
                            break;
                    }
                    break;
                case "playlist":
                    {
                        if (!ClientAllowed(requester))
                            return;

                        if (request.HttpMethod == "POST")
                        {
                            using (var reader = new StreamReader(request.InputStream))
                            {
                                string posted = reader.ReadToEnd();
                                var playlistRequest = JsonConvert.DeserializeAnonymousType(posted, new { Filter = string.Empty, Page = 0, SortBy = "artist", Ascending = true });

                                Song[] songs = songPlayer.GetPlayList(
                                    playlistRequest.Filter,
                                    playlistRequest.SortBy,
                                    playlistRequest.Ascending
                                ).ToArray();

                                response.ContentType = "application/json";
                                WriteUtf8String(response.OutputStream, JsonConvert.SerializeObject(
                                    new
                                    {
                                        TotalPageCount = (songs.Length + (_pageSize - 1)) / _pageSize,
                                        CurrentPage = playlistRequest.Page,
                                        SongsForCurrentPage = songs.Skip((playlistRequest.Page - 1) * _pageSize).Take(_pageSize).ToArray(),
                                        SortBy = playlistRequest.SortBy,
                                        Ascending = playlistRequest.Ascending
                                    }
                                ));
                            }
                        }
                        break;
                    }
                case "next":
                    if (!ClientAllowed(requester))
                        return;

                    response.ContentType = "application/json";
                    songPlayer.Next(requester);
                    break;
                case "rescan":
                    if (!ClientAllowed(requester))
                        return;

                    response.ContentType = "application/json";
                    songPlayer.Rescan();
                    ImageHelper.Purge();
                    break;
                case "pause":
                    if (!ClientAllowed(requester))
                        return;

                    response.ContentType = "application/json";
//.........这里部分代码省略.........
开发者ID:Daspeed,项目名称:SongRequest,代码行数:101,代码来源:DynamicHandler.cs

示例2: ServeMvcResponse

		static bool ServeMvcResponse(HttpListenerResponse httpResponse, HttpResponse mvcResponse)
		{
			var status = mvcResponse.Status;
			httpResponse.StatusCode = status;
			httpResponse.ContentType = mvcResponse.ContentType;
			httpResponse.RedirectLocation = mvcResponse.RedirectUrl;

			// headers
			if (mvcResponse.Headers.Count > 0)
			{
				foreach (var header in mvcResponse.Headers)
				{
					httpResponse.AppendHeader(header.Key as String, header.Value as String);
				}
			}

			// cookies
			var cookies = mvcResponse.Cookies;
			if(cookies != null && cookies.Count > 0)
			{
				foreach (var cookie in cookies.Values)
				{
					var cookieHeader = GetHeaderValue(cookie);
					httpResponse.Headers.Add("Set-Cookie", cookieHeader);
				}
			}

			// body
			var body = mvcResponse.Body;
			if (body != null)
			{
				byte[] buffer;

				var byteArrayBody = body as byte[];                
				if (byteArrayBody != null)
				{
					buffer = byteArrayBody;
				}
				else
				{
					// si no son bytes tratarlo siempre como un string.
					buffer = Encoding.UTF8.GetBytes(body.ToString());
				}

				httpResponse.ContentLength64 = buffer.LongLength;
				httpResponse.OutputStream.Write(buffer, 0, buffer.Length);
			}

			return true;
		}
开发者ID:knocte,项目名称:Sfx.Mvc,代码行数:50,代码来源:AppServer.cs


注:本文中的System.Net.HttpListenerResponse.AppendHeader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。