From a0434e8c914c14cf055512eb750f540f3bcf9294 Mon Sep 17 00:00:00 2001 From: Jaex Date: Fri, 23 Dec 2016 10:24:38 +0300 Subject: [PATCH] Removed SendRequestStream method --- ShareX.UploadersLib/BaseUploaders/Uploader.cs | 52 ++++++++----------- ShareX.UploadersLib/FileUploaders/Mega.cs | 2 +- ShareX.UploadersLib/FileUploaders/OwnCloud.cs | 2 +- ShareX.UploadersLib/ImageUploaders/Picasa.cs | 2 +- .../ImageUploaders/VgymeUploader.cs | 6 +-- 5 files changed, 28 insertions(+), 36 deletions(-) diff --git a/ShareX.UploadersLib/BaseUploaders/Uploader.cs b/ShareX.UploadersLib/BaseUploaders/Uploader.cs index 5bad6e9c6..43a3063ef 100644 --- a/ShareX.UploadersLib/BaseUploaders/Uploader.cs +++ b/ShareX.UploadersLib/BaseUploaders/Uploader.cs @@ -126,7 +126,7 @@ public virtual void StopUpload() } } - protected string SendRequest(HttpMethod method, string url, Dictionary arguments = null, NameValueCollection headers = null, + protected string SendRequest(HttpMethod method, string url, Dictionary args = null, NameValueCollection headers = null, CookieCollection cookies = null, ResponseType responseType = ResponseType.Text) { HttpWebResponse response = null; @@ -135,11 +135,11 @@ protected string SendRequest(HttpMethod method, string url, Dictionary arguments = null, NameValueCollection headers = null, + protected string SendRequest(HttpMethod method, string url, string content, Dictionary args = null, NameValueCollection headers = null, CookieCollection cookies = null, ResponseType responseType = ResponseType.Text) { byte[] data = Encoding.UTF8.GetBytes(content); @@ -161,32 +161,24 @@ protected string SendRequest(HttpMethod method, string url, string content, Dict using (MemoryStream ms = new MemoryStream()) { ms.Write(data, 0, data.Length); - return SendRequest(method, url, ms, arguments, headers, cookies, responseType); + + return SendRequest(method, url, ms, null, args, headers, cookies, responseType); } } - protected string SendRequest(HttpMethod method, string url, Stream content, Dictionary arguments = null, NameValueCollection headers = null, + protected string SendRequest(HttpMethod method, string url, Stream data, string contentType = null, Dictionary args = null, NameValueCollection headers = null, CookieCollection cookies = null, ResponseType responseType = ResponseType.Text) { - using (HttpWebResponse response = GetResponse(method, url, content, null, arguments, headers, cookies)) + using (HttpWebResponse response = GetResponse(method, url, data, contentType, args, headers, cookies)) { return ResponseToString(response, responseType); } } - protected string SendRequestStream(string url, Stream stream, string contentType, NameValueCollection headers = null, - CookieCollection cookies = null, HttpMethod method = HttpMethod.POST, ResponseType responseType = ResponseType.Text) - { - using (HttpWebResponse response = GetResponse(method, url, stream, contentType, null, headers, cookies)) - { - return ResponseToString(response, responseType); - } - } - - protected bool SendRequest(HttpMethod method, Stream downloadStream, string url, Dictionary arguments = null, + protected bool SendRequest(HttpMethod method, Stream downloadStream, string url, Dictionary args = null, NameValueCollection headers = null, CookieCollection cookies = null, string contentType = null) { - using (HttpWebResponse response = GetResponse(method, url, null, contentType, arguments, headers, cookies)) + using (HttpWebResponse response = GetResponse(method, url, null, contentType, args, headers, cookies)) { if (response != null) { @@ -210,7 +202,7 @@ protected string SendRequestJSON(string url, string json, NameValueCollection he stream = new MemoryStream(data); } - return SendRequestStream(url, stream, ContentTypeJSON, headers, cookies, method); + return SendRequest(method, url, stream, ContentTypeJSON, null, headers, cookies); } finally { @@ -218,24 +210,24 @@ protected string SendRequestJSON(string url, string json, NameValueCollection he } } - protected string SendRequestURLEncoded(string url, Dictionary arguments, NameValueCollection headers = null, CookieCollection cookies = null, + protected string SendRequestURLEncoded(string url, Dictionary args, NameValueCollection headers = null, CookieCollection cookies = null, HttpMethod method = HttpMethod.POST, ResponseType responseType = ResponseType.Text) { - string query = CreateQuery(arguments); + string query = CreateQuery(args); byte[] data = Encoding.UTF8.GetBytes(query); using (MemoryStream stream = new MemoryStream()) { stream.Write(data, 0, data.Length); - return SendRequestStream(url, stream, ContentTypeURLEncoded, headers, cookies, method, responseType); + return SendRequest(method, url, stream, ContentTypeURLEncoded, null, headers, cookies, responseType); } } - protected NameValueCollection SendRequestStreamGetHeaders(string url, Stream stream, string contentType, NameValueCollection headers = null, + protected NameValueCollection SendRequestStreamGetHeaders(string url, Stream data, string contentType, NameValueCollection headers = null, CookieCollection cookies = null, HttpMethod method = HttpMethod.POST) { - using (HttpWebResponse response = GetResponse(method, url, stream, contentType, null, headers, cookies)) + using (HttpWebResponse response = GetResponse(method, url, data, contentType, null, headers, cookies)) { if (response != null) { @@ -246,12 +238,12 @@ protected NameValueCollection SendRequestStreamGetHeaders(string url, Stream str } } - private HttpWebResponse SendRequestMultiPart(string url, Dictionary arguments, NameValueCollection headers = null, CookieCollection cookies = null, + private HttpWebResponse SendRequestMultiPart(string url, Dictionary args, NameValueCollection headers = null, CookieCollection cookies = null, HttpMethod method = HttpMethod.POST) { string boundary = CreateBoundary(); string contentType = ContentTypeMultipartFormData + "; boundary=" + boundary; - byte[] data = MakeInputContent(boundary, arguments); + byte[] data = MakeInputContent(boundary, args); using (MemoryStream stream = new MemoryStream()) { @@ -309,7 +301,7 @@ private HttpWebResponse GetResponse(HttpMethod method, string url, Stream data = return null; } - protected UploadResult UploadData(Stream dataStream, string url, string fileName, string fileFormName = "file", Dictionary arguments = null, + protected UploadResult UploadData(Stream data, string url, string fileName, string fileFormName = "file", Dictionary args = null, NameValueCollection headers = null, CookieCollection cookies = null, ResponseType responseType = ResponseType.Text, HttpMethod method = HttpMethod.POST, string contentType = ContentTypeMultipartFormData, string metadata = null) { @@ -323,7 +315,7 @@ protected UploadResult UploadData(Stream dataStream, string url, string fileName string boundary = CreateBoundary(); contentType += "; boundary=" + boundary; - byte[] bytesArguments = MakeInputContent(boundary, arguments, false); + byte[] bytesArguments = MakeInputContent(boundary, args, false); byte[] bytesDataOpen; byte[] bytesDataDatafile = { }; @@ -339,7 +331,7 @@ protected UploadResult UploadData(Stream dataStream, string url, string fileName byte[] bytesDataClose = MakeFileInputContentClose(boundary); - long contentLength = bytesArguments.Length + bytesDataOpen.Length + bytesDataDatafile.Length + dataStream.Length + bytesDataClose.Length; + long contentLength = bytesArguments.Length + bytesDataOpen.Length + bytesDataDatafile.Length + data.Length + bytesDataClose.Length; HttpWebRequest request = PrepareWebRequest(method, url, headers, cookies, contentType, contentLength); using (Stream requestStream = request.GetRequestStream()) @@ -347,7 +339,7 @@ protected UploadResult UploadData(Stream dataStream, string url, string fileName requestStream.Write(bytesArguments, 0, bytesArguments.Length); requestStream.Write(bytesDataOpen, 0, bytesDataOpen.Length); requestStream.Write(bytesDataDatafile, 0, bytesDataDatafile.Length); - if (!TransferData(dataStream, requestStream)) return null; + if (!TransferData(data, requestStream)) return null; requestStream.Write(bytesDataClose, 0, bytesDataClose.Length); } diff --git a/ShareX.UploadersLib/FileUploaders/Mega.cs b/ShareX.UploadersLib/FileUploaders/Mega.cs index 406fd3bb2..5f240f1f5 100644 --- a/ShareX.UploadersLib/FileUploaders/Mega.cs +++ b/ShareX.UploadersLib/FileUploaders/Mega.cs @@ -160,7 +160,7 @@ public string PostRequestRaw(Uri url, Stream dataStream) try { AllowReportProgress = true; - return SendRequestStream(url.ToString(), dataStream, "application/octet-stream"); + return SendRequest(HttpMethod.POST, url.ToString(), dataStream, "application/octet-stream"); } finally { diff --git a/ShareX.UploadersLib/FileUploaders/OwnCloud.cs b/ShareX.UploadersLib/FileUploaders/OwnCloud.cs index bf58e0218..531065070 100644 --- a/ShareX.UploadersLib/FileUploaders/OwnCloud.cs +++ b/ShareX.UploadersLib/FileUploaders/OwnCloud.cs @@ -104,7 +104,7 @@ public override UploadResult Upload(Stream stream, string fileName) url = URLHelpers.FixPrefix(url); NameValueCollection headers = CreateAuthenticationHeader(Username, Password); - string response = SendRequestStream(url, stream, Helpers.GetMimeType(fileName), headers, method: HttpMethod.PUT); + string response = SendRequest(HttpMethod.PUT, url, stream, Helpers.GetMimeType(fileName), null, headers); UploadResult result = new UploadResult(response); diff --git a/ShareX.UploadersLib/ImageUploaders/Picasa.cs b/ShareX.UploadersLib/ImageUploaders/Picasa.cs index 13f969684..0e260b335 100644 --- a/ShareX.UploadersLib/ImageUploaders/Picasa.cs +++ b/ShareX.UploadersLib/ImageUploaders/Picasa.cs @@ -204,7 +204,7 @@ public override UploadResult Upload(Stream stream, string fileName) NameValueCollection headers = GetAuthHeaders(); headers.Add("Slug", URLHelpers.URLEncode(fileName)); - ur.Response = SendRequestStream(url, stream, contentType, headers); + ur.Response = SendRequest(HttpMethod.POST, url, stream, contentType, null, headers); if (ur.Response != null) { diff --git a/ShareX.UploadersLib/ImageUploaders/VgymeUploader.cs b/ShareX.UploadersLib/ImageUploaders/VgymeUploader.cs index 5f9df6226..5174fe2f5 100644 --- a/ShareX.UploadersLib/ImageUploaders/VgymeUploader.cs +++ b/ShareX.UploadersLib/ImageUploaders/VgymeUploader.cs @@ -57,10 +57,10 @@ public sealed class VgymeUploader : ImageUploader public override UploadResult Upload(Stream stream, string fileName) { - Dictionary arguments = new Dictionary(); - if (!string.IsNullOrEmpty(UserKey)) arguments.Add("userkey", UserKey); + Dictionary args = new Dictionary(); + if (!string.IsNullOrEmpty(UserKey)) args.Add("userkey", UserKey); - UploadResult result = UploadData(stream, "https://vgy.me/upload", fileName, arguments: arguments); + UploadResult result = UploadData(stream, "https://vgy.me/upload", fileName, "file", args); if (result.IsSuccess) {