mirror of
https://github.com/ShareX/ShareX.git
synced 2024-06-30 20:20:34 +12:00
fixed #39: Added response headers option to custom uploader
This commit is contained in:
parent
cb68f36804
commit
65f0a5ea2b
|
@ -157,7 +157,9 @@ public enum ResponseType
|
|||
[Description("Response text")]
|
||||
Text,
|
||||
[Description("Redirection URL")]
|
||||
RedirectionURL
|
||||
RedirectionURL,
|
||||
[Description("Response headers")]
|
||||
Headers
|
||||
}
|
||||
|
||||
public enum ProxyMethod
|
||||
|
|
|
@ -44,7 +44,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
if (string.IsNullOrEmpty(customUploader.FileFormName)) throw new Exception("'File form name' must be not empty when using custom file uploader.");
|
||||
if (string.IsNullOrEmpty(customUploader.RequestURL)) throw new Exception("'Request URL' must be not empty.");
|
||||
|
||||
UploadResult result = UploadData(stream, customUploader.RequestURL, fileName, customUploader.FileFormName, customUploader.ParseArguments());
|
||||
UploadResult result = UploadData(stream, customUploader.RequestURL, fileName, customUploader.FileFormName, customUploader.ParseArguments(), responseType: customUploader.ResponseType);
|
||||
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
|
|
|
@ -1406,6 +1406,8 @@ private async void TestCustomUploader(CustomUploaderType type, CustomUploaderIte
|
|||
{
|
||||
UploadResult result = null;
|
||||
|
||||
txtCustomUploaderLog.ResetText();
|
||||
|
||||
await TaskEx.Run(() =>
|
||||
{
|
||||
try
|
||||
|
|
|
@ -44,7 +44,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
if (string.IsNullOrEmpty(customUploader.FileFormName)) throw new Exception("'File form name' must be not empty when using custom image uploader.");
|
||||
if (string.IsNullOrEmpty(customUploader.RequestURL)) throw new Exception("'Request URL' must be not empty.");
|
||||
|
||||
UploadResult result = UploadData(stream, customUploader.RequestURL, fileName, customUploader.FileFormName, customUploader.ParseArguments());
|
||||
UploadResult result = UploadData(stream, customUploader.RequestURL, fileName, customUploader.FileFormName, customUploader.ParseArguments(), responseType: customUploader.ResponseType);
|
||||
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
|
|
|
@ -64,7 +64,7 @@ public override UploadResult UploadText(string text, string fileName)
|
|||
byte[] byteArray = Encoding.UTF8.GetBytes(text);
|
||||
using (MemoryStream stream = new MemoryStream(byteArray))
|
||||
{
|
||||
result = UploadData(stream, customUploader.RequestURL, fileName, customUploader.FileFormName, args);
|
||||
result = UploadData(stream, customUploader.RequestURL, fileName, customUploader.FileFormName, args, responseType: customUploader.ResponseType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -209,7 +209,7 @@ private HttpWebResponse PostResponseJSON(string url, string json)
|
|||
|
||||
protected UploadResult UploadData(Stream dataStream, string url, string fileName, string fileFormName = "file",
|
||||
Dictionary<string, string> arguments = null, CookieCollection cookies = null, NameValueCollection headers = null,
|
||||
bool suppressWebExceptions = true)
|
||||
ResponseType responseType = ResponseType.Text, bool suppressWebExceptions = true)
|
||||
{
|
||||
UploadResult result = new UploadResult();
|
||||
|
||||
|
@ -235,13 +235,14 @@ private HttpWebResponse PostResponseJSON(string url, string json)
|
|||
requestStream.Write(bytesDataClose, 0, bytesDataClose.Length);
|
||||
}
|
||||
|
||||
result.Response = ResponseToString(request.GetResponse());
|
||||
result.Response = ResponseToString(request.GetResponse(), responseType);
|
||||
result.IsSuccess = true;
|
||||
}
|
||||
catch (WebException e)
|
||||
{
|
||||
if (!suppressWebExceptions)
|
||||
throw;
|
||||
|
||||
if (!stopUpload) result.Response = AddWebError(e);
|
||||
}
|
||||
catch (Exception e)
|
||||
|
@ -497,6 +498,14 @@ private string ResponseToString(WebResponse response, ResponseType responseType
|
|||
}
|
||||
case ResponseType.RedirectionURL:
|
||||
return response.ResponseUri.OriginalString;
|
||||
case ResponseType.Headers:
|
||||
StringBuilder sbHeaders = new StringBuilder();
|
||||
foreach (string key in response.Headers.AllKeys)
|
||||
{
|
||||
string value = response.Headers[key];
|
||||
sbHeaders.AppendFormat("{0}: \"{1}\"{2}", key, value, Environment.NewLine);
|
||||
}
|
||||
return sbHeaders.ToString().Trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -599,7 +608,7 @@ private string AddWebError(Exception e)
|
|||
str.AppendLine("StackTrace:");
|
||||
str.AppendLine(e.StackTrace);
|
||||
|
||||
string errorText = str.ToString();
|
||||
string errorText = str.ToString().Trim();
|
||||
Errors.Add(errorText);
|
||||
DebugHelper.WriteLine("AddWebError(): " + errorText);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue