-
Notifications
You must be signed in to change notification settings - Fork 9
/
OrbitPostRequestBuilder.cs
115 lines (96 loc) · 4.13 KB
/
OrbitPostRequestBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
namespace shazam
{
public class OrbitPostRequestBuilder : IRequestBuilder
{
private MemoryStream requestDataStream = new MemoryStream();
private string Boundary
{
get;
set;
}
public IceKey Encryptor
{
get;
private set;
}
public char[] Key
{
get;
private set;
}
public OrbitPostRequestBuilder(IceKey encryptor, char[] key)
{
this.Encryptor = encryptor;
this.Key = key;
this.Boundary = "AJ8xP50454bf20Gp";
}
public void AddEncryptedFile(string name, string fileName, byte[] fileData, int fileSize)
{
this.Encryptor.set(this.Key);
byte[] numArray = this.Encryptor.encBinary(fileData, fileSize);
this.AddFile(name, fileName, numArray, (int)numArray.Length);
}
public void AddEncryptedParameter(string name, string value)
{
this.Encryptor.set(this.Key);
char[] chrArray = this.Encryptor.encString(value);
this.AddParameter(name, new string(chrArray));
}
public void AddFile(string name, string fileName, byte[] fileData, int fileSize)
{
string[] newLine = new string[] { "--{0}", Environment.NewLine, "Content-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"", Environment.NewLine, "Content-Type: {3}", Environment.NewLine, Environment.NewLine };
string str = string.Concat(newLine);
object[] boundary = new object[] { this.Boundary, name, fileName, "application/octet-stream" };
string str1 = string.Format(str, boundary);
byte[] bytes = Encoding.UTF8.GetBytes(str1);
this.requestDataStream.Write(bytes, 0, (int)bytes.Length);
this.requestDataStream.Write(fileData, 0, fileSize);
string newLine1 = Environment.NewLine;
byte[] numArray = Encoding.UTF8.GetBytes(newLine1);
this.requestDataStream.Write(numArray, 0, (int)numArray.Length);
}
public void AddParameter(string name, string value)
{
string[] newLine = new string[] { "--{0}", Environment.NewLine, "Content-Disposition: form-data; name=\"{1}\"", Environment.NewLine, Environment.NewLine, "{2}", Environment.NewLine };
string str = string.Concat(newLine);
object[] boundary = new object[] { this.Boundary, name, value };
string str1 = string.Format(str, boundary);
byte[] bytes = Encoding.UTF8.GetBytes(str1);
this.requestDataStream.Write(bytes, 0, (int)bytes.Length);
}
public string MakeRequestUri(string scheme, string hostName, string path)
{
return string.Concat(scheme, "://", hostName, path);
}
public void PopulateWebRequestHeaders(WebRequest webRequest)
{
webRequest.Method = "POST";
webRequest.ContentType = string.Concat("multipart/form-data; boundary=", this.Boundary);
}
public void WriteToRequestStream(Stream requestStream)
{
string str = string.Concat("--", this.Boundary, "--");
byte[] bytes = Encoding.UTF8.GetBytes(str);
this.requestDataStream.Write(bytes, 0, (int)bytes.Length);
byte[] array = this.requestDataStream.ToArray();
requestStream.Write(array, 0, (int)array.Length);
}
}
public interface IRequestBuilder
{
void AddEncryptedFile(string name, string fileName, byte[] fileData, int fileSize);
void AddEncryptedParameter(string name, string value);
void AddFile(string name, string fileName, byte[] fileData, int fileSize);
void AddParameter(string name, string value);
string MakeRequestUri(string scheme, string hostName, string path);
void PopulateWebRequestHeaders(WebRequest webRequest);
void WriteToRequestStream(Stream requestStream);
}
}