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


C# MemoryStream.WriteAscii方法代码示例

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


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

示例1: WriteWebSocketUpgradeResponse

        public static int WriteWebSocketUpgradeResponse(WebSocketUpgradeResponse response, MemoryStream ms)
        {
            var bytesWritten = 0;

            bytesWritten += ms.WriteAsciiFormat("HTTP/1.1 {0} {1}\r\n", (int)response.StatusCode, response.StatusDescription);
            bytesWritten += ms.WriteAsciiFormat("Server: {0}\r\n", Server);
            bytesWritten += ms.WriteAsciiFormat("Date: {0}\r\n", DateTime.UtcNow.ToString("R"));

            var acceptResponse = response as WebSocketUpgradeResponse.AcceptUpgradeResponse;
            if (acceptResponse != null)
            {
                var acceptKey = Encoding.UTF8.GetBytes(acceptResponse.RequestKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
                var acceptKeyHash = Convert.ToBase64String(SHA1.Create().ComputeHash(acceptKey));

                bytesWritten += ms.WriteAscii("Connection: upgrade\r\n");
                bytesWritten += ms.WriteAscii("Upgrade: websocket\r\n");
                bytesWritten += ms.WriteAsciiFormat("Sec-WebSocket-Accept: {0}\r\n", acceptKeyHash);
            }
            else // Reject
            {
                bytesWritten += ms.WriteAscii("Connection: close\r\n");
            }

            bytesWritten += ms.Write(CRLF);

            return bytesWritten;
        }
开发者ID:CubeCoders,项目名称:mHttp,代码行数:27,代码来源:HttpResponseWriter.cs

示例2: TestParseHeaders

        public void TestParseHeaders()
        {
            var request = new MemoryStream(8192);
            var buffer = request.GetBuffer();
            var start = 0;

            request.WriteAscii("User-Agent: curl/7.35.0\r\n");
            request.WriteAscii("Host: localhost:8080\r\n");
            request.WriteAscii("Accept: */*\r\n");
            request.WriteAscii("\r\n");

            var headers = new Dictionary<string, string>();
            Assert.IsTrue(RequestParser.TryParseHeaders(buffer, ref start, (int)request.Length, headers.Add));
        }
开发者ID:joongonn,项目名称:mHttp,代码行数:14,代码来源:RequestParserTests.cs

示例3: TestMixedNewLines

        public void TestMixedNewLines()
        {
            var request = new MemoryStream(8192);
            var buffer = request.GetBuffer();
            var start = 0;

            int lineStart, lineEnd;

            request.WriteAscii("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0\n");
            request.WriteAscii("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n");
            request.WriteAscii("Accept-language: en-US;q=0.7,en;q=0.3\n");
            request.WriteAscii("Connection: close\r\n");

            Assert.IsTrue(RequestParser.TryReadLine(buffer, ref start, (int)request.Length, out lineStart, out lineEnd));
            var line = Encoding.ASCII.GetString(buffer, lineStart, lineEnd);
            Assert.AreEqual("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0", line);
        }
开发者ID:joongonn,项目名称:mHttp,代码行数:17,代码来源:RequestParserTests.cs

示例4: TestParseHeader

        public void TestParseHeader()
        {
            var request = new MemoryStream(8192);
            var buffer = request.GetBuffer();
            var start = 0;

            int lineStart, lineEnd;

            request.WriteAscii("Host : localhost:8080\r\n");
            Assert.IsTrue(RequestParser.TryReadLine(buffer, ref start, (int)request.Length, out lineStart, out lineEnd));

            string name, value;
            RequestParser.ParseHeader(buffer, lineStart, lineEnd, out name, out value);
            Assert.AreEqual("Host", name);
            Assert.AreEqual("localhost:8080", value);
        }
开发者ID:joongonn,项目名称:mHttp,代码行数:16,代码来源:RequestParserTests.cs

示例5: TestParseRequestLine1

        public void TestParseRequestLine1()
        {
            var request = new MemoryStream(8192);
            var buffer = request.GetBuffer();
            var start = 0;

            int lineStart, lineEnd;

            request.WriteAscii("POST /accounts?flag=x HTTP/1.1\r\n");
            Assert.IsTrue(RequestParser.TryReadLine(buffer, ref start, (int)request.Length, out lineStart, out lineEnd));

            Method method;
            string path, query, version;

            RequestParser.ParseRequestLine(buffer, lineStart, lineEnd, out method, out path, out query, out version);
            Assert.AreEqual(Method.POST, method);
            Assert.AreEqual("/accounts", path);
            Assert.AreEqual("?flag=x", query);
            Assert.AreEqual("HTTP/1.1", version);
        }
开发者ID:joongonn,项目名称:mHttp,代码行数:20,代码来源:RequestParserTests.cs

示例6: TestTryReadLine

        public void TestTryReadLine()
        {
            var request = new MemoryStream(8192);
            var buffer = request.GetBuffer();
            var start = 0;

            int lineStart, lineEnd;

            Assert.IsFalse(RequestParser.TryReadLine(buffer, ref start, (int)request.Length, out lineStart, out lineEnd));

            request.WriteAscii("\r\n");
            Assert.IsTrue(RequestParser.TryReadLine(buffer, ref start, (int)request.Length, out lineStart, out lineEnd));

            request.WriteAscii("GET /");
            Assert.IsFalse(RequestParser.TryReadLine(buffer, ref start, (int)request.Length, out lineStart, out lineEnd));

            request.WriteAscii(" HTTP/1.1\r\nUser-Agent: ");
            Assert.IsTrue(RequestParser.TryReadLine(buffer, ref start, (int)request.Length, out lineStart, out lineEnd));

            Assert.AreEqual(Encoding.ASCII.GetString(buffer, lineStart, lineEnd - lineStart), "GET / HTTP/1.1");
        }
开发者ID:joongonn,项目名称:mHttp,代码行数:21,代码来源:RequestParserTests.cs

示例7: TestTryParseHttpPostRequest

        public void TestTryParseHttpPostRequest()
        {
            var request = new MemoryStream(8192);
            var buffer = request.GetBuffer();
            var start = 0;

            request.WriteAscii("POST /accounts/create HTTP/1.1\r\n");
            request.WriteAscii("Host: localhost:8080\r\n");
            request.WriteAscii("Content-Length: 23\r\n");
            request.WriteAscii("\r\n");
            request.WriteAscii(@"{ ""username"" : ""test"" }");

            var state = new HttpRequest(RemoteEndPoint, false);
            HttpRequest httpRequest;
            Assert.IsTrue(RequestParser.TryParseHttpRequest(buffer, ref start, (int)request.Length, state, out httpRequest));
            Assert.AreEqual(Method.POST, httpRequest.Method);
            Assert.AreEqual("http://localhost:8080/accounts/create", httpRequest.Url.AbsoluteUri);

            var requestBody = httpRequest.Body.ReadToEnd();
            Assert.AreEqual(@"{ ""username"" : ""test"" }", requestBody);
        }
开发者ID:joongonn,项目名称:mHttp,代码行数:21,代码来源:RequestParserTests.cs

示例8: TestTryParseHttpGetRequest

        public void TestTryParseHttpGetRequest()
        {
            var request = new MemoryStream(8192);
            var buffer = request.GetBuffer();
            var start = 0;

            request.WriteAscii("GET /index.jsp HTTP/1.1\r\n");
            request.WriteAscii("User-Agent: curl/7.35.0\r\n");
            request.WriteAscii("Host: localhost:8080\r\n");
            request.WriteAscii("Accept: */*\r\n");

            var state = new HttpRequest(RemoteEndPoint, false);
            HttpRequest httpRequest;
            Assert.IsFalse(RequestParser.TryParseHttpRequest(buffer, ref start, (int)request.Length, state, out httpRequest));

            request.WriteAscii("\r\n");
            Assert.IsTrue(RequestParser.TryParseHttpRequest(buffer, ref start, (int)request.Length, state, out httpRequest));
            Assert.AreEqual(Method.GET, httpRequest.Method);
            Assert.AreEqual("http://localhost:8080/index.jsp", httpRequest.Url.AbsoluteUri);
            Assert.AreEqual("curl/7.35.0", httpRequest.Headers["User-Agent"]);
        }
开发者ID:joongonn,项目名称:mHttp,代码行数:21,代码来源:RequestParserTests.cs


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