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的更多相关文章

随机推荐

  1. 通过 Spark R 操作 Hive

    作为数据工程师,我日常用的主力语言是R,HiveQL,Java与Scala.R是非常适合做数据清洗的脚本语言,并且有非常好用的服务端IDE——RStudio Server:而用户日志主要储存在hive ...

  2. Linux 内核版本,Ubuntu版本的查看

    查看内核版本 1)  cat /proc/version [root@a ~]# cat /proc/version Linux version 2.6.18-194.el5 (mockbuild@x ...

  3. android 获取配置文件 相对路径

    --src --com.example.Demo1 --IndexActivity.class       --test.txt 在IndexActivity.class 获取test.txt中的文件 ...

  4. hashMap put方法 第三行代码

    inflateTable(threshold) 分析: Hi.java public class Hi { //临界值(一个值最接近2的n次幂的数,比如7的临界值为8) int threshold; ...

  5. git之移除.idea

    有时候不小心提交了.idea目录,git会一直track这个目录,可以通过一下命令移除: mv .idea ../.idea_backup rm -r .idea git rm -r .idea gi ...

  6. soapui-groovy脚本中文乱码及符号乱码、响应乱码解决方案

    groovy脚本中文乱码及符号乱码,解决方案: 响应乱码解决方案:

  7. 浅谈android中只使用一个TextView实现高仿京东,淘宝各种倒计时

    今天给大家带来的是只使用一个TextView实现一个高仿京东.淘宝.唯品会等各种电商APP的活动倒计时.近期公司一直加班也没来得及时间去整理,今天难得歇息想把这个分享给大家.只求共同学习,以及自己兴许 ...

  8. 【设计和开发一套简单自己主动化UI框架】

    !有兴趣的朋友请直接移步Github,本帖子已经不做更新,框架的详细的实现已经做了优化和代码整理,本文仅仅介绍了详细的设计思路! 目标:编写一个简单通用UI框架用于管理页面和完毕导航跳转 终于的实现效 ...

  9. aspose.word 使用简单方法

    aspose.word使用简单方法 概念介绍 使用aspose生成word报表步骤: 加载word模板 提供数据源 填充 加载模板 提供了4种重载方法 1 2 3 4 5 public Documen ...

  10. JQuery $.each遍历JSON字符串报Uncaught TypeError:Cannot use 'in' operator to search for

    查看一个简单的jQuery的例子来遍历一个JavaScript数组对象. [js] view plaincopy   var json = [ {"id":"1" ...