Dropbox download file update

This commit is contained in:
Jaex 2016-06-29 04:09:31 +03:00
parent d0055bb819
commit 25a1bbcf0c
3 changed files with 19 additions and 12 deletions

View file

@ -40,6 +40,10 @@ public class Uploader
{
private static readonly string UserAgent = "ShareX";
public string ContentTypeMultipartFormData = "multipart/form-data";
public string ContentTypeJSON = "application/json";
public string ContentTypeURLEncoded = "application/x-www-form-urlencoded";
public delegate void ProgressEventHandler(ProgressManager progress);
public event ProgressEventHandler ProgressChanged;
@ -170,9 +174,9 @@ protected string SendRequest(HttpMethod method, string url, Stream content, Dict
}
protected bool SendRequest(HttpMethod method, Stream downloadStream, string url, Dictionary<string, string> arguments = null,
NameValueCollection headers = null, CookieCollection cookies = null)
NameValueCollection headers = null, CookieCollection cookies = null, string contentType = null)
{
using (HttpWebResponse response = GetResponse(method, url, arguments, headers, cookies))
using (HttpWebResponse response = GetResponse(method, url, arguments, headers, cookies, null, contentType))
{
if (response != null)
{
@ -185,7 +189,7 @@ protected bool SendRequest(HttpMethod method, Stream downloadStream, string url,
}
private HttpWebResponse GetResponse(HttpMethod method, string url, Dictionary<string, string> arguments = null, NameValueCollection headers = null,
CookieCollection cookies = null, Stream dataStream = null)
CookieCollection cookies = null, Stream dataStream = null, string contentType = null)
{
IsUploading = true;
StopUploadRequested = false;
@ -194,7 +198,7 @@ private HttpWebResponse GetResponse(HttpMethod method, string url, Dictionary<st
try
{
HttpWebRequest request = PrepareWebRequest(method, url, headers, cookies);
HttpWebRequest request = PrepareWebRequest(method, url, headers, cookies, contentType);
if (dataStream != null)
{
@ -238,7 +242,7 @@ protected string SendRequestJSON(string url, string json, NameValueCollection he
stream = new MemoryStream(data);
}
return SendRequestStream(url, stream, "application/json", headers, cookies, method);
return SendRequestStream(url, stream, ContentTypeJSON, headers, cookies, method);
}
finally
{
@ -256,7 +260,7 @@ protected string SendRequestURLEncoded(string url, Dictionary<string, string> ar
{
stream.Write(data, 0, data.Length);
return SendRequestStream(url, stream, "application/x-www-form-urlencoded", headers, cookies, method, responseType);
return SendRequestStream(url, stream, ContentTypeURLEncoded, headers, cookies, method, responseType);
}
}
@ -287,7 +291,7 @@ private HttpWebResponse SendRequestMultiPart(string url, Dictionary<string, stri
HttpMethod method = HttpMethod.POST)
{
string boundary = CreateBoundary();
string contentType = "multipart/form-data; boundary=" + boundary;
string contentType = ContentTypeMultipartFormData + "; boundary=" + boundary;
byte[] data = MakeInputContent(boundary, arguments);
using (MemoryStream stream = new MemoryStream())
@ -343,7 +347,7 @@ private HttpWebResponse GetResponse(string url, Stream dataStream, string conten
protected UploadResult UploadData(Stream dataStream, string url, string fileName, string fileFormName = "file", Dictionary<string, string> arguments = null,
NameValueCollection headers = null, CookieCollection cookies = null, ResponseType responseType = ResponseType.Text, HttpMethod method = HttpMethod.POST,
string contentType = "multipart/form-data", string metadata = null)
string contentType = ContentTypeMultipartFormData, string metadata = null)
{
UploadResult result = new UploadResult();
@ -535,7 +539,7 @@ private byte[] MakeFileInputContentOpen(string boundary, string fileFormName, st
if (metadata != null)
{
format = string.Format("--{0}\r\nContent-Type: {1}; charset=UTF-8\r\n\r\n{2}\r\n\r\n", boundary, "application/json", metadata);
format = string.Format("--{0}\r\nContent-Type: {1}; charset=UTF-8\r\n\r\n{2}\r\n\r\n", boundary, ContentTypeJSON, metadata);
}
else
{

View file

@ -175,8 +175,11 @@ public bool DownloadFile(string path, Stream downloadStream)
{
if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo))
{
string url = URLHelpers.CombineURL(URLDownload, URLHelpers.URLPathEncode(path));
return SendRequest(HttpMethod.GET, downloadStream, url, headers: GetAuthHeaders());
NameValueCollection headers = GetAuthHeaders();
path = URLHelpers.AddSlash(path, SlashType.Prefix);
string arg = JsonConvert.SerializeObject(new DropboxPath(path));
return SendRequest(HttpMethod.POST, downloadStream, URLDownload, headers: headers, contentType: ContentTypeJSON);
}
return false;

View file

@ -99,7 +99,7 @@ public bool GetAccessToken(string code)
args.Add("code", code);
WebHeaderCollection headers = new WebHeaderCollection();
headers.Add("Accept", "application/json");
headers.Add("Accept", ContentTypeJSON);
string response = SendRequest(HttpMethod.POST, "https://github.com/login/oauth/access_token", args, headers);