Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# jQuery发布到.NET HttpListener并再次返回_C#_Javascript_Http - Fatal编程技术网

C# jQuery发布到.NET HttpListener并再次返回

C# jQuery发布到.NET HttpListener并再次返回,c#,javascript,http,C#,Javascript,Http,我有一段javascript代码,它使用jQuery.post将一些数据发送到使用HttpListener的.NET应用程序 以下是js: $.post("http://localhost:8080/catch", { name: "John", time: "2pm" }, function(data) { alert(data); }); 而C#: 别忘了这件事。除非您的javascript托管在http://localhost:8080您将无法将AJAX

我有一段javascript代码,它使用jQuery.post将一些数据发送到使用HttpListener的.NET应用程序

以下是js:

$.post("http://localhost:8080/catch", { name: "John", time: "2pm" },
    function(data) { 
        alert(data);
    });
而C#:

别忘了这件事。除非您的javascript托管在
http://localhost:8080
您将无法将AJAX请求发送到此URL。也不允许使用不同的端口号。您需要在
HTML页面上托管javascript文件http://localhost:8080
如果您想让它起作用。或者让服务器发送,但这仅适用于GET请求


备注:请确保将一次性资源包装到服务器上,以正确处置这些资源,否则服务器可能会开始泄漏网络连接句柄。

我看不到内容类型的设置。将内容类型设置为
text/html

response.ContentType = "text/html";

不要忘记关闭响应来释放资源

对响应调用Close将强制通过底层套接字发送响应,然后处理其所有一次性对象

在您的示例中,Close方法仅在输出流上调用。这将通过套接字发送响应,但不会处理与响应相关的任何资源,其中包括您引用的输出流

// Complete async GetContext and reference required objects
HttpListenerContext Context = Listener.EndGetContext(Result);
HttpListenerRequest Request = Context.Request;
HttpListenerResponse Response = Context.Response;

// Process the incoming request here

// Complete the request and release it's resources by call the Close method
Response.Close();

您可以大大简化代码的编写。只要用这个:

        // Construct a response.
        string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
        context.Response.Write(responseString);
//构造一个响应。
string responseString=“你好,世界!”;
context.Response.Write(responseString);

不需要
输出流
或其他大部分代码。如果您确实有理由使用它,请注意您实际上不应该关闭
OutputStream
。当您使用
Resopnse.OutputStream
时,您正在检索对它的引用,但您没有获得所有权。它仍然属于
响应
对象,并且在请求结束时处理
响应
时将正确关闭。

这不太可能是违规原因。似乎是服务器的位置是的,都是本地的。这个JS实际上是从我正在制作的firefox扩展中执行的。想法是右键单击一个有趣的URL并将其发送到本地机器上运行的进程。我正在尝试自动化一些非常繁琐的web应用程序调试。所以请求确实通过了,我在回调中得到了响应,只是没有任何数据。@LoveMeSomeCode你试过我的吗?@Aliostad是的,到目前为止没有运气。仍在获取回调客户端,但没有数据。我尝试了你的建议,但没有成功。在设置ContentLength 64之前,我将其设置正确。我仍然会触发javascript回调,然后抛出一个错误,因为数据未定义。使用fiddler捕获响应并更新您的问题。好主意,但我安装了fiddler 2,它似乎不适用于localhost。我可以发誓有一个firefox扩展显示了这些东西。我去四处看看,有效载荷是多少?来吧请你把回答粘贴到问题中好吗?好的,我的意思是只需按原样发布数据(所有HTTP响应,包括标题)。如果您在注释中输出,它将丢失所有非常重要的新行字符。
// Complete async GetContext and reference required objects
HttpListenerContext Context = Listener.EndGetContext(Result);
HttpListenerRequest Request = Context.Request;
HttpListenerResponse Response = Context.Response;

// Process the incoming request here

// Complete the request and release it's resources by call the Close method
Response.Close();
        // Construct a response.
        string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
        context.Response.Write(responseString);