markus / ConvertService / ServiceBase / DownloadLinkTest / Program.cs @ ddc223b4
이력 | 보기 | 이력해설 | 다운로드 (2.68 KB)
1 | 77cdac33 | taeseongkim | //using RestSharp; |
---|---|---|---|
2 | using Markus.Service.Helper; |
||
3 | using RestSharp; |
||
4 | using System; |
||
5 | using System.Collections.Generic; |
||
6 | using System.IO; |
||
7 | using System.Linq; |
||
8 | using System.Net; |
||
9 | using System.Net.Http; |
||
10 | using System.Net.Http.Headers; |
||
11 | using System.Text; |
||
12 | using System.Threading.Tasks; |
||
13 | using System.Web; |
||
14 | |||
15 | namespace DownloadLinkTest |
||
16 | { |
||
17 | class Program |
||
18 | { |
||
19 | static void Main(string[] args) |
||
20 | { |
||
21 | |||
22 | try |
||
23 | { |
||
24 | Download("http://210.94.128.124/service/common-download.dft?key=CA8069433B4D5EEEE0500A0A79000EF6"); |
||
25 | } |
||
26 | catch (Exception ex) |
||
27 | { |
||
28 | |||
29 | Console.WriteLine(ex.ToString()); |
||
30 | } |
||
31 | |||
32 | Console.ReadKey(); |
||
33 | } |
||
34 | |||
35 | private static void Download(string uri) |
||
36 | { |
||
37 | //var FileName = DownloadUri.GetFileName(uri); |
||
38 | |||
39 | var client = new RestClient(uri); |
||
40 | client.Timeout = -1; |
||
41 | var request = new RestRequest(Method.GET); |
||
42 | IRestResponse response = client.Execute(request); |
||
43 | |||
44 | var fileName = DownloadUri.GetFileNameInDisposition(new System.Net.Mime.ContentDisposition(response.Headers.Where(x => x.Name == "Content-Disposition").First().Value.ToString())); |
||
45 | |||
46 | var fileInfo = new FileInfo("test.pdf"); |
||
47 | |||
48 | using (var fileStream = fileInfo.OpenWrite()) |
||
49 | { |
||
50 | fileStream.Write(response.RawBytes, 0, response.RawBytes.Length); |
||
51 | } |
||
52 | |||
53 | //StreamWriter sw = new StreamWriter(fs); |
||
54 | //sw.Write(str); |
||
55 | //sw.Close(); |
||
56 | //using (var stream = response.Content.ReadAsStringAsync().Result) |
||
57 | //{ |
||
58 | // var fileInfo = new FileInfo("test.pdf"); |
||
59 | // using (var fileStream = fileInfo.OpenWrite()) |
||
60 | // { |
||
61 | // stream.CopyTo(fileStream); |
||
62 | // } |
||
63 | //} |
||
64 | } |
||
65 | |||
66 | |||
67 | public static string callWebRequest(string uri) |
||
68 | { |
||
69 | string responseFromServer = string.Empty; |
||
70 | |||
71 | try |
||
72 | { |
||
73 | |||
74 | WebRequest request = WebRequest.Create(uri); |
||
75 | //request.Method = "GET"; |
||
76 | //request.ContentType = "application/octet-stream"; |
||
77 | |||
78 | using (WebResponse response = request.GetResponse()) |
||
79 | using (Stream dataStream = response.GetResponseStream()) |
||
80 | using (StreamReader reader = new StreamReader(dataStream)) |
||
81 | { |
||
82 | responseFromServer = reader.ReadToEnd(); |
||
83 | } |
||
84 | |||
85 | } |
||
86 | catch (Exception e) |
||
87 | { |
||
88 | Console.WriteLine(e.ToString()); |
||
89 | } |
||
90 | |||
91 | return responseFromServer; |
||
92 | } |
||
93 | } |
||
94 | } |