@@ -61,14 +61,54 @@ public Stream DownloadSubtitle(string hash, string language)
6161 public string UploadSubtitle ( string movieFile , string subfile )
6262 {
6363 string movieHash = Utils . GetHashString ( movieFile ) ;
64- string subText = File . ReadAllText ( subfile , Encoding . UTF8 ) ;
65- byte [ ] textBytes = Encoding . UTF8 . GetBytes ( subText ) ;
64+ string contentBody = GetFormatedBody ( movieHash , File . ReadAllText ( subfile , Encoding . UTF8 ) ) ;
65+ byte [ ] textBytes = Encoding . UTF8 . GetBytes ( contentBody ) ;
6666 string action = $ "?action=upload&hash={ movieHash } ";
6767 HttpWebRequest request = RunRequestGet ( action , WebRequestMethods . Http . Post ) ;
68+ request . ContentType = "multipart/form-data; boundary=xYzZY" ;
69+ request . Headers . Add ( HttpRequestHeader . Pragma , "no-cache" ) ;
6870 request . ContentLength = textBytes . Length ;
69- Stream rs = request . GetRequestStream ( ) ;
70- rs . Write ( textBytes , 0 , textBytes . Length ) ;
71- return ReadResponseStream ( request . GetResponse ( ) ) ;
71+ Stream requestStream = request . GetRequestStream ( ) ;
72+ requestStream . Write ( textBytes , 0 , textBytes . Length ) ;
73+ requestStream . Dispose ( ) ;
74+ try
75+ {
76+ using ( WebResponse responseStream = request . GetResponse ( ) )
77+ {
78+ return ReadResponseStream ( responseStream ) ;
79+ }
80+ }
81+ catch ( WebException ex )
82+ {
83+ return ex . Message ;
84+ }
85+ finally
86+ {
87+ if ( requestStream != null )
88+ {
89+ requestStream . Close ( ) ;
90+ requestStream . Dispose ( ) ;
91+ }
92+ }
93+ }
94+
95+ private static string GetFormatedBody ( string movieHash , string subtitleContent )
96+ {
97+ // PAYLOAD
98+ string boundaryFormat = @"--xYzZY
99+ Content-Disposition: form-data; name=""hash""
100+
101+ {0}
102+ --xYzZY
103+ Content-Disposition: form-data; name=""file""; filename=""{0}.srt""
104+ Content-Type: application/octet-stream
105+ Content-Transfer-Encoding: binary
106+
107+ {1}
108+
109+ --xYzZY
110+ " ;
111+ return string . Format ( boundaryFormat , movieHash , subtitleContent . TrimEnd ( ) ) ;
72112 }
73113
74114 public HttpWebRequest RunRequestGet ( string action , string method )
@@ -86,5 +126,6 @@ private static string ReadResponseStream(WebResponse reponseStream)
86126 return sr . ReadToEnd ( ) ;
87127 }
88128 }
129+
89130 }
90131}
0 commit comments