WopiServerTutorial
Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text; namespace WopiServerTutorial
{
class WopiServer
{
private HttpListener Listener; static void Main(string[] args)
{
WopiServer s = new WopiServer();
s.Start(); Console.WriteLine("A simple wopi webserver. Press a key to quit.");
Console.ReadKey(); s.Stop();
} public void Start()
{
Listener = new HttpListener();
Listener.Prefixes.Add(@"http://+:8080/");
Listener.Start();
Listener.BeginGetContext(ProcessRequest, Listener);
Console.WriteLine(@"WopiServer Started");
} public void Stop()
{
Listener.Stop();
} private void ProcessRequest(IAsyncResult result)
{
HttpListener listener = (HttpListener)result.AsyncState;
HttpListenerContext context = listener.EndGetContext(result); Console.WriteLine(@"Got a " + context.Request.HttpMethod + " request for URL: " + context.Request.Url.PathAndQuery);
var stringarr = context.Request.Url.AbsolutePath.Split('/');
var rootDir = @"C:\\temp\\"; if (stringarr.Length == 5 && context.Request.HttpMethod.Equals(@"GET"))
{
Console.WriteLine(@"Getting content for the file: " + rootDir + stringarr[3]); // get file's content
var file = rootDir + stringarr[3];
var stream = new FileStream(file, FileMode.Open);
var fi = new FileInfo(file); context.Response.ContentType = @"application/octet-stream";
context.Response.ContentLength64 = fi.Length;
stream.CopyTo(context.Response.OutputStream);
context.Response.Close();
}
//else if (stringarr.Length == 5 && context.Request.HttpMethod.Equals(@"POST"))
//{
// // write
//}
else if (stringarr.Length == 4 && context.Request.HttpMethod.Equals(@"GET"))
{
Console.WriteLine(@"Getting metdata for the file: " + rootDir + stringarr[3]);
var fi = new FileInfo(rootDir + stringarr[3]); CheckFileInfo cfi = new CheckFileInfo();
cfi.AllowExternalMarketplace = false;
cfi.BaseFileName = fi.Name;
cfi.BreadcrumbBrandName = "";
cfi.BreadcrumbBrandUrl = "";
cfi.BreadcrumbDocName = "";
cfi.BreadcrumbDocUrl = "";
cfi.BreadcrumbFolderName = "";
cfi.BreadcrumbFolderUrl = "";
cfi.ClientUrl = "";
cfi.CloseButtonClosesWindow = false;
cfi.CloseUrl = "";
cfi.DisableBrowserCachingOfUserContent = true;
cfi.DisablePrint = true;
cfi.DisableTranslation = true;
cfi.DownloadUrl = "";
cfi.FileUrl = "";
cfi.FileSharingUrl = "";
cfi.HostAuthenticationId = "s-1-5-21-3430578067-4192788304-1690859819-21774";
cfi.HostEditUrl = "";
cfi.HostEmbeddedEditUrl = "";
cfi.HostEmbeddedViewUrl = "";
cfi.HostName = @"SharePoint";
cfi.HostNotes = @"HostBIEnabled";
cfi.HostRestUrl = "";
cfi.HostViewUrl = "";
cfi.IrmPolicyDescription = "";
cfi.IrmPolicyTitle = "";
cfi.OwnerId = @"4257508bfe174aa28b461536d8b6b648";
cfi.PresenceProvider = "AD";
cfi.PresenceUserId = @"S-1-5-21-3430578067-4192788304-1690859819-21774";
cfi.PrivacyUrl = "";
cfi.ProtectInClient = false;
cfi.ReadOnly = false;
cfi.RestrictedWebViewOnly = false;
cfi.SHA256 = "";
cfi.SignoutUrl = "";
cfi.Size = fi.Length;
cfi.SupportsCoauth = false;
cfi.SupportsCobalt = false;
cfi.SupportsFolders = false;
cfi.SupportsLocks = true;
cfi.SupportsScenarioLinks = false;
cfi.SupportsSecureStore = false;
cfi.SupportsUpdate = true;
cfi.TenantId = @"33b62539-8c5e-423c-aa3e-cc2a9fd796f2";
cfi.TermsOfUseUrl = "";
cfi.TimeZone = @"+0300#0000-11-00-01T02:00:00:0000#+0000#0000-03-00-02T02:00:00:0000#-0060";
cfi.UserCanAttend = false;
cfi.UserCanNotWriteRelative = false;
cfi.UserCanPresent = false;
cfi.UserCanWrite = true;
cfi.UserFriendlyName = "";
cfi.UserId = "";
cfi.Version = @"%22%7B59CCD75F%2D0687%2D4F86%2DBBCF%2D059126640640%7D%2C1%22";
cfi.WebEditingDisabled = false; // encode json
var memoryStream = new MemoryStream();
var json = new DataContractJsonSerializer(typeof(CheckFileInfo));
json.WriteObject(memoryStream, cfi);
memoryStream.Flush();
memoryStream.Position = 0;
StreamReader streamReader = new StreamReader(memoryStream);
var jsonResponse = Encoding.UTF8.GetBytes(streamReader.ReadToEnd()); context.Response.ContentType = @"application/json";
context.Response.ContentLength64 = jsonResponse.Length;
context.Response.OutputStream.Write(jsonResponse, 0, jsonResponse.Length);
context.Response.Close();
}
else
{
byte[] buffer = Encoding.UTF8.GetBytes("");
context.Response.ContentLength64 = buffer.Length;
context.Response.ContentType = @"application/json";
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.OutputStream.Close();
} Listener.BeginGetContext(ProcessRequest, Listener);
}
}
}
CheckFileInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text; namespace WopiServerTutorial
{
[DataContract]
public class CheckFileInfo
{
[DataMember]
public bool AllowExternalMarketplace { get; set; }
[DataMember]
public string BaseFileName { get; set; }
[DataMember]
public string BreadcrumbBrandName { get; set; }
[DataMember]
public string BreadcrumbBrandUrl { get; set; }
[DataMember]
public string BreadcrumbDocName { get; set; }
[DataMember]
public string BreadcrumbDocUrl { get; set; }
[DataMember]
public string BreadcrumbFolderName { get; set; }
[DataMember]
public string BreadcrumbFolderUrl { get; set; }
[DataMember]
public string ClientUrl { get; set; }
[DataMember]
public bool CloseButtonClosesWindow { get; set; }
[DataMember]
public string CloseUrl { get; set; }
[DataMember]
public bool DisableBrowserCachingOfUserContent { get; set; }
[DataMember]
public bool DisablePrint { get; set; }
[DataMember]
public bool DisableTranslation { get; set; }
[DataMember]
public string DownloadUrl { get; set; }
[DataMember]
public string FileSharingUrl { get; set; }
[DataMember]
public string FileUrl { get; set; }
[DataMember]
public string HostAuthenticationId { get; set; }
[DataMember]
public string HostEditUrl { get; set; }
[DataMember]
public string HostEmbeddedEditUrl { get; set; }
[DataMember]
public string HostEmbeddedViewUrl { get; set; }
[DataMember]
public string HostName { get; set; }
[DataMember]
public string HostNotes { get; set; }
[DataMember]
public string HostRestUrl { get; set; }
[DataMember]
public string HostViewUrl { get; set; }
[DataMember]
public string IrmPolicyDescription { get; set; }
[DataMember]
public string IrmPolicyTitle { get; set; }
[DataMember]
public string OwnerId { get; set; }
[DataMember]
public string PresenceProvider { get; set; }
[DataMember]
public string PresenceUserId { get; set; }
[DataMember]
public string PrivacyUrl { get; set; }
[DataMember]
public bool ProtectInClient { get; set; }
[DataMember]
public bool ReadOnly { get; set; }
[DataMember]
public bool RestrictedWebViewOnly { get; set; }
[DataMember]
public string SHA256 { get; set; }
[DataMember]
public string SignoutUrl { get; set; }
[DataMember]
public long Size { get; set; }
[DataMember]
public bool SupportsCoauth { get; set; }
[DataMember]
public bool SupportsCobalt { get; set; }
[DataMember]
public bool SupportsFolders { get; set; }
[DataMember]
public bool SupportsLocks { get; set; }
[DataMember]
public bool SupportsScenarioLinks { get; set; }
[DataMember]
public bool SupportsSecureStore { get; set; }
[DataMember]
public bool SupportsUpdate { get; set; }
[DataMember]
public string TenantId { get; set; }
[DataMember]
public string TermsOfUseUrl { get; set; }
[DataMember]
public string TimeZone { get; set; }
[DataMember]
public bool UserCanAttend { get; set; }
[DataMember]
public bool UserCanNotWriteRelative { get; set; }
[DataMember]
public bool UserCanPresent { get; set; }
[DataMember]
public bool UserCanWrite { get; set; }
[DataMember]
public string UserFriendlyName { get; set; }
[DataMember]
public string UserId { get; set; }
[DataMember]
public string Version { get; set; }
[DataMember]
public bool WebEditingDisabled { get; set; }
}
}
WopiServerTutorial的更多相关文章
随机推荐
- 数据框排序 data.frame order
# sorting examples using the mtcars datasetattach(mtcars) # sort by mpgnewdata <- mtcars[order(mp ...
- qt 字体的相关问题
(一)qtconfig字体列表不全的问题? 发现界面的上的文字不能正常显示,后调用qtconfig发现里面识别的字体非常少,怀疑是编译的时候参数未能设置正确,于是经过多次试验,终于成功找到根结所在,. ...
- 《Qt数据类型》--QByteArray,QString,int,hex之间的转化
对于QString和QByteArray,他们都有一个toInt的静态函数,QString::toInt()是根据string的字面值转化为int类型,比如string:"123" ...
- 7、Qt MetaObject System详解
网上的资源比较乱,该文章整理自地址:http://www.xuebuyuan.com/735789.html Qt meta-object系统基于三个方面: 1.QObject提供一个基类,方便派生类 ...
- HashMap的长度为什么要是2的n次方
HashMap为了存取高效,要尽量较少碰撞,就是要尽量把数据分配均匀,每个链表长度大致相同,这个实现就在把数据存到哪个链表中的算法: 这个算法实际就是取模,hash%length,计算机中直接求余效率 ...
- easy-ui 使用总结
1.datagrid 2次加载问题: 解决方案:<table id="DataGrid" class="easyui-datagrid"> 改为 & ...
- 高通 mixer_paths.xml 音频配置文件 初始化过程
记录一下高通音频配置文件mixer_paths.xml初始化过程.参考代码基于Android O. hardware/qcom/audio/hal/audio_hw.c struct audio_mo ...
- e832. 从JTabbedPane中移动卡片
To move a tab, it must first be removed and then reinserted into the tabbed pane as a new tab. Unfor ...
- 本机IP、127.0.0.1和0.0.0.0的区别
本机ip.127.0.0.1和0.0.0.0区别 网络java IP地址的记法: IP地址由四个字节构成,为了方便阅读和书写,每个字节用0-255的数字表示,字节之间用’.'分割,如: 10.10 ...
- css 垂直居中,指定文本宽度换行,指定高度出滚动条
!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"&g ...