Tag: httplistener

非管理员的HttpListenerException“访问被拒绝”

我编写了一个使用HttpListener监听HTTP请求的C#应用​​程序 – 显然! 我使用的命名空间前缀也使用netsh为当前用户注册(如SO上的每个人所建议的)。 问题是尽管使用netsh我的应用程序仍然会为非管理员用户抛出“访问被拒绝”例外。 操作系统是Windows 7。 更新:当我使用非管理员用户运行它时,好像我的应用程序没有执行netsh命令。 我的代码有问题吗? 没有exception抛出。 AddAddress(“http://localhost:8400/”, Environment.UserDomainName, Environment.UserName); HttpListener _listener = new HttpListener(); _listener.Prefixes.Add(“http://localhost:8400/”); _listener.Start(); … /** method stolen from an SO thread. sorry can’t remember the author **/ static void AddAddress(string address, string domain, string user) { string args = string.Format(@”http add urlacl url={0}”, address) + ” user=\”” + […]

支持https的http侦听器以c#编码

我发现了很多答案如何将httplistener设置为使用HTTPS,但每个解决方案都需要使用命令行。 我想这是最快的方法,但我想编写C#类来处理这个问题。 在旧的解决方案中,我使用了webserver类(在Internet的某个地方找到,我不记得确切的名称),它允许以这种方式添加证书: webserver.Certificate = new X509Certificate2(“MyCert.pfx”, “MyPassword”); 有没有办法用httplistener实现这一点? 从代码显然。 问候。

从HttpListener解析POST参数

假设我有HttpListener。 它听一些端口和IP。 当我发送POST请求时它会抓住它。 我如何从HttpListenerRequest解析POST参数? HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; if ( request.HttpMethod == “POST” ) { // Here i can read all parameters in string but how to parse each one i don’t know }

使用HttpListener提供下载文件时性能不佳

我正在尝试使用C# HttpListener创建一个简单的Web服务器,并提供要下载的文件。 我发现传输速率非常差,特别是与从共享中复制相同文件相比。 这是否为HttpListener所知,可以采取哪些措施来改进它? 这里有一些关于我对这个问题所做研究的其他信息。 在本地连接时下载速率提高了很多,但在这种情况下几乎立即复制文件,因此很难测量差异比率。 然而,当远程连接( LAN环境,彼此相邻的机器)时,传输时间大约是从共享进行简单文件复制的时间的25倍。 似乎没有使用可用的网络带宽来加快速度。 我发现了一些关于HttpListener其他问题和讨论似乎表明了类似的问题,请看这里: HttpListener vs本机性能 HttpListener性能优化 (但这不是关于下载) MSDN文档还声明HttpListener基于http.sys ,允许带宽限制。 可能是这里发生了一些不必要的带宽限制或我的代码有问题吗? 在我测试过的机器上(Windows 7和Windows 2008 R2),没有IIS存在。 在我的示例中,我正在启动一个HttpListener如下所示: HttpListener listener = new HttpListener(); listener.Prefixes.Add(“http://*:80/”); listener.Start(); 这是我简单文件下载的代码: HttpListenerResponse response = null; try { HttpListenerContext context = listener.GetContext(); response = context.Response; using( FileStream fs = File.OpenRead( @”c:\downloadsample\testfile.pdf” ) ) { byte[] buffer = […]

C#:HttpListener错误服务内容

我已经实现了类似的东西,只有真正的区别是 string filename = context.Request.RawUrl.Replace(“/”, “\\”).Remove(0,1); string path = Uri.UnescapeDataString(Path.Combine(_baseFolder, filename)); 这样我就可以遍历子目录了。 这适用于网页和其他文本文件类型,但在尝试提供媒体内容时,我得到了例外 HttpListenerException:由于线程退出或应用程序请求,I / O操作已中止 其次是 InvalidOperationException:在写入所有字节之前无法关闭流。 在using语句中。 有关如何处理或停止这些例外的任何建议? 谢谢

C#中的HTTP代理服务器

我的公司正在尝试使用.NET Fx 3.5和C#编写代理服务器。 从我们的研究中我已经读过HttpListener不是代理服务器的好候选者,虽然我不确定为什么。 我们目前正在使用Mentalis代理示例源代码,但其中包括实现我们自己的日志记录和性能计数器。 使用HttpListener将包装Http.sys,这将为我们提供一些开箱即用的性能统计信息。 那么为什么HttpListener是HTTP代理工作的不良候选者呢? (是的,我们通过编写或配置ICAP服务器来考虑Squid 3.1。)

HttpListener服务器头文件c#

我正在尝试为个人项目编写C#http服务器,我想知道如何将返回的服务器头从Microsoft-HTTPAPI / 2.0更改为其他内容? public class HttpWebServer { private HttpListener Listener; public void Start() { Listener = new HttpListener(); Listener.Prefixes.Add(“http://*:5555/”); Listener.Start(); Listener.BeginGetContext(ProcessRequest, Listener); Console.WriteLine(“Connection Started”); } public void Stop() { Listener.Stop(); } private void ProcessRequest(IAsyncResult result) { HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); string responseString = “Hello World”; byte[] buffer = Encoding.UTF8.GetBytes(responseString); context.Response.ContentLength64 = […]

Mono HttpListener客户端证书

我使用HttpListener创建了一个服务器。 它在没有SSL的情况下工作得很好,但是使用SSL时会发生奇怪 我已经使用httpcfg安装了证书,甚至使用我自己的程序,它安装正确,监听器启动并提供HTTPS请求,但始终要求提供客户端证书。 它不会发生在Windows / .net上,只有Linux / mono(我使用的是ver 3.4.0)并且非常烦人,我不希望每次尝试登录客户端时都会询问用户证书。 这是单声道错误还是有任何方法可以禁用客户端证书协商? 谢谢。

如何使用HttpListener同时处理多个连接?

在我构建的应用程序中,需要能够同时为多个客户端提供服务的Web服务器。 为此,我使用HttpListener对象。 使用Async方法\事件BeginGetContext和EndGetContext 。 在委托方法中,有一个监听器再次开始监听的呼叫,它主要起作用。 提供的代码是我在这里和那里找到的代码混合,以及模拟数据处理瓶颈的延迟。 问题是,它只在最后一个连接服务之后开始管理下一个连接..对我没用。 public class HtServer { public void startServer(){ HttpListener HL = new HttpListener(); HL.Prefixes.Add(“http://127.0.0.1:800/”); HL.Start(); IAsyncResult HLC = HL.BeginGetContext(new AsyncCallback(clientConnection),HL); } public void clientConnection(IAsyncResult res){ HttpListener listener = (HttpListener)res.AsyncState; HttpListenerContext context = listener.EndGetContext(res); HttpListenerRequest request = context.Request; // Obtain a response object. HttpListenerResponse response = context.Response; // Construct a […]

如何刷新HttpListener响应流?

HttpListener为您提供响应流,但调用flush意味着什么(来自消息来源很清楚,因为它实际上什么都不做)。 挖掘HTTP API内部显示这是HttpListener本身的限制。 任何人都知道如何刷新HttpListener的响应流(可能是reflection或额外的P / Invokes)? 更新:如果您没有刷新选项或定义缓冲区大小的能力,则无法对任何内容进行http流。