1. 程式人生 > >C#開發BIMFACE系列12 服務端API之檔案轉換

C#開發BIMFACE系列12 服務端API之檔案轉換

系列目錄     【已更新最新開發文章,點選檢視詳細】

在代表模型的原始檔上傳到BIMFACE後,一般會進行三種API呼叫操作:

  1. 發起模型轉換

  2. 查詢轉換狀態

  3. 如轉換成功,獲取模型轉換後的BIM資料

在模型成功進行轉換後,模型內的BIM資訊會在雲端進行解析,抽取並結構化入庫。這些資訊包含:
  • 構件屬性資訊

  • 構件分類樹

  • 樓層

  • 單體

  • 專業

  • 構件材質

  • 模型連結

  • 空間

  • 房間

  • 圖紙

  • …​

在確認模型轉換成功後,為了開發者能方便的獲取這些BIM資訊並整合在自己的應用中,BIMFACE提供了一系列的資料介面,這些介面支援兩種驗權方式:

Access token: 代表自身應用的身份,使用應用的appkey, secret,通過呼叫/oauth2/token介面獲取。
View token:    代表對單個模型的訪問許可權,使用access token,通過呼叫/view/token以及相關介面獲得。

發起轉換

請求地址:PUT https://api.bimface.com/translate

說明:原始檔上傳成功後,即可發起對該檔案的轉換。由於轉換不能立即完成,BIMFace支援在檔案轉換完成以後,通過Callback機制通知應用;另外,應用也可以通過介面查詢轉換狀態。

引數:application/json 格式

請求 path(示例):https://api.bimface.com/translate

請求 header(示例):"Authorization: Bearer dc671840-bacc-4dc5-a134-97c1918d664b"

請求 body(示例):

{
  "callback" : "https://api.glodon.com/viewing/callback?authCode=iklJk0affae&signature=2ef131395fb6442eb99abd83d45c3201",
  "config" : {
    "string" : "string"
  },
  "priority" : 2,
  "source" : {
    "compressed" : false,
    "fileId" : 1277823232112,
    "rootName" : "rootFileName.rvt"
  }
} 
注意:請求體中的 config 可以設定為空。"config":null 或者傳入指定的轉換引數 "config":{"texture":true} 。

HTTP響應示例(200):

{
  "code" : "success",
  "data" : {
    "createTime" : "2017-12-25 17:23:46",
    "databagId" : "9b711803a43b92d871cde346b63e5019",
    "fileId" : 1248789071339712,
    "name" : "bimface_2018.rvt",
    "priority" : 2,
    "reason" : "reason",
    "status" : "success",
    "thumbnail" : [ "https://m.bimface.com/9b711803a43b92d871cde346b63e5019/thumbnail/96.png", "https://m.bimface.com/9b711803a43b92d871cde346b63e5019/thumbnail/256.png" ]
  },
  "message" : ""
}
  請求體內的引數解釋  

DGW與RVT格式的檔案轉換的配置引數不同,所以封裝了2個對應的C#類:

 1 /// <summary>
 2 ///  發起DWG檔案轉化的請求資料
 3 /// </summary>
 4 [Serializable]
 5 public class DwgFileTranslateRequest : FileTranslateRequest
 6 {
 7     /// <summary>
 8     ///  Dwg模型轉換引擎自定義引數,config引數跟轉換引擎相關,不同的轉換引擎支援不同的config格式。
 9     /// 例如轉換時新增內建材質,則新增引數值{"texture":true},新增外部材質時參考“使用模型外接材質場景”請求報文。
10     /// 如果不需要設定該引數,則設定為null
11     /// </summary>
12     [JsonProperty("config", NullValueHandling = NullValueHandling.Ignore)]
13     public DwgModelConfig Config { get; set; }
14 }
 1 /// <summary>
 2 ///  發起Rvt檔案轉化的請求資料
 3 /// </summary>
 4 [Serializable]
 5 public class RvtFileTranslateRequest : FileTranslateRequest
 6 {
 7     /// <summary>
 8     ///  Rvt模型轉換引擎自定義引數,config引數跟轉換引擎相關,不同的轉換引擎支援不同的config格式。
 9     /// 例如轉換時新增內建材質,則新增引數值{"texture":true},新增外部材質時參考“使用模型外接材質場景”請求報文。
10     /// 如果不需要設定該引數,則設定為null
11     /// </summary>
12     [JsonProperty("config", NullValueHandling = NullValueHandling.Ignore)]
13     public RvtModelConfig Config { get; set; }
14 }
1 /// <summary>
2 ///  其他三維模型檔案,包括RVT格式文轉化的請求資料
3 /// </summary>
4 [Serializable]
5 public class Other3DModelFileTranslateRequest : RvtFileTranslateRequest
6 {
7 }
 1 [Serializable]
 2 public class TranslateSource
 3 {
 4     public TranslateSource()
 5     {
 6         Compressed = false;
 7         RootName = null;
 8     }
 9 
10     /// <summary>
11     ///  檔案Id,即呼叫上傳檔案API返回的fileId
12     /// </summary>
13     [JsonProperty("fileId")]
14     public long FileId { get; set; }
15 
16     /// <summary>
17     ///  是否為壓縮檔案,預設為false
18     /// </summary>
19     [JsonProperty("compressed")]
20     public bool Compressed { get; set; }
21 
22     /// <summary>
23     ///  如果是壓縮檔案,必須指定壓縮包中哪一個是主檔案。(例如:root.rvt)。
24     /// 如果不是壓縮,則設定為 null
25     /// </summary>
26     [JsonProperty("rootName", NullValueHandling = NullValueHandling.Ignore)]
27     public string RootName { get; set; }
28 }

共同的基類FileTranslateRequest:

 1 /// <summary>
 2 ///  發起檔案轉化的請求資料
 3 /// </summary>
 4 [Serializable]
 5 public class FileTranslateRequest
 6 {
 7     public FileTranslateRequest()
 8     {
 9         Priority = 2;
10         CallBack = "http://www.app.com/receive";
11     }
12 
13     [JsonProperty("source")]
14     public TranslateSource Source { get; set; }
15 
16     /// <summary>
17     /// 優先順序,數字越大,優先順序越低。只能是1, 2, 3。預設為2
18     /// </summary>
19     [JsonProperty("priority")]
20     public int Priority { get; set; }
21 
22     /// <summary>
23     ///  Callback地址,待轉換完畢以後,BIMFace會回撥該地址
24     /// </summary>
25     [JsonProperty("callback")]
26     public string CallBack { get; set; }
27 }

不同模型轉換支援的自定義引數config:

(1) rvt模型

對應封裝成的C#實體類:

 1 /// <summary>
 2 ///  rvt 模型配置項
 3 /// </summary>
 4 [Serializable]
 5 public class RvtModelConfig
 6 {
 7     public RvtModelConfig()
 8     {
 9         //設定 null,在序列化的時候忽略該欄位,不出現在序列化後的字串中
10         Texture = null;
11         ExportDwg = null;
12         ExportDrawing = null;
13         ExportPdf = null;
14         ViewName = null;
15         DisplayLevel = null;
16         ExportDwgInstance = null;
17         ExportHiddenObjects = null;
18         ExportSystemType = null;
19         ExportProperties = null;
20         Unit = null;
21         ExportSchedule = null;
22     }
23 
24     /// <summary>
25     ///  轉換時是否新增材質。預設為 false
26     /// </summary>
27     [JsonProperty("texture", NullValueHandling = NullValueHandling.Ignore)]
28     public bool? Texture { get; set; }
29 
30     /// <summary>
31     ///  rvt2md是否匯出dwg檔案。預設為 false
32     /// </summary>
33     [JsonProperty("exportDwg", NullValueHandling = NullValueHandling.Ignore)]
34     public bool? ExportDwg { get; set; }
35 
36     /// <summary>
37     ///  dwg2md是否匯出mdv(向量化圖紙);取值為true時,exportDwg自動設定為true。預設為 false
38     /// </summary>
39     [JsonProperty("exportDrawing", NullValueHandling = NullValueHandling.Ignore)]
40     public bool? ExportDrawing { get; set; }
41 
42     /// <summary>
43     ///  dwg2md是否匯出pdf檔案。預設為 false
44     /// </summary>
45     [JsonProperty("exportPdf", NullValueHandling = NullValueHandling.Ignore)]
46     public bool? ExportPdf { get; set; }
47 
48     /// <summary>
49     ///  轉換使用的3D檢視。預設為 {3D}
50     /// </summary>
51     [JsonProperty("viewName", NullValueHandling = NullValueHandling.Ignore)]
52     public string ViewName { get; set; }
53 
54     /// <summary>
55     ///  設定轉換的精細度,fine(精細),medium(中等),coarse(粗略)。預設為 fine
56     /// </summary>
57     [JsonProperty("displaylevel", NullValueHandling = NullValueHandling.Ignore)]
58     public string DisplayLevel { get; set; }
59 
60     /// <summary>
61     /// 是否匯出dwg例項。預設為 false
62     /// </summary>
63     [JsonProperty("exportDwgInstance", NullValueHandling = NullValueHandling.Ignore)]
64     public bool? ExportDwgInstance { get; set; }
65 
66     /// <summary>
67     /// 是否匯出三維檢視中隱藏的構件。預設為 false
68     /// </summary>
69     [JsonProperty("exportHiddenObjects", NullValueHandling = NullValueHandling.Ignore)]
70     public bool? ExportHiddenObjects { get; set; }
71 
72     /// <summary>
73     /// 是否在userData中加入mepSystemType。預設為 false
74     /// </summary>
75     [JsonProperty("exportSystemType", NullValueHandling = NullValueHandling.Ignore)]
76     public bool? ExportSystemType { get; set; }
77 
78     /// <summary>
79     /// 是否在匯出NWD的屬性db檔案。預設為 false
80     /// </summary>
81     [JsonProperty("exportProperties", NullValueHandling = NullValueHandling.Ignore)]
82     public bool? ExportProperties { get; set; }
83 
84     /// <summary>
85     ///  設定轉換使用的單位,取值"ft"\"feet"\"英尺"採用revit預設的英尺為單位,預設以毫米為單位。預設為空
86     /// </summary>
87     [JsonProperty("unit", NullValueHandling = NullValueHandling.Ignore)]
88     public string Unit { get; set; }
89 
90     /// <summary>
91     /// 是否使用明細表內容。預設為 false
92     /// </summary>
93     [JsonProperty("exportSchedule", NullValueHandling = NullValueHandling.Ignore)]
94     public bool? ExportSchedule { get; set; }
95 }

Rvt轉換配置中很多選項都是有預設值的,如果手動設定的值與預設值相同,那麼可以不用設定該項。

為了簡化顯示請求body中的config配置項,在建構函式中將數值型別的配置項預設設定為 null,再配合 Newtonsoft.Json.dll

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)] 屬性,在序列化時可以不生成該項。

(2) dwg模型

對應封裝成的C#實體類

 1 /// <summary>
 2 ///  dwg 模型配置項
 3 /// </summary>
 4 [Serializable]
 5 public class DwgModelConfig
 6 {
 7     /// <summary>
 8     ///  是否轉成向量圖紙,預設為 true
 9     /// </summary>
10     [JsonProperty("exportDrawing")]
11     public bool ExportDrawing { get; set; }
12 
13     /// <summary>
14     ///  是否匯出pdf檔案,預設為 false
15     /// </summary>
16     [JsonProperty("exportPdf")]
17     public bool ExportPdf { get; set; }
18 
19     /// <summary>
20     ///  是否匯出縮圖,預設為 true
21     /// </summary>
22     [JsonProperty("exportThumb")]
23     public bool ExportThumb { get; set; }
24 }

 

下面分別介紹常用的不同型別的檔案轉換場景

1、DWG檔案轉換成向量圖紙

請求 body(示例):

{
    "source":{
        "fileId":1402934652281952,            // 檔案ID
        "compressed":false                    // 檔案是否為壓縮檔案
    },
    "priority":2,                             // 轉換優先順序
    "callback":"http://www.app.com/receive",  // 回撥地址
    "config":null                             // 轉換配置選項
} 

C#實現方法:

 1 /// <summary>
 2 ///  發起轉換。將DWG檔案轉換成圖片。
 3 /// </summary>
 4 /// <param name="accessToken">令牌</param>
 5 /// <param name="request">DWG檔案轉化的請求資料物件。根據實際需要設定物件裡面的引數,不需要的引數不用賦值</param>
 6 /// <returns></returns>
 7 public virtual FileTranslateResponse TranslateDwgToPicture(string accessToken, DwgFileTranslateRequest request)
 8 {
 9     string data = request.SerializeToJson();
10     return TranslateFile(accessToken, data);
11 }

 其中呼叫的 TranslateFile()方法如下:

 1 /// <summary>
 2 ///  發起轉換。
 3 ///  原始檔上傳成功後,即可發起對該檔案的轉換。由於轉換不能立即完成,BIMFace支援在檔案轉換完成以後,通過Callback機制通知應用;
 4 ///  另外,應用也可以通過介面查詢轉換狀態
 5 /// </summary>
 6 /// <param name="accessToken">令牌</param>
 7 /// <param name="data">請求體資料</param>
 8 /// <returns></returns>
 9 private FileTranslateResponse TranslateFile(string accessToken, string data)
10 {
11     //PUT https://api.bimface.com/translate
12     string url = BimfaceConstants.API_HOST + "/translate";
13 
14     BimFaceHttpHeaders headers = new BimFaceHttpHeaders();
15     headers.AddOAuth2Header(accessToken);
16 
17     try
18     {
19         FileTranslateResponse response;
20 
21         HttpManager httpManager = new HttpManager(headers);
22         HttpResult httpResult = httpManager.Put(url, data);
23         if (httpResult.Status == HttpResult.STATUS_SUCCESS)
24         {
25             response = httpResult.Text.DeserializeJsonToObject<FileTranslateResponse>();
26         }
27         else
28         {
29             response = new FileTranslateResponse
30             {
31                 Message = httpResult.RefText
32             };
33         }
34 
35         return response;
36     }
37     catch (Exception ex)
38     {
39         throw new Exception("[發起檔案轉換]發生異常!", ex);
40     }
41 }

該方法在後面的幾種模型轉換中也會用到,是公用的方法。

其中呼叫到的 httpManager.Put() 方法,請參考《C# HTTP系列》

2、DWG檔案轉換成圖片

請求 body(示例):

{
    "source":{
        "fileId":857482189666208,
        "compressed":false,
        "rootName":"root.dwg"
    },
    "priority":2,
    "callback":"http://www.app.com/receive",
    "config":{
            "exportDrawing":false             // 是否轉成向量圖紙
    }
}

C#實現方法: 

 1 /// <summary>
 2 ///  發起轉換。將DWG檔案轉換成圖片。
 3 /// </summary>
 4 /// <param name="accessToken">令牌</param>
 5 /// <param name="request">DWG檔案轉化的請求資料物件。根據實際需要設定物件裡面的引數,不需要的引數不用賦值</param>
 6 /// <returns></returns>
 7 public virtual FileTranslateResponse TranslateDwgToPicture(string accessToken, DwgFileTranslateRequest request)
 8 {
 9     string data = request.SerializeToJson();
10     return TranslateFile(accessToken, data);
11 }
3、RVT檔案轉換成著色模式的效果

請求 body(示例):

{
    "source":{
        "fileId":857482189666208,
        "compressed":false,
        "rootName":"root.rvt"       // 如果是壓縮檔案,必須指定壓縮包中哪一個是主檔案
    },
    "priority":2,
    "callback":"http://www.app.com/receive",
    "config":null
}

C#實現方法:

 1 /// <summary>
 2 ///  發起轉換。將RVT檔案轉換成著色模式的效果。
 3 /// </summary>
 4 /// <param name="accessToken">令牌</param>
 5 /// <param name="request">RVT檔案轉化的請求資料物件。根據實際需要設定物件裡面的引數,不需要的引數不用賦值</param>
 6 /// <returns></returns>
 7 public virtual FileTranslateResponse TranslateRvtToRenderStyle(string accessToken, RvtFileTranslateRequest request)
 8 {
 9     string data = request.SerializeToJson();
10     return TranslateFile(accessToken, data);
11 }
4、RVT檔案轉換成真真實模式的效果

請求 body(示例):

{
    "source":{
        "fileId":857482189666208,
        "compressed":false,
        "rootName":"root.rvt"
    },
    "priority":2,
    "callback":"http://www.app.com/receive",
    "config":{"texture":true}                 // 轉換時是否新增材質
}

C#實現方法:

 1 /// <summary>
 2 ///  發起轉換。將RVT檔案轉換成真真實模式的效果。
 3 /// </summary>
 4 /// <param name="accessToken">令牌</param>
 5 /// <param name="request">RVT檔案轉化的請求資料物件。根據實際需要設定物件裡面的引數,不需要的引數不用賦值</param>
 6 /// <returns></returns>
 7 public virtual FileTranslateResponse TranslateRvtToRealStyle(string accessToken, RvtFileTranslateRequest request)
 8 {
 9     string data = request.SerializeToJson();
10     return TranslateFile(accessToken, data);
11 }
5、RVT格式檔案轉換成具備二三維聯動的功能

請求 body(示例):

{
    "source":{
        "fileId":1402934652281952,
        "compressed":false
    },
    "priority":2,
    "config":{
        "texture": false,        // 轉換時是否新增材質
        "exportDwg": true,       // rvt2md是否匯出dwg檔案
        "exportPdf": true,       // dwg2md是否匯出pdf檔案
        "exportDrawing": true    // dwg2md是否匯出mdv(向量化圖紙);取值為true時,exportDwg自動設定為true
    },
    "callback":"http://www.app.com/receive"
}

C#實現方法: 

 1 /// <summary>
 2 ///  發起轉換。將RVT格式檔案轉換為具備二三維聯動的功能效果。
 3 /// </summary>
 4 /// <param name="accessToken">令牌</param>
 5 /// <param name="request">RVT檔案轉化的請求資料物件。根據實際需要設定物件裡面的引數,不需要的引數不用賦值</param>
 6 /// <returns></returns>
 7 public virtual FileTranslateResponse TranslateRvtTo23LinkStyle(string accessToken, RvtFileTranslateRequest request)
 8 {
 9     string data = request.SerializeToJson();
10     return TranslateFile(accessToken, data);
11 }
6、其它三維模型檔案轉換:常規轉換(不帶材質)

請求 body(示例):

{
    "source":{
        "fileId":857482189666208,
        "compressed":false,
        "rootName":"root.skp"
    },
    "priority":2,
    "callback":"http://www.app.com/receive",
    "config":null
}

C#實現方法:

 1 /// <summary>
 2 ///  發起轉換。其它三維模型檔案轉換,常規轉換(不帶材質)
 3 /// </summary>
 4 /// <param name="accessToken">令牌</param>
 5 /// <param name="request">其他三維模型檔案,包括RVT格式檔案轉化的請求資料物件。根據實際需要設定物件裡面的引數,不需要的引數不用賦值</param>
 6 /// <returns></returns>
 7 public virtual FileTranslateResponse TranslateOther3DModelToWithoutMaterialStyle(string accessToken, Other3DModelFileTranslateRequest request)
 8 {
 9     string data = request.SerializeToJson();
10     return TranslateFile(accessToken, data);
11 }
7、其他三維模型檔案包括RVT格式檔案,需要轉換出引用的外部材質場景、貼圖等

請求 body(示例):

{
    "source":{
        "fileId":1234621112557376,
        "compressed":true,
        "rootName":"bimface_2018_打包材質&系統材質庫.rvt"
    },
    "priority":2,
    "callback":"http://www.app.com/receive",
    "config":{"texture":true}
}

C#實現方法:

 1 /// <summary>
 2 ///  發起轉換。
 3 ///  其他三維模型檔案包括RVT格式檔案,需要轉換出引用的外部材質場景、貼圖等
 4 /// (上傳的檔案必須為壓縮包,壓縮包內同級目錄包含模型檔案和關聯的所有材質檔案,轉換時必須指定rootName為主檔案)。
 5 /// </summary>
 6 /// <param name="accessToken">令牌</param>
 7 /// <param name="request">其他三維模型檔案,包括RVT格式檔案轉化的請求資料物件。根據實際需要設定物件裡面的引數,不需要的引數不用賦值</param>
 8 /// <returns></returns>
 9 public virtual FileTranslateResponse TranslateOther3DModelToWithMaterialStyle(string accessToken, Other3DModelFileTranslateRequest request)
10 {
11     string data = request.SerializeToJson();
12     return TranslateFile(accessToken, data);
13 }
  測試

在BIMFACE的控制檯中可以看到我們上傳的檔案列表,共計2個檔案。

 

下面以 rac_advanced_sample_project-三維檢視 - From Parking Area.dwg 檔案為例,測試“將DWG檔案轉換成向量圖紙”方法。

在如下所示的測試頁面中,DWG檔案轉換區域中,選擇相關的轉換引數,然後點選【將DWG檔案轉換成向量圖紙】按鈕開始轉換

重新整理控制檯中的列表可以看到該檔案的模型狀態顯示為“轉換中”

等待幾秒或者幾分鐘後,該檔案的模型狀態顯示為“轉換成功”

待BIMFace轉換完畢後,根據應用傳入的回撥地址,BIMFace會通知轉換結果,轉換可能成功、也可能失敗。

檢視伺服器上配置的Callback處理程式記錄的日誌:

  Callback的配置與業務邏輯

Callback的配置項如下:

Callback 傳回以下引數:

signature(簽名):為了確保回撥訊息是由BIMFace發出的,應用在收到回撥訊息後,須驗證簽名。簽名的計算方式:MD5(``appKey:appSecret:fileId:status:nonce''),如果應用計算的簽名與BIMFace返回的簽名一致,則證明該訊息是安全可靠的。

應用收到回撥後,須向BIMFace傳送回執,回執訊息:HTTP STATUS 200

BimFaceHandler的回撥地址對應的完整邏輯方法如下:
 1     /// <summary>
 2     /// BimFaceHandler 的摘要說明
 3     /// </summary>
 4     public class BimFaceHandler : IHttpHandler
 5     {
 6         public void ProcessRequest(HttpContext context)
 7         {
 8             context.Response.ContentType = "text/plain";
 9             //context.Response.Write("Hello World");
10 
11             string appKey = ConfigUtility.GetAppSettingValue("BIMFACE_AppKey");
12             string appSecret = ConfigUtility.GetAppSettingValue("BIMFACE_AppSecret");
13             string uid = context.Request.QueryString["uid"];  // SparkBimFace
14 
15             #region 校驗
16             if (appKey.IsNullOrWhiteSpace())
17             {
18                 LogUtility.Error("BIMFace appKey 配置項沒有配置!");
19 
20                 return;
21             }
22 
23             if (appSecret.IsNullOrWhiteSpace())
24             {
25                 LogUtility.Error("BIMFace appSecret 配置項沒有配置!");
26 
27                 return;
28             }
29 
30             if (uid.IsNullOrWhiteSpace())
31             {
32                 LogUtility.Error("[非法請求]回撥地址Url連結中的引數 uid 沒有配置或者配置的值為空!");
33 
34                 return;
35             }
36             #endregion
37 
38             long fileId = context.Request.QueryString["fileId"].ToLong();  // 檔案ID
39             string status = context.Request.QueryString["status"];         // 轉換的結果
40             string reason = context.Request.QueryString["reason"];         // 若轉換失敗,則返回失敗原因
41             string thumbnail = context.Request.QueryString["thumbnail"];   // 縮圖地址
42             string nonce = context.Request.QueryString["nonce"];           // 回撥隨機數
43             string signature = context.Request.QueryString["signature"];   // BIMFACE的加密簽名
44 
45            
46             string callbackResponse = string.Format("fileId:{0},\r\nstatus:{1},\r\nreason:{2},\r\nthumbnail:{3},\r\nnonce:{4},\r\nsignature:{5}",
47                                                      fileId, status, reason, thumbnail, nonce, signature);
48             string tip;
49             string custCalcSignature;
50             FileConvertApi api = new FileConvertApi();
51             bool checkSignature = api.CheckCallbackSignature(appKey, appSecret, fileId, status, nonce, signature, out custCalcSignature);
52             if (checkSignature)
53             {
54                 tip = "[BIMFace發出的回撥資訊簽名驗證成功!]"
55                       + Environment.NewLine
56                       + callbackResponse;
57                 LogUtility.Info(tip);
58 
59                 //Todo 此處可以根據fileId把相關的資訊寫入資料庫中
60 
61                 // 回執訊息:應用收到回撥後,須向BIMFace傳送回執,回執訊息:HTTP STATUS 200
62                 context.Response.Write("HTTP STATUS 200");
63             }
64             else
65             {
66                 tip = "[BIMFace發出的回撥資訊簽名驗證不通過!]"
67                     + Environment.NewLine
68                     + callbackResponse
69                     + Environment.NewLine
70                     + "自定義計算簽名 custCalcSignature:" + custCalcSignature;
71 
72                 LogUtility.Error(tip);
73 
74                 context.Response.Write(tip);
75             }
76 
77             context.Response.End();
78         }
79 
80         /// <summary>
81         ///  該屬性獲得一個布林值,指示另一個請求是否可以使用該HTTP處理程式的例項。
82         /// <para>如果設定為true,能提高效能,但要注意執行緒之間安全性問題。如果設定為false,則執行緒是安全的</para>
83         /// </summary>
84         public bool IsReusable
85         {
86             get
87             {
88                 return false;
89             }
90         }
91     }

其中呼叫到的 CheckCallbackSignature()方法,用於驗證BIMFace發出的回撥訊息簽名信息是否安全可靠

 1 /// <summary>
 2 ///  驗證BIMFace發出的回撥訊息簽名信息是否安全可靠
 3 /// </summary>
 4 /// <param name="appKey">開發者祕鑰</param>
 5 /// <param name="appSecret">開發者密碼</param>
 6 /// <param name="fileId">BIMFace發出的回撥資訊:檔案ID</param>
 7 /// <param name="status">BIMFace發出的回撥資訊:轉換的結果</param>
 8 /// <param name="nonce">BIMFace發出的回撥資訊:回撥隨機數</param>
 9 /// <param name="signature">BIMFace發出的回撥資訊:簽名</param>
10 /// <param name="custCalcSignature">輸出引數:根據BIMFACE平臺的加密規則計算出來的簽名信息</param>
11 /// <returns></returns>
12 public bool CheckCallbackSignature(string appKey, string appSecret, long fileId, string status, string nonce, string signature, out string custCalcSignature)
13 {
14     custCalcSignature = GetCallbackSignature(appKey, appSecret, fileId, status, nonce);
15 
16     return custCalcSignature == signature;
17 }
 1 /// <summary>
 2 ///  獲取BIMFace發出的回撥訊息簽名信息
 3 /// </summary>
 4 /// <param name="appKey">開發者祕鑰</param>
 5 /// <param name="appSecret">開發者密碼</param>
 6 /// <param name="fileId">BIMFace發出的回撥資訊:檔案ID</param>
 7 /// <param name="status">BIMFace發出的回撥資訊:轉換的結果</param>
 8 /// <param name="nonce">BIMFace發出的回撥資訊:回撥隨機數</param>
 9 /// <returns></returns>
10 public string GetCallbackSignature(string appKey, string appSecret, long fileId, string status, string nonce)
11 {
12     /* signature(簽名):為了確保回撥訊息是由BIMFace發出的,應用在收到回撥訊息後,須驗證簽名。
13      * 簽名的計算方式:MD5("appKey:appSecret:fileId:status:nonce"),如果應用計算的簽名與BIMFace返回的簽名一致,則證明該訊息是安全可靠的。 
14      */
15 
16     return string.Format("{0}:{1}:{2}:{3}:{4}", appKey, appSecret, fileId, status, nonce).EncryptByMD5();
17 }

這裡使用C#的自定義擴充套件方法 EncryptByMD5()

 1 /// <summary>
 2 ///  使用 MD5(不可逆加密) 演算法加密字串
 3 /// </summary>
 4 /// <param name="this">擴充套件物件。字串</param>
 5 /// <param name="caseType">字串大小寫。預設小寫</param>
 6 /// <returns></returns>
 7 public static string EncryptByMD5(this string @this, CaseType caseType = CaseType.Lower)
 8 {
 9     using (MD5 md5 = MD5.Create())
10     {
11         var sb = new StringBuilder();
12         byte[] hashBytes = md5.ComputeHash(Encoding.Default.GetBytes(@this));
13         foreach (byte bytes in hashBytes)
14         {
15             sb.Append(bytes.ToString("X2"));//X2 表示二進位制
16         }
17 
18         return caseType == CaseType.Upper ? sb.ToString() : sb.ToString().ToLower();
19     }
20 }

 

系列目錄     【已更新最新開發文章,點選檢視詳細】 &nbs