HttpListener 和web.config的关系?

mnm35 2007-02-09 09:22:11
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Net;
using System.Collections;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{

string strTmp = "http://*:8010/";
string strTmp1 = "http://*:8010/Login/";
string[] Prefixes = new string[] { strTmp, strTmp1 };

SimpleListenerExample(Prefixes);
}

// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
// URI prefixes are required,
// for example "http://contoso.com:8080/index/".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");

// Create a listener.
HttpListener listener = new HttpListener();
listener.AuthenticationSchemeSelectorDelegate = new AuthenticationSchemeSelector(AuthenticationSchemeForClient);

// Add the prefixes.
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
listener.Start();
Console.WriteLine("Listening...");
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
listener.Stop();
}

static AuthenticationSchemes AuthenticationSchemeForClient(HttpListenerRequest request)
{
Console.WriteLine("Client authentication protocol selection in progress...");

// Do not authenticate local machine requests.
if (request.RawUrl!="/")
{
return AuthenticationSchemes.Anonymous;
}
else
{
return AuthenticationSchemes.IntegratedWindowsAuthentication;
}
}
}
}
我的意思
1.http://*:8010/"是根目录下需要验证
2.http://*:8010/Login是根目录下不需要验证

但是我想通过设置web.config来设置权限。
我现在是AuthenticationSchemeForClient 方法来设置的 ,和web.config无关




...全文
278 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cao_david 2007-06-18
  • 打赏
  • 举报
回复
上面方法有点错误
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost/suibian/");
listener.Start();
HttpListenerRequest request = listener.GetContext().Request;
HttpListenerResponse response = listener.GetContext().Response;
Uri url = request.Url;
cao_david 2007-06-18
  • 打赏
  • 举报
回复
HttpListener 和web.config的没有关系
HttpListener 主要是监控客户端提交的东西
如果你想对所有的客户端提交的页面做某种处理
如:(现在我在做一个博客,想做URL映射,让地址显示为http://localhost/blog/userName
但我现在真实地址是这样的
http://localhost/blog/index.aspx?id=***)

实现
在web.config <httpModules>中添加
<httpModules>
<add name="HttpListenerModule" type="portal.HttpListenerModule"/>
</httpModules>


namespace portal
{
/// <summary>
/// HttpListenerModule的摘要说明

/// </summary>
public class HttpListenerModule:IHttpModule
{
// IHttpModule::Init
public void Init(HttpApplication app)
{
// 注册管道事件
app.AcquireRequestState +=
new EventHandler(OnAcquireRequestState);
}

// IHttpModule::Dispose
public void Dispose() {}

// 确定是否正在处理 F5 或后退/前进操作
private void OnAcquireRequestState(object sender, EventArgs e) {
// 访问 HTTP 上下文
HttpListener listener = new HttpListener();
HttpListenerRequest request = listener.GetContext().Request;
Uri url = request.url;
//下面就是解析当前的url了应该比较简单我就不说了;
//当然再这里你还可作其它处理。
return;
}
}

}
tanbear2 2007-06-06
  • 打赏
  • 举报
回复
HttpListener这个类到底是起什么作用的?
cat_hsfz 2007-02-09
  • 打赏
  • 举报
回复
你自己实现一个Web Server?建议你看看Cassini这个项目,一个可以脱离IIS独立运行的支持ASP.NET的Web Server。
mnm35 2007-02-09
  • 打赏
  • 举报
回复
大家帮忙顶啊。
chy3503319 2007-02-09
  • 打赏
  • 举报
回复
不懂 帮顶了
wshuangminlg 2007-02-09
  • 打赏
  • 举报
回复
帮LZ顶
mnm35 2007-02-09
  • 打赏
  • 举报
回复
顶....

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧