public static void To string filePathIn int type string filePathOut by

 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
public static void To(string filePathIn, int type, string filePathOut)
{
byte[] filePosted = System.IO.File.ReadAllBytes(filePathIn);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dca.neutrinosoft.net/api/convert/");
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
var data = new Wrapper
{
ConvertType = (ConvertType)type,
File = filePosted
};
string postData = JsonConvert.SerializeObject(data);
byte[] bytes = Encoding.UTF8.GetBytes(postData);
request.ContentLength = bytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
var otvet = ReadFully(stream);
System.IO.File.WriteAllBytes(filePathOut, otvet);
stream.Dispose();
}
public enum ConvertType
{
ToPNG,
ToPDF
}
public class Wrapper
{
public byte[] File { get; set; }
public ConvertType ConvertType { get; set; }
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}