本文整理汇总了C#中HttpServer.GetFile方法的典型用法代码示例。如果您正苦于以下问题:C# HttpServer.GetFile方法的具体用法?C# HttpServer.GetFile怎么用?C# HttpServer.GetFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpServer
的用法示例。
在下文中一共展示了HttpServer.GetFile方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Awake
void Awake()
{
var port = 12002;
var addr = "localhost";
var fullUrl = addr + ":" + port;
httpsv = new HttpServer("http://" + fullUrl);
httpsv.RootPath = "./htmlcontents"; // TODO: まともなパスに書き換える
string[] fs = System.IO.Directory.GetFiles(@httpsv.RootPath, "*");
Debug.Log("current path:" + fs[0]);
httpsv.Log.Level = LogLevel.Trace;
httpsv.OnGet += (sender, e) => {
var req = e.Request;
var res = e.Response;
var path = req.RawUrl;
Debug.Log("http request:" + req);
if (path == "/") path += "index.html";
var content = httpsv.GetFile(path);
if (content == null) {
res.StatusCode = (int) HttpStatusCode.NotFound;
res.WriteContent(
System.Text.Encoding.UTF8.GetBytes(
"File Not Found"));
return;
}
if (path.EndsWith(".html")) {
res.ContentType = "text/html";
res.ContentEncoding = Encoding.UTF8;
}
res.WriteContent(content);
};
httpsv.WaitTime = TimeSpan.FromSeconds(2);
httpsv.AddWebSocketService<MyService>("/MyService",() => {
var service = new MyService(recognizedVoices);
return service;
});
httpsv.Start();
Debug.Log("http server started with " + fullUrl);
}
示例2: Main
public static void Main(string[] args)
{
/* Create a new instance of the HttpServer class.
*
* If you would like to provide the secure connection, you should create the instance with
* the 'secure' parameter set to true, or the https scheme HTTP URL.
*/
var httpsv = new HttpServer (4649);
//var httpsv = new HttpServer (5963, true);
//var httpsv = new HttpServer (System.Net.IPAddress.Parse ("127.0.0.1"), 4649);
//var httpsv = new HttpServer (System.Net.IPAddress.Parse ("127.0.0.1"), 5963, true);
//var httpsv = new HttpServer ("http://localhost:4649");
//var httpsv = new HttpServer ("https://localhost:5963");
#if DEBUG
// To change the logging level.
httpsv.Log.Level = LogLevel.Trace;
// To change the wait time for the response to the WebSocket Ping or Close.
httpsv.WaitTime = TimeSpan.FromSeconds (2);
#endif
/* To provide the secure connection.
var cert = ConfigurationManager.AppSettings["ServerCertFile"];
var passwd = ConfigurationManager.AppSettings["CertFilePassword"];
httpsv.SslConfiguration.ServerCertificate = new X509Certificate2 (cert, passwd);
*/
/* To provide the HTTP Authentication (Basic/Digest).
httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic;
httpsv.Realm = "WebSocket Test";
httpsv.UserCredentialsFinder = id => {
var name = id.Name;
// Return user name, password, and roles.
return name == "nobita"
? new NetworkCredential (name, "password", "gunfighter")
: null; // If the user credentials aren't found.
};
*/
// To set the document root path.
httpsv.RootPath = ConfigurationManager.AppSettings["RootPath"];
// To set the HTTP GET method event.
httpsv.OnGet += (sender, e) => {
var req = e.Request;
var res = e.Response;
var path = req.RawUrl;
if (path == "/")
path += "index.html";
var content = httpsv.GetFile (path);
if (content == null) {
res.StatusCode = (int) HttpStatusCode.NotFound;
return;
}
if (path.EndsWith (".html")) {
res.ContentType = "text/html";
res.ContentEncoding = Encoding.UTF8;
}
res.WriteContent (content);
};
// Not to remove the inactive WebSocket sessions periodically.
//httpsv.KeepClean = false;
// To resolve to wait for socket in TIME_WAIT state.
//httpsv.ReuseAddress = true;
// Add the WebSocket services.
httpsv.AddWebSocketService<Echo> ("/Echo");
httpsv.AddWebSocketService<Chat> ("/Chat");
/* Add the WebSocket service with initializing.
httpsv.AddWebSocketService<Chat> (
"/Chat",
() => new Chat ("Anon#") {
Protocol = "chat",
// To ignore the Sec-WebSocket-Extensions header.
IgnoreExtensions = true,
// To validate the Origin header.
OriginValidator = val => {
// Check the value of the Origin header, and return true if valid.
Uri origin;
return !val.IsNullOrEmpty () &&
Uri.TryCreate (val, UriKind.Absolute, out origin) &&
origin.Host == "localhost";
},
// To validate the Cookies.
CookiesValidator = (req, res) => {
// Check the Cookies in 'req', and set the Cookies to send to the client with 'res'
// if necessary.
foreach (Cookie cookie in req) {
cookie.Expired = true;
res.Add (cookie);
}
return true; // If valid.
//.........这里部分代码省略.........