Removed SendRequestStream method

This commit is contained in:
Jaex 2016-12-23 10:24:38 +03:00
parent 09886bc3f3
commit a0434e8c91
5 changed files with 28 additions and 36 deletions

View file

@ -126,7 +126,7 @@ public virtual void StopUpload()
} }
} }
protected string SendRequest(HttpMethod method, string url, Dictionary<string, string> arguments = null, NameValueCollection headers = null, protected string SendRequest(HttpMethod method, string url, Dictionary<string, string> args = null, NameValueCollection headers = null,
CookieCollection cookies = null, ResponseType responseType = ResponseType.Text) CookieCollection cookies = null, ResponseType responseType = ResponseType.Text)
{ {
HttpWebResponse response = null; HttpWebResponse response = null;
@ -135,11 +135,11 @@ protected string SendRequest(HttpMethod method, string url, Dictionary<string, s
{ {
if (method == HttpMethod.POST) // Multipart form data if (method == HttpMethod.POST) // Multipart form data
{ {
response = SendRequestMultiPart(url, arguments, headers, cookies); response = SendRequestMultiPart(url, args, headers, cookies);
} }
else else
{ {
response = GetResponse(method, url, null, null, arguments, headers, cookies); response = GetResponse(method, url, null, null, args, headers, cookies);
} }
return ResponseToString(response, responseType); return ResponseToString(response, responseType);
@ -153,7 +153,7 @@ protected string SendRequest(HttpMethod method, string url, Dictionary<string, s
} }
} }
protected string SendRequest(HttpMethod method, string url, string content, Dictionary<string, string> arguments = null, NameValueCollection headers = null, protected string SendRequest(HttpMethod method, string url, string content, Dictionary<string, string> args = null, NameValueCollection headers = null,
CookieCollection cookies = null, ResponseType responseType = ResponseType.Text) CookieCollection cookies = null, ResponseType responseType = ResponseType.Text)
{ {
byte[] data = Encoding.UTF8.GetBytes(content); 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()) using (MemoryStream ms = new MemoryStream())
{ {
ms.Write(data, 0, data.Length); 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<string, string> arguments = null, NameValueCollection headers = null, protected string SendRequest(HttpMethod method, string url, Stream data, string contentType = null, Dictionary<string, string> args = null, NameValueCollection headers = null,
CookieCollection cookies = null, ResponseType responseType = ResponseType.Text) 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); return ResponseToString(response, responseType);
} }
} }
protected string SendRequestStream(string url, Stream stream, string contentType, NameValueCollection headers = null, protected bool SendRequest(HttpMethod method, Stream downloadStream, string url, Dictionary<string, string> args = 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<string, string> arguments = null,
NameValueCollection headers = null, CookieCollection cookies = null, string contentType = 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) if (response != null)
{ {
@ -210,7 +202,7 @@ protected string SendRequestJSON(string url, string json, NameValueCollection he
stream = new MemoryStream(data); stream = new MemoryStream(data);
} }
return SendRequestStream(url, stream, ContentTypeJSON, headers, cookies, method); return SendRequest(method, url, stream, ContentTypeJSON, null, headers, cookies);
} }
finally finally
{ {
@ -218,24 +210,24 @@ protected string SendRequestJSON(string url, string json, NameValueCollection he
} }
} }
protected string SendRequestURLEncoded(string url, Dictionary<string, string> arguments, NameValueCollection headers = null, CookieCollection cookies = null, protected string SendRequestURLEncoded(string url, Dictionary<string, string> args, NameValueCollection headers = null, CookieCollection cookies = null,
HttpMethod method = HttpMethod.POST, ResponseType responseType = ResponseType.Text) HttpMethod method = HttpMethod.POST, ResponseType responseType = ResponseType.Text)
{ {
string query = CreateQuery(arguments); string query = CreateQuery(args);
byte[] data = Encoding.UTF8.GetBytes(query); byte[] data = Encoding.UTF8.GetBytes(query);
using (MemoryStream stream = new MemoryStream()) using (MemoryStream stream = new MemoryStream())
{ {
stream.Write(data, 0, data.Length); 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) 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) if (response != null)
{ {
@ -246,12 +238,12 @@ protected NameValueCollection SendRequestStreamGetHeaders(string url, Stream str
} }
} }
private HttpWebResponse SendRequestMultiPart(string url, Dictionary<string, string> arguments, NameValueCollection headers = null, CookieCollection cookies = null, private HttpWebResponse SendRequestMultiPart(string url, Dictionary<string, string> args, NameValueCollection headers = null, CookieCollection cookies = null,
HttpMethod method = HttpMethod.POST) HttpMethod method = HttpMethod.POST)
{ {
string boundary = CreateBoundary(); string boundary = CreateBoundary();
string contentType = ContentTypeMultipartFormData + "; boundary=" + boundary; string contentType = ContentTypeMultipartFormData + "; boundary=" + boundary;
byte[] data = MakeInputContent(boundary, arguments); byte[] data = MakeInputContent(boundary, args);
using (MemoryStream stream = new MemoryStream()) using (MemoryStream stream = new MemoryStream())
{ {
@ -309,7 +301,7 @@ private HttpWebResponse GetResponse(HttpMethod method, string url, Stream data =
return null; return null;
} }
protected UploadResult UploadData(Stream dataStream, string url, string fileName, string fileFormName = "file", Dictionary<string, string> arguments = null, protected UploadResult UploadData(Stream data, string url, string fileName, string fileFormName = "file", Dictionary<string, string> args = null,
NameValueCollection headers = null, CookieCollection cookies = null, ResponseType responseType = ResponseType.Text, HttpMethod method = HttpMethod.POST, NameValueCollection headers = null, CookieCollection cookies = null, ResponseType responseType = ResponseType.Text, HttpMethod method = HttpMethod.POST,
string contentType = ContentTypeMultipartFormData, string metadata = null) string contentType = ContentTypeMultipartFormData, string metadata = null)
{ {
@ -323,7 +315,7 @@ protected UploadResult UploadData(Stream dataStream, string url, string fileName
string boundary = CreateBoundary(); string boundary = CreateBoundary();
contentType += "; boundary=" + boundary; contentType += "; boundary=" + boundary;
byte[] bytesArguments = MakeInputContent(boundary, arguments, false); byte[] bytesArguments = MakeInputContent(boundary, args, false);
byte[] bytesDataOpen; byte[] bytesDataOpen;
byte[] bytesDataDatafile = { }; byte[] bytesDataDatafile = { };
@ -339,7 +331,7 @@ protected UploadResult UploadData(Stream dataStream, string url, string fileName
byte[] bytesDataClose = MakeFileInputContentClose(boundary); 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); HttpWebRequest request = PrepareWebRequest(method, url, headers, cookies, contentType, contentLength);
using (Stream requestStream = request.GetRequestStream()) 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(bytesArguments, 0, bytesArguments.Length);
requestStream.Write(bytesDataOpen, 0, bytesDataOpen.Length); requestStream.Write(bytesDataOpen, 0, bytesDataOpen.Length);
requestStream.Write(bytesDataDatafile, 0, bytesDataDatafile.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); requestStream.Write(bytesDataClose, 0, bytesDataClose.Length);
} }

View file

@ -160,7 +160,7 @@ public string PostRequestRaw(Uri url, Stream dataStream)
try try
{ {
AllowReportProgress = true; AllowReportProgress = true;
return SendRequestStream(url.ToString(), dataStream, "application/octet-stream"); return SendRequest(HttpMethod.POST, url.ToString(), dataStream, "application/octet-stream");
} }
finally finally
{ {

View file

@ -104,7 +104,7 @@ public override UploadResult Upload(Stream stream, string fileName)
url = URLHelpers.FixPrefix(url); url = URLHelpers.FixPrefix(url);
NameValueCollection headers = CreateAuthenticationHeader(Username, Password); 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); UploadResult result = new UploadResult(response);

View file

@ -204,7 +204,7 @@ public override UploadResult Upload(Stream stream, string fileName)
NameValueCollection headers = GetAuthHeaders(); NameValueCollection headers = GetAuthHeaders();
headers.Add("Slug", URLHelpers.URLEncode(fileName)); 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) if (ur.Response != null)
{ {

View file

@ -57,10 +57,10 @@ public sealed class VgymeUploader : ImageUploader
public override UploadResult Upload(Stream stream, string fileName) public override UploadResult Upload(Stream stream, string fileName)
{ {
Dictionary<string, string> arguments = new Dictionary<string, string>(); Dictionary<string, string> args = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(UserKey)) arguments.Add("userkey", UserKey); 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) if (result.IsSuccess)
{ {