remove debug code
[GitHub/Stricted/SpeedportHybridControl.git] / SpeedportHybridControl / Data / SpeedportHybridAPI.cs
1 using Newtonsoft.Json.Linq;
2 using System;
3 using System.Linq;
4 using System.IO;
5 using System.Net;
6 using System.Text;
7 using System.Text.RegularExpressions;
8 using System.Windows;
9 using System.Threading;
10 using System.Collections.Generic;
11 using SpeedportHybridControl.Implementations;
12 using Newtonsoft.Json;
13 using SpeedportHybridControl.PageModel;
14
15 namespace SpeedportHybridControl.Data
16 {
17 public class SpeedportHybridAPI : SingletonFactory<SpeedportHybridAPI>
18 {
19 public string _ip = "speedport.ip";
20 private DateTime _lastReboot = DateTime.MinValue;
21 private bool _checkIsActive = false;
22 public string _password;
23 public string _challenge;
24 public string _hash;
25 public string _derivedk;
26 public CookieContainer _cookie = new CookieContainer();
27
28 public string ip
29 {
30 get { return _ip; }
31 set { _ip = value; }
32 }
33
34 /**
35 * Requests the password-challenge from the router.
36 *
37 * @return string
38 */
39 public string getChallenge()
40 {
41 string response = sendRequest("data/Login.json", "csrf_token=nulltoken&showpw=0&challengev=null");
42 if (response.IsNullOrEmpty())
43 return string.Empty;
44
45 string challenge = string.Empty;
46 try
47 {
48 JToken jArray = JToken.Parse(response);
49
50 challenge = jArray.getVar("challengev");
51 jArray = null;
52 }
53 catch (Exception ex)
54 {
55 LogManager.WriteToLog(ex.ToString());
56 }
57
58 response = null;
59
60 return challenge;
61 }
62
63 /**
64 * calculate the derivedk
65 *
66 * @param string $password
67 * @return string
68 */
69 public string getDerviedk()
70 {
71 return _password.sha256().pbkdf2(_challenge.Substring(0, 16));
72 }
73
74 /**
75 * login into the router with the given password
76 *
77 * @param string $password
78 * @return bool
79 */
80 public bool login(string password)
81 {
82 if (password.IsNullOrEmpty())
83 {
84 return false;
85 }
86
87 _cookie = new CookieContainer();
88
89 _password = password;
90 _challenge = getChallenge();
91 _hash = string.Concat(_challenge, ":", password).sha256();
92
93 string response = sendRequest("data/Login.json", string.Concat("csrf_token=nulltoken&showpw=0&password=", _hash));
94 if (response.IsNullOrEmpty())
95 return false;
96
97 _cookie.Add(new Cookie("challengev", _challenge) { Domain = "speedport.ip" });
98
99 bool login = false;
100 try
101 {
102 JToken jArray = JToken.Parse(response);
103 if (jArray.getVar("login").Equals("success"))
104 {
105 if (isLoggedin().Equals(false))
106 {
107 login = false;
108 }
109 else {
110 login = true;
111 _derivedk = getDerviedk();
112 _lastReboot = getLastReboot();
113 }
114 }
115 jArray = null;
116 }
117 catch (Exception ex)
118 {
119 LogManager.WriteToLog(ex.ToString());
120 }
121
122 response = null;
123
124 return login;
125 }
126
127 /**
128 * logout
129 *
130 * @return bool
131 */
132 public bool logout()
133 {
134 string response = sendRequest("data/Login.json", string.Concat("csrf_token=", getToken(), "&logout=byby"));
135 if (response.IsNullOrEmpty())
136 return false;
137
138 bool logout = false;
139 try
140 {
141 JToken jArray = JToken.Parse(response);
142 if (jArray.getVar("status").Equals("ok"))
143 {
144 logout = true;
145 _password = "";
146 _challenge = "";
147 _cookie = new CookieContainer();
148 _lastReboot = DateTime.MinValue;
149 _hash = "";
150 _derivedk = "";
151 }
152
153 jArray = null;
154 }
155 catch (Exception ex)
156 {
157 LogManager.WriteToLog(ex.ToString());
158 }
159
160 response = null;
161
162 return logout;
163 }
164
165 /**
166 * check if we are logged in
167 *
168 * @return bool
169 */
170 public bool checkLogin()
171 {
172 if (_checkIsActive.Equals(false))
173 {
174 _checkIsActive = true;
175 if (isLoggedin().Equals(false))
176 {
177 Thread.Sleep(400);
178
179 if (login(_password).Equals(false))
180 {
181 // should we try to relogin? login(_password);...
182 new Thread(() => { LogManager.WriteToLog("Session expired."); }).Start();
183 _password = "";
184 _challenge = "";
185 _cookie = new CookieContainer();
186 _lastReboot = DateTime.MinValue;
187 _hash = "";
188 _derivedk = "";
189
190 LoginPageModel lpm = Application.Current.FindResource("LoginPageModel") as LoginPageModel;
191 lpm.LogoutAction();
192 MainWindowModel mwm = Application.Current.FindResource("MainWindowModel") as MainWindowModel;
193 mwm.SwitchToLoginPage.Execute();
194
195 new Thread(() => { MessageBox.Show("Session expired.", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
196 _checkIsActive = false;
197 return false;
198 }
199 }
200
201 _checkIsActive = false;
202 }
203
204 return true;
205 }
206
207 /**
208 * check if we are logged in
209 *
210 * @param bool ischeck
211 * @return bool
212 */
213 public bool isLoggedin()
214 {
215 string response = sendRequest("data/heartbeat.json");
216 if (response.IsNullOrEmpty())
217 return false;
218
219 bool login = false;
220 try
221 {
222 JToken jArray = JToken.Parse(response);
223
224 if (jArray.getVar("loginstate").Equals("1"))
225 {
226 login = true;
227 }
228
229 jArray = null;
230 }
231
232 catch (Exception ex)
233 {
234 LogManager.WriteToLog(ex.ToString());
235 }
236
237 response = null;
238
239 return login;
240 }
241
242 /**
243 * reboot the router
244 */
245 public void reboot()
246 {
247 if (checkLogin().Equals(false))
248 return;
249
250 string response = sendRequest("data/Reboot.json", string.Concat("csrf_token=", Uri.EscapeUriString(getToken()), "&reboot_device=true"));
251 if (response.IsNullOrEmpty())
252 return;
253 try
254 {
255 JToken jArray = JToken.Parse(response);
256 if (jArray.getVar("status").Equals("ok"))
257 {
258 new Thread(() => { MessageBox.Show("Router Reboot.", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information); }).Start();
259 LogManager.WriteToLog("Router Reboot.");
260 _password = "";
261 _challenge = "";
262 _cookie = new CookieContainer();
263 _hash = "";
264 _derivedk = "";
265
266 LoginPageModel lpm = Application.Current.FindResource("LoginPageModel") as LoginPageModel;
267 lpm.LoginCommand.Execute();
268 MainWindowModel mwm = Application.Current.FindResource("MainWindowModel") as MainWindowModel;
269 mwm.SwitchToLoginPage.Execute();
270 }
271
272 jArray = null;
273 }
274 catch (Exception ex)
275 {
276 LogManager.WriteToLog(ex.ToString());
277 }
278
279 response = null;
280 }
281
282 /**
283 * reconnect LTE
284 *
285 * @return bool
286 */
287 public bool reconnectLte()
288 {
289 if (checkLogin().Equals(false))
290 return false;
291
292 Thread.Sleep(400);
293
294 string response = sendEnryptedRequest("data/modules.json", string.Concat("lte_reconn=1&csrf_token=", Uri.EscapeUriString(getToken())));
295 if (response.IsNullOrEmpty())
296 return false;
297
298 try
299 {
300 JToken jArray = JToken.Parse(response);
301
302 response = null;
303
304 if (jArray.getVar("status").Equals("ok"))
305 {
306 jArray = null;
307 return true;
308 }
309
310 jArray = null;
311 }
312 catch (Exception ex)
313 {
314 LogManager.WriteToLog(ex.ToString());
315 }
316
317 response = null;
318
319 return false;
320 }
321
322 /**
323 * reconnect DSL
324 *
325 * @return bool
326 */
327 public bool reconnectDSL()
328 {
329 if (checkLogin().Equals(false))
330 return false;
331
332 Thread.Sleep(400);
333
334 string response = sendEnryptedRequest("data/Connect.json", string.Concat("csrf_token=", Uri.EscapeUriString(getToken()), "&showpw=0&password=", _hash, "&req_connect=offline"));
335 if (response.IsNullOrEmpty())
336 return false;
337
338 bool offline = false;
339 try
340 {
341 JToken jArray = JToken.Parse(response);
342
343 response = null;
344
345 if (jArray.getVar("status").Equals("ok"))
346 {
347 offline = true;
348 }
349
350 jArray = null;
351
352 if (offline.Equals(true))
353 {
354 response = sendEnryptedRequest("data/Connect.json", string.Concat("csrf_token=", Uri.EscapeUriString(getToken()), "&showpw=0&password=", _hash, "&req_connect=online"));
355 jArray = JToken.Parse(response);
356 if (jArray.getVar("status").Equals("ok"))
357 {
358 jArray = null;
359 return true;
360 }
361 }
362 }
363 catch (Exception ex)
364 {
365 LogManager.WriteToLog(ex.ToString());
366 }
367
368 response = null;
369
370 return false;
371 }
372
373 /**
374 * change dsl connection status
375 *
376 * @param string status
377 * @return bool
378 */
379 public bool changeDSLStatus(string status)
380 {
381 if (checkLogin().Equals(false))
382 return false;
383
384 if (status.Equals("online") || status.Equals("offline"))
385 {
386
387 string response = sendEnryptedRequest("data/Connect.json", string.Concat("req_connect=", status, "&csrf_token=", Uri.EscapeUriString(getToken())));
388 if (response.IsNullOrEmpty())
389 return false;
390 try
391 {
392 JToken jArray = JToken.Parse(response);
393
394 response = null;
395
396 if (jArray.getVar("status").Equals("ok"))
397 {
398 jArray = null;
399 return true;
400 }
401 }
402 catch (Exception ex)
403 {
404 LogManager.WriteToLog(ex.ToString());
405 }
406
407 response = null;
408 }
409
410 return false;
411 }
412
413 /**
414 * change lte connection status
415 *
416 * @param string status
417 * @return bool
418 */
419 public bool changeLTEStatus(string status)
420 {
421 if (checkLogin().Equals(false))
422 return false;
423
424 if (status.Equals("online") || status.Equals("offline"))
425 {
426 if (status.Equals("online"))
427 status = "1";
428
429 if (status.Equals("offline"))
430 status = "0";
431
432 string response = sendEnryptedRequest("data/Modules.json", string.Concat("use_lte=", status, "&csrf_token=", Uri.EscapeUriString(getToken())));
433 if (response.IsNullOrEmpty())
434 return false;
435 try
436 {
437 JToken jArray = JToken.Parse(response);
438
439 response = null;
440
441 if (jArray.getVar("status").Equals("ok"))
442 {
443 jArray = null;
444 return true;
445 }
446 }
447 catch (Exception ex)
448 {
449 LogManager.WriteToLog(ex.ToString());
450 }
451
452 response = null;
453 }
454
455 return false;
456 }
457
458 /**
459 * reset the router to Factory Default
460 * not tested
461 *
462 * @return bool
463 */
464 public bool resetToFactoryDefault()
465 {
466 if (checkLogin().Equals(false))
467 return false;
468
469 Thread.Sleep(400);
470
471 string response = sendEnryptedRequest("data/resetAllSetting.json", string.Concat("csrf_token=nulltoken&showpw=0&password=", _hash, "&reset_all=true"));
472 if (response.IsNullOrEmpty())
473 return false;
474
475 try
476 {
477 JToken jArray = JToken.Parse(response);
478 if (jArray.getVar("status").Equals("ok"))
479 {
480 return true;
481 }
482
483 jArray = null;
484 }
485 catch (Exception ex)
486 {
487 LogManager.WriteToLog(ex.ToString());
488 }
489
490 response = null;
491
492 return false;
493 }
494
495 /**
496 * check for firmware update
497 */
498 public void checkFirmware()
499 {
500 if (checkLogin().Equals(false))
501 return;
502
503 Thread.Sleep(400);
504
505 string response = sendRequest("data/checkfirm.json");
506 if (response.IsNullOrEmpty())
507 return;
508
509 try
510 {
511 bool fw_isActual = false;
512 JToken jArray = JToken.Parse(response);
513
514 if (jArray.getVar("fw_isActual").Equals("1"))
515 {
516 fw_isActual = true;
517 }
518
519 if (fw_isActual.Equals(true))
520 {
521 // Die Firmware ist aktuell.
522 MessageBox.Show("Die Firmware ist aktuell.", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
523 }
524 else {
525 // Es liegt eine neuere Firmware-Version vor. Möchten Sie diese Version jetzt installieren?
526 MessageBox.Show("Es liegt eine neuere Firmware-Version vor.\nMöchten Sie diese Version jetzt installieren?", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Warning);
527 }
528
529 jArray = null;
530 }
531 catch (Exception ex)
532 {
533 LogManager.WriteToLog(ex.ToString());
534 }
535
536 response = null;
537 }
538
539 /**
540 * flush dns cache
541 */
542 public void flushDNS()
543 {
544 if (checkLogin().Equals(false))
545 return;
546
547 Thread.Sleep(400);
548
549 string response = sendEnryptedRequest("data/dns.json", "op_type=flush_dns_cache");
550 if (response.IsNullOrEmpty())
551 return;
552
553 try
554 {
555 JToken jArray = JToken.Parse(response);
556
557 if (jArray["DCI"].Count().Equals(0))
558 {
559 new Thread(() => { MessageBox.Show("DNS cache geleert", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information); }).Start();
560 }
561 else {
562 new Thread(() => { MessageBox.Show("unable to flush dns cache", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
563 }
564
565
566 jArray = null;
567 }
568 catch (Exception ex)
569 {
570 LogManager.WriteToLog(ex.ToString());
571 }
572
573 response = null;
574 }
575
576 /**
577 * clear the Syslog
578 */
579 public void clearSyslog()
580 {
581 if (checkLogin().Equals(false))
582 return;
583
584 Thread.Sleep(400);
585
586 string response = sendEnryptedRequest("data/SystemMessages.json", string.Concat("action_clearlist=true&clear_type=0&", "csrf_token=", getToken()));
587 if (response.IsNullOrEmpty())
588 return;
589
590 try
591 {
592 JToken jArray = JToken.Parse(response);
593
594 if (jArray.getVar("status").Equals("ok"))
595 {
596 // ok
597 new Thread(() => { MessageBox.Show("Syslog geleert", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information); }).Start();
598 }
599 else {
600 // fail
601 new Thread(() => { MessageBox.Show("Konnte Syslog nicht leeren.", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
602 }
603
604 jArray = null;
605 }
606 catch (Exception ex)
607 {
608 LogManager.WriteToLog(ex.ToString());
609 }
610
611 response = null;
612 }
613
614 /**
615 * set QueueSkbTimeOut
616 *
617 * @param string value
618 */
619 public void setQueueSkbTimeOut(string value)
620 {
621 if (checkLogin().Equals(false))
622 return;
623
624 string response = sendEnryptedRequest("data/bonding_tr181.json", string.Concat("bonding_QueueSkbTimeOut=", value));
625 if (response.IsNullOrEmpty())
626 return;
627 try
628 {
629 TR181PageModel obj = JsonConvert.DeserializeObject<TR181PageModel>(response);
630
631 if (obj.QueueSkbTimeOut.Equals(value))
632 {
633 new Thread(() => { MessageBox.Show("QueueSkbTimeOut geändert", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information); }).Start();
634 }
635 else {
636 new Thread(() => { MessageBox.Show("unable to change QueueSkbTimeOut", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
637 }
638
639 obj = null;
640 }
641 catch (Exception ex)
642 {
643 LogManager.WriteToLog(ex.ToString());
644 }
645
646 response = null;
647 }
648
649 /**
650 * set Antenna Mode
651 *
652 * @param string value
653 */
654 public void setAntennaMode(string value)
655 {
656 if (checkLogin().Equals(false))
657 return;
658
659 string response = sendEnryptedRequest("data/lteinfo.json", string.Concat("mode_select=", value));
660 if (response.IsNullOrEmpty())
661 return;
662 try
663 {
664 LteInfoModel obj = JsonConvert.DeserializeObject<LteInfoModel>(response);
665
666 string antenna_mode;
667 if (obj.antenna_mode.Equals("Antennal set to internal"))
668 {
669 antenna_mode = "Inner";
670 }
671 else if (obj.antenna_mode.Equals("Antennal set to external"))
672 {
673 antenna_mode = "Outer";
674 }
675 else {
676 antenna_mode = "Auto";
677 }
678
679 if (antenna_mode.Equals(value))
680 {
681 new Thread(() => { MessageBox.Show("Antennen Modus geändert", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information); }).Start();
682 }
683 else {
684 new Thread(() => { MessageBox.Show("Antennen Modus ändern Fehlgeschlagen", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
685 }
686
687 antenna_mode = null;
688
689 obj = null;
690 }
691 catch (Exception ex)
692 {
693 LogManager.WriteToLog(ex.ToString());
694 }
695
696 response = null;
697 }
698
699 /**
700 * get Last Reboot time
701 *
702 * @return DateTime
703 */
704 public DateTime getLastReboot()
705 {
706 if (_lastReboot.Equals(DateTime.MinValue).Equals(false))
707 {
708 return _lastReboot;
709 }
710
711 string response = sendRequest("data/Reboot.json");
712
713 if (response.IsNullOrEmpty())
714 return DateTime.Now;
715
716 JToken jArray = JToken.Parse(response);
717
718 DateTime lastReboot = DateTime.Parse(string.Concat(jArray.getVar("reboot_date"), " ", jArray.getVar("reboot_time")));
719
720 jArray = null;
721
722 return lastReboot;
723 }
724
725 /**
726 * get the csrf token from router
727 *
728 * @return string
729 */
730 public string getToken()
731 {
732 string response = sendRequest("html/content/overview/index.html");
733 if (response.IsNullOrEmpty())
734 return string.Empty;
735
736 string a = "csrf_token = \"";
737 string b = "\";";
738 string token = response.Substring((response.IndexOf(a) + a.Length), (response.IndexOf(b) - response.IndexOf(a) - a.Length));
739
740 response = null;
741 a = null;
742 b = null;
743
744 return token;
745 }
746
747 /**
748 * send encrypted request to the router
749 *
750 * @param string path
751 * @param string post
752 * @param bool cookie
753 * @return string
754 */
755 public string sendEnryptedRequest(string path, string post = "", bool cookie = true)
756 {
757 string response = string.Empty;
758
759 try
760 {
761 sjcl sjcl = new sjcl();
762
763 string iv = _challenge.Substring(16, 16);
764 string adata = _challenge.Substring(32, 16);
765 string dKey = _derivedk;
766
767
768 // TODO: check if we need this really?
769 if (post.IsNullOrEmpty().Equals(false))
770 {
771 post = sjcl.encrypt(dKey, post, iv, adata);
772 }
773
774
775 response = sendRequest(path, post, cookie);
776 // check if the return value is hex (hex = enrypted)
777 if (Regex.IsMatch(response, @"\A\b[0-9a-fA-F]+\b\Z").Equals(true))
778 {
779 response = sjcl.decrypt(dKey, response, iv, adata);
780 }
781
782 post = null;
783 iv = null;
784 adata = null;
785 dKey = null;
786 sjcl = null;
787
788 }
789 catch (ArgumentOutOfRangeException ex)
790 {
791 LogManager.WriteToLog(ex.ToString());
792 }
793 catch (Exception ex)
794 {
795 LogManager.WriteToLog(ex.ToString());
796 }
797
798 return response;
799 }
800
801 /**
802 * send request to the router
803 *
804 * @param string path
805 * @param string post
806 * @param bool cookie
807 * @return string
808 */
809 public string sendRequest(string path, string post = "", bool cookie = true)
810 {
811 string response = string.Empty;
812 try
813 {
814 string url = string.Concat("http://", ip, "/", path, "?lang=de");
815
816 HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
817 /* set timeout to 10 seconds */
818 webRequest.Timeout = 10000;
819
820 if (cookie.Equals(true))
821 {
822 webRequest.CookieContainer = _cookie;
823 }
824
825 if (post.IsNullOrEmpty().Equals(false))
826 {
827 webRequest.Method = "POST";
828 byte[] dataStream = Encoding.UTF8.GetBytes(post);
829 webRequest.ContentLength = dataStream.Length;
830 Stream newStream = webRequest.GetRequestStream();
831 newStream.Write(dataStream, 0, dataStream.Length);
832 newStream.Close();
833 newStream.Dispose();
834 newStream = null;
835 dataStream = null;
836 }
837
838 WebResponse webResponse = webRequest.GetResponse();
839 StreamReader reader = new StreamReader(webResponse.GetResponseStream());
840 response = reader.ReadToEnd().ToString();
841
842 webResponse.Dispose();
843 reader.Dispose();
844 reader = null;
845 webRequest = null;
846 webResponse = null;
847 post = null;
848 }
849 catch (Exception ex)
850 {
851 LogManager.WriteToLog(ex.ToString());
852 }
853
854 return response;
855 }
856
857 public string sendRequest2(string path, Dictionary<string, object> files, string post = "", bool cookie = true)
858 {
859 string response = string.Empty;
860
861 try
862 {
863 string url = string.Concat("http://", ip, "/", path, "?lang=de");
864
865 string boundary = string.Concat("---------------------------", DateTime.Now.Ticks.ToString("x"));
866 byte[] boundaryBytes = Encoding.ASCII.GetBytes(string.Concat("\r\n--", boundary, "\r\n"));
867
868 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
869 request.ContentType = string.Concat("multipart/form-data; boundary=", boundary);
870 request.Method = "POST";
871 request.KeepAlive = true;
872
873 if (cookie.Equals(true))
874 {
875 request.CookieContainer = _cookie;
876 }
877
878 Stream requestStream = request.GetRequestStream();
879
880 if (string.IsNullOrEmpty(post).Equals(false))
881 {
882 byte[] dataStream = Encoding.UTF8.GetBytes(post);
883 requestStream.Write(dataStream, 0, dataStream.Length);
884 dataStream = null;
885 }
886
887 if (files != null && files.Count > 0)
888 {
889 foreach (KeyValuePair<string, object> pair in files)
890 {
891 requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
892 if (pair.Value is FormFile)
893 {
894 FormFile file = pair.Value as FormFile;
895 string header = string.Concat("Content-Disposition: form-data; name=\"", pair.Key, "\"; filename=\"", file.Name, "\"\r\nContent-Type: ", file.ContentType, "\r\n\r\n");
896 byte[] bytes = Encoding.UTF8.GetBytes(header);
897 requestStream.Write(bytes, 0, bytes.Length);
898 byte[] buffer = new byte[32768];
899 int bytesRead;
900 if (file.Stream == null)
901 {
902 // upload from file
903 FileStream fileStream = File.OpenRead(file.FilePath);
904 while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
905 {
906 requestStream.Write(buffer, 0, bytesRead);
907 }
908 fileStream.Close();
909 }
910 else {
911 // upload from given stream
912 while ((bytesRead = file.Stream.Read(buffer, 0, buffer.Length)) != 0)
913 requestStream.Write(buffer, 0, bytesRead);
914 }
915 }
916 else {
917 string data = string.Concat("Content-Disposition: form-data; name=\"", pair.Key, "\"\r\n\r\n", pair.Value);
918 byte[] bytes = Encoding.UTF8.GetBytes(data);
919 requestStream.Write(bytes, 0, bytes.Length);
920 }
921 }
922
923 byte[] trailer = Encoding.ASCII.GetBytes(string.Concat("\r\n--", boundary, "--\r\n"));
924 requestStream.Write(trailer, 0, trailer.Length);
925 requestStream.Close();
926
927 }
928
929 WebResponse webResponse = request.GetResponse();
930 Stream responseStream = webResponse.GetResponseStream();
931 StreamReader reader = new StreamReader(responseStream);
932 response = reader.ReadToEnd();
933
934 webResponse.Dispose();
935 reader.Dispose();
936 reader = null;
937 request = null;
938 webResponse = null;
939 }
940 catch (Exception ex)
941 {
942 LogManager.WriteToLog(ex.ToString());
943 }
944
945 return response;
946 }
947 }
948
949 public class FormFile
950 {
951 public string Name { get; set; }
952
953 public string ContentType { get; set; }
954
955 public string FilePath { get; set; }
956
957 public Stream Stream { get; set; }
958 }
959 }