· 7 years ago · Mar 03, 2018, 05:06 AM
1public class Vuforia
2{
3 private static string VufuriaProcess(string requestPath, string JsonObject, string httpVerb)
4 {
5 string secret_key = ConfigurationManager.AppSettings["Vsecret_key"];
6 string access_key = ConfigurationManager.AppSettings["Vaccess_key"];
7 ASCIIEncoding Encoding = new ASCIIEncoding();
8 MD5 md5 = MD5.Create();
9 string serviceURI = "https://vws.vuforia.com" + requestPath;
10 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(serviceURI);
11 request.Method = httpVerb;
12
13 byte[] contentbytes = null;
14 byte[] contentMD5bytes = md5.ComputeHash(Encoding.GetBytes(""));
15 string contentMD5 = BitConverter.ToString(contentMD5bytes).Replace("-", "").ToLower();// "d41d8cd98f00b204e9800998ecf8427e";
16 string contentType = "";
17
18 if (JsonObject != null && JsonObject != "") //if body request is not null
19 {
20 contentbytes = Encoding.GetBytes(JsonObject);
21 contentMD5bytes = md5.ComputeHash(contentbytes);
22 contentMD5 = BitConverter.ToString(contentMD5bytes).Replace("-", "").ToLower();
23 contentType = "application/json";
24 request.ContentLength = contentbytes.Length;
25
26 }
27
28 #region Signature
29 HMACSHA1 sha1 = new HMACSHA1(System.Text.Encoding.ASCII.GetBytes(secret_key));
30 request.ContentType = contentType;
31 string date = string.Format("{0:r}", DateTime.Now.ToUniversalTime());
32 string StringToSign = String.Format("{0}n{1}n{2}n{3}n{4}", httpVerb, contentMD5, contentType, date, requestPath); // HTTP-Verb, Content-MD5, Content-Type, Date, Request-Path;
33 byte[] sha1Bytes = Encoding.GetBytes(StringToSign);
34 MemoryStream stream = new MemoryStream(sha1Bytes);
35 byte[] sha1Hash = sha1.ComputeHash(stream);
36 string signature = System.Convert.ToBase64String(sha1Hash);
37 #endregion Signature
38
39 request.Headers.Add("Authorization", string.Format("VWS {0}:{1}", access_key, signature));
40 try
41 {
42 string strResponse;
43 request.Date = DateTime.Now.ToUniversalTime();
44 if (JsonObject != null && JsonObject != "")
45 {
46 var newStream = request.GetRequestStream();
47 newStream.Write(contentbytes, 0, contentbytes.Length);
48 newStream.Close();
49 }
50 var response = request.GetResponse();
51 using (Stream Varstream = response.GetResponseStream())
52 {
53 using (StreamReader reader = new StreamReader(Varstream))
54 {
55 strResponse = reader.ReadToEnd();
56 }
57 }
58 return strResponse;
59 }
60 catch (WebException ex)
61 {
62 return ex.Message;
63 }
64 }
65 public static string AddTarget(byte[] bTarget, string TargetName, float width)
66 {
67 string image = System.Convert.ToBase64String(bTarget);
68 string json = "{"name":"" + TargetName + "", "width": "+width+", "image":"" + image + "", "active_flag":true}";
69 return VufuriaProcess("/targets", json, "POST");
70 }
71 public static string UpdateTarget(byte[] bTarget, string TargetName, float width, string TargetId)
72 {
73 string image = System.Convert.ToBase64String(bTarget);
74 string json = "{"name":"" + TargetName + "", "width": " + width + ", "image": "" + image + "", "active_flag": true}";
75 return VufuriaProcess("/targets/" + TargetId, json.ToString(), "PUT");
76 }
77 public static string DeleteTarget(string TargetId)
78 {
79 return VufuriaProcess("/targets/" + TargetId, null, "DELETE");
80 }
81 public static string RetrieveTargetRecord(string TargetId)
82 {
83 return VufuriaProcess("/targets/" + TargetId, null, "GET");
84 }
85 public static string CheckDuplicateTargets(string TargetId)
86 {
87 return VufuriaProcess("/duplicates/" + TargetId, null, "GET");
88 }
89 public static string GetTargetList()
90 {
91 return VufuriaProcess("/targets", null, "GET");
92 }
93 public static string TargetSummaryReport(string TargetId)
94 {
95 return VufuriaProcess("/summary/" + TargetId, null, "GET");
96 }
97 public static string DatabaseSummaryReport()
98 {
99 return VufuriaProcess("/summary", null, "GET");
100 }
101}