【问题标题】:Return text from output stream in c#从 C# 中的输出流返回文本
【发布时间】:2015-09-28 22:46:30
【问题描述】:

我有一个场景,我需要在 c# 中获取字符串作为输出流。我需要在 javascript 中获取这些字符串值。以下是代码

JS

var timestampUrl : "getTime.aspx" // Used somewhere in plugin. Syntax is fine

最初写为 var timestampUrl : "getTime.php" 但我将其更改为也可以在 .aspx 中使用

getTime.php 代码

<?php
echo time();
?>

getTime.aspx.cs

var ts = ((long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds) / 1000;
Response.ContentType = "text/plain";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(ts.ToString());
Response.OutputStream.Write(bytes, 0, bytes.Length);

但我得到的输出为

1436538190%3C!DOCTYPE%20html%3E%3Chtml%20xmlns=%22http://www.w3.org/1999/xhtml%22%3E%3Chead%3E%3Ctitle%3E%3C/title%3E%3C/head%3E%3Cbody%3E%20%20%20%20%3Cform%20method=%22post%22%20action=%22getTime.aspx%22%20id=%22form1%22%3E%3Cdiv%20class=%22aspNetHidden%22%3E%3Cinput%20type=%22hidden%22%20name=%22__VIEWSTATE%22%20id=%22__VIEWSTATE%22%20value=%22oyHYMIrhkYo9Ho3QkkQWovQ9tbRhQ2wRTHQfGgw4jJwaPeQsTPcZzR1s5K/dHFoH+p82j3XOogiKTqnH0MB/T9K/8kizxTDiLPwKPNWHHp0=%22%20/%3E%3C/div%3E%20%20%20%20%3Cdiv%3E%20%20%20%20%20%20%20%20%3C/div%3E%20%20%20%20%3Cdiv%20class=%22aspNetHidden%22%3E%3Cinput%20type=%22hidden%22%20name=%22__VIEWSTATEGENERATOR%22%20id=%22__VIEWSTATEGENERATOR%22%20value=%22EA09725A%22%20/%3E%3C/div%3E%3C/form%3E%3C!--%20Visual%20Studio%20Browser%20Link%20--%3E%3Cscript%20type=%22application/json%22%20id=%22__browserLink_initializationData%22%3E%20%20%20%20%7B%22appName%22:%22InternetExplorer%22,%22requestId%22:%22f532ae409a044422a3178fdad51fb6a0%22%7D%3C/script%3E%3Cscript%20type=%22text/javascript%22%20src=%22http://localhost:62796/df94eb33421548aca703559a9bdf2a2c/browserLink%22%20async=%22async%22%3E%3C/script%3E%3C!--%20End%20Browser%20Link%20--%3E%3C/body%3E%3C/html%3E

而不仅仅是436538190

可能是什么原因?

【问题讨论】:

    标签: javascript c# outputstream


    【解决方案1】:

    您需要关闭流。

    var ts = ((long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds) / 1000;
    Response.ContentType = "text/plain";
    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(ts.ToString());
    Response.OutputStream.Write(bytes, 0, bytes.Length);
    Response.Flush();
    //now signal the httpapplication to stop processing the request.
    HttpContext.Current.ApplicationInstance.CompleteRequest();
    

    见:issue with Response.OutputStream.Write adding html code in the resulting file

    【讨论】:

    • 我可以使用Response.End 做到这一点。但无论如何,感谢您的指导。如果您愿意,可以编辑您的答案
    猜你喜欢
    • 2011-02-19
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多