2015-06-08 01:34:33 +12:00
#region License Information ( GPL v3 )
/ *
ShareX - A program that allows you to take screenshots and share any file type
2018-01-02 03:59:14 +13:00
Copyright ( c ) 2007 - 2018 ShareX Team
2015-06-08 01:34:33 +12:00
This program is free software ; you can redistribute it and / or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation ; either version 2
of the License , or ( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
Optionally you can also view the license at < http : //www.gnu.org/licenses/>.
* /
#endregion License Information ( GPL v3 )
using ShareX.HelpersLib ;
using System ;
using System.Collections.Generic ;
using System.Collections.Specialized ;
using System.IO ;
using System.Net ;
using System.Net.Cache ;
using System.Text ;
namespace ShareX.UploadersLib
{
public class Uploader
{
2016-12-23 21:21:37 +13:00
protected const string ContentTypeMultipartFormData = "multipart/form-data" ;
protected const string ContentTypeJSON = "application/json" ;
protected const string ContentTypeURLEncoded = "application/x-www-form-urlencoded" ;
protected const string ContentTypeOctetStream = "application/octet-stream" ;
2015-06-08 01:34:33 +12:00
public delegate void ProgressEventHandler ( ProgressManager progress ) ;
public event ProgressEventHandler ProgressChanged ;
2015-12-10 07:32:26 +13:00
public event Action < string > EarlyURLCopyRequested ;
2015-06-08 01:34:33 +12:00
public bool IsUploading { get ; protected set ; }
2016-12-29 00:23:01 +13:00
public List < string > Errors { get ; private set ; } = new List < string > ( ) ;
2016-12-23 21:51:13 +13:00
public bool IsError = > ! StopUploadRequested & & Errors ! = null & & Errors . Count > 0 ;
2016-12-29 00:23:01 +13:00
public int BufferSize { get ; set ; } = 8192 ;
2017-10-17 04:01:58 +13:00
public bool VerboseLogs { get ; set ; }
public string VerboseLogsPath { get ; set ; }
2015-06-08 01:34:33 +12:00
2016-12-23 21:51:13 +13:00
protected bool StopUploadRequested { get ; set ; }
2016-12-29 00:23:01 +13:00
protected bool AllowReportProgress { get ; set ; } = true ;
2017-02-03 19:04:39 +13:00
protected bool ReturnResponseOnError { get ; set ; }
2016-12-23 21:21:37 +13:00
2015-06-08 01:34:33 +12:00
private HttpWebRequest currentRequest ;
2017-10-17 04:01:58 +13:00
private Logger verboseLogger ;
2015-06-08 01:34:33 +12:00
2017-02-10 01:52:07 +13:00
public static void UpdateServicePointManager ( )
2015-06-08 01:34:33 +12:00
{
ServicePointManager . DefaultConnectionLimit = 25 ;
ServicePointManager . Expect100Continue = false ;
ServicePointManager . UseNagleAlgorithm = false ;
2017-02-02 10:09:20 +13:00
try
{
// Default value for .NET Framework 4.0 and 4.5: SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls
// Default value for .NET Framework 4.6: SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12
ServicePointManager . SecurityProtocol = ( SecurityProtocolType ) 192 | ( SecurityProtocolType ) 768 | ( SecurityProtocolType ) 3072 ;
}
catch ( NotSupportedException )
{
2017-02-05 20:52:53 +13:00
DebugHelper . WriteLine ( "Unable to configure TLS 1.2 as the default security protocol. .NET Framework 4.5 or newer version must be installed in your system to support it." ) ;
2017-02-02 10:09:20 +13:00
}
2015-06-08 01:34:33 +12:00
}
protected void OnProgressChanged ( ProgressManager progress )
{
if ( ProgressChanged ! = null )
{
ProgressChanged ( progress ) ;
}
}
2015-12-10 07:32:26 +13:00
protected void OnEarlyURLCopyRequested ( string url )
{
if ( EarlyURLCopyRequested ! = null & & ! string . IsNullOrEmpty ( url ) )
{
EarlyURLCopyRequested ( url ) ;
}
}
2015-06-08 01:34:33 +12:00
public string ToErrorString ( )
{
if ( IsError )
{
return string . Join ( Environment . NewLine , Errors ) ;
}
2016-05-25 06:15:45 +12:00
return "" ;
2015-06-08 01:34:33 +12:00
}
public virtual void StopUpload ( )
{
if ( IsUploading )
{
StopUploadRequested = true ;
if ( currentRequest ! = null )
{
try
{
currentRequest . Abort ( ) ;
}
catch ( Exception e )
{
DebugHelper . WriteException ( e ) ;
}
}
}
}
2016-12-23 20:24:38 +13:00
protected string SendRequest ( HttpMethod method , string url , Dictionary < string , string > args = null , NameValueCollection headers = null ,
2015-06-08 01:34:33 +12:00
CookieCollection cookies = null , ResponseType responseType = ResponseType . Text )
{
2016-12-27 05:47:46 +13:00
return SendRequest ( method , url , ( Stream ) null , null , args , headers , cookies , responseType ) ;
2016-12-27 05:30:17 +13:00
}
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 )
{
2017-10-17 04:01:58 +13:00
using ( HttpWebResponse webResponse = GetResponse ( method , url , data , contentType , args , headers , cookies ) )
2015-06-08 01:34:33 +12:00
{
2017-10-17 04:01:58 +13:00
string response = ResponseToString ( webResponse , responseType ) ;
if ( VerboseLogs & & ! string . IsNullOrEmpty ( VerboseLogsPath ) )
{
WriteVerboseLog ( url , args , headers , response ) ;
}
return response ;
2015-06-08 01:34:33 +12:00
}
}
2016-12-23 20:55:16 +13:00
protected string SendRequest ( HttpMethod method , string url , string content , string contentType = null , Dictionary < string , string > args = null , NameValueCollection headers = null ,
2015-06-08 01:34:33 +12:00
CookieCollection cookies = null , ResponseType responseType = ResponseType . Text )
{
byte [ ] data = Encoding . UTF8 . GetBytes ( content ) ;
using ( MemoryStream ms = new MemoryStream ( ) )
{
ms . Write ( data , 0 , data . Length ) ;
2016-12-23 20:55:16 +13:00
return SendRequest ( method , url , ms , contentType , args , headers , cookies , responseType ) ;
2016-12-23 14:11:01 +13:00
}
}
2016-12-23 21:21:37 +13:00
protected string SendRequestURLEncoded ( HttpMethod method , string url , Dictionary < string , string > args , NameValueCollection headers = null , CookieCollection cookies = null ,
ResponseType responseType = ResponseType . Text )
2015-06-08 01:34:33 +12:00
{
2017-09-24 20:14:27 +13:00
string query = URLHelpers . CreateQuery ( args ) ;
2015-06-08 01:34:33 +12:00
2016-12-23 21:51:13 +13:00
return SendRequest ( method , url , query , ContentTypeURLEncoded , args , headers , cookies , responseType ) ;
2015-06-08 01:34:33 +12:00
}
2016-12-23 21:21:37 +13:00
protected NameValueCollection SendRequestGetHeaders ( HttpMethod method , string url , Stream data , string contentType , Dictionary < string , string > args ,
NameValueCollection headers = null , CookieCollection cookies = null )
2015-06-08 01:34:33 +12:00
{
2016-12-23 20:24:38 +13:00
using ( HttpWebResponse response = GetResponse ( method , url , data , contentType , null , headers , cookies ) )
2015-06-08 01:34:33 +12:00
{
if ( response ! = null )
{
return response . Headers ;
}
2016-12-23 21:51:13 +13:00
return null ;
}
}
protected bool SendRequestDownload ( HttpMethod method , string url , Stream downloadStream , Dictionary < string , string > args = null ,
NameValueCollection headers = null , CookieCollection cookies = null , string contentType = null )
{
using ( HttpWebResponse response = GetResponse ( method , url , null , contentType , args , headers , cookies ) )
{
if ( response ! = null )
{
2016-12-27 06:23:29 +13:00
using ( Stream responseStream = response . GetResponseStream ( ) )
{
responseStream . CopyStreamTo ( downloadStream , BufferSize ) ;
}
2016-12-23 21:51:13 +13:00
return true ;
}
2015-06-08 01:34:33 +12:00
}
2016-12-23 21:51:13 +13:00
return false ;
2015-06-08 01:34:33 +12:00
}
2016-12-27 05:30:17 +13:00
protected string SendRequestMultiPart ( string url , Dictionary < string , string > args , NameValueCollection headers = null , CookieCollection cookies = null ,
ResponseType responseType = ResponseType . Text )
2015-06-08 01:34:33 +12:00
{
string boundary = CreateBoundary ( ) ;
2016-06-29 13:09:31 +12:00
string contentType = ContentTypeMultipartFormData + "; boundary=" + boundary ;
2016-12-23 20:24:38 +13:00
byte [ ] data = MakeInputContent ( boundary , args ) ;
2015-06-08 01:34:33 +12:00
using ( MemoryStream stream = new MemoryStream ( ) )
{
stream . Write ( data , 0 , data . Length ) ;
2016-12-23 21:21:37 +13:00
2017-10-17 04:01:58 +13:00
using ( HttpWebResponse webResponse = GetResponse ( HttpMethod . POST , url , stream , contentType , null , headers , cookies ) )
2015-06-08 01:34:33 +12:00
{
2017-10-17 04:01:58 +13:00
string response = ResponseToString ( webResponse , responseType ) ;
if ( VerboseLogs & & ! string . IsNullOrEmpty ( VerboseLogsPath ) )
{
WriteVerboseLog ( url , args , headers , response ) ;
}
return response ;
2016-06-29 12:50:16 +12:00
}
2015-06-08 01:34:33 +12:00
}
}
2016-12-27 05:47:46 +13:00
protected UploadResult SendRequestFile ( string url , Stream data , string fileName , string fileFormName = "file" , Dictionary < string , string > args = null ,
2015-06-08 01:34:33 +12:00
NameValueCollection headers = null , CookieCollection cookies = null , ResponseType responseType = ResponseType . Text , HttpMethod method = HttpMethod . POST ,
2016-06-29 13:09:31 +12:00
string contentType = ContentTypeMultipartFormData , string metadata = null )
2015-06-08 01:34:33 +12:00
{
UploadResult result = new UploadResult ( ) ;
IsUploading = true ;
StopUploadRequested = false ;
try
{
string boundary = CreateBoundary ( ) ;
2016-06-29 12:50:16 +12:00
contentType + = "; boundary=" + boundary ;
2015-06-08 01:34:33 +12:00
2016-12-23 20:24:38 +13:00
byte [ ] bytesArguments = MakeInputContent ( boundary , args , false ) ;
2015-06-08 01:34:33 +12:00
byte [ ] bytesDataOpen ;
byte [ ] bytesDataDatafile = { } ;
if ( metadata ! = null )
{
bytesDataOpen = MakeFileInputContentOpen ( boundary , fileFormName , fileName , metadata ) ;
bytesDataDatafile = MakeFileInputContentOpen ( boundary , fileFormName , fileName , null ) ;
}
else
{
bytesDataOpen = MakeFileInputContentOpen ( boundary , fileFormName , fileName ) ;
}
byte [ ] bytesDataClose = MakeFileInputContentClose ( boundary ) ;
2016-12-23 20:24:38 +13:00
long contentLength = bytesArguments . Length + bytesDataOpen . Length + bytesDataDatafile . Length + data . Length + bytesDataClose . Length ;
2017-10-17 04:01:58 +13:00
2016-06-29 12:50:16 +12:00
HttpWebRequest request = PrepareWebRequest ( method , url , headers , cookies , contentType , contentLength ) ;
2015-06-08 01:34:33 +12:00
using ( Stream requestStream = request . GetRequestStream ( ) )
{
requestStream . Write ( bytesArguments , 0 , bytesArguments . Length ) ;
requestStream . Write ( bytesDataOpen , 0 , bytesDataOpen . Length ) ;
requestStream . Write ( bytesDataDatafile , 0 , bytesDataDatafile . Length ) ;
2016-12-23 20:24:38 +13:00
if ( ! TransferData ( data , requestStream ) ) return null ;
2015-06-08 01:34:33 +12:00
requestStream . Write ( bytesDataClose , 0 , bytesDataClose . Length ) ;
}
2016-12-27 06:23:29 +13:00
using ( WebResponse response = request . GetResponse ( ) )
{
result . Response = ResponseToString ( response , responseType ) ;
}
2015-06-08 01:34:33 +12:00
result . IsSuccess = true ;
}
catch ( Exception e )
{
if ( ! StopUploadRequested )
{
2017-01-23 19:32:30 +13:00
string response = AddWebError ( e , url ) ;
2015-12-15 00:16:07 +13:00
2017-02-03 19:04:39 +13:00
if ( ReturnResponseOnError & & e is WebException )
2015-12-15 00:16:07 +13:00
{
result . Response = response ;
}
2015-06-08 01:34:33 +12:00
}
}
finally
{
currentRequest = null ;
IsUploading = false ;
2017-10-17 04:01:58 +13:00
if ( VerboseLogs & & ! string . IsNullOrEmpty ( VerboseLogsPath ) )
{
WriteVerboseLog ( url , args , headers , result . Response ) ;
}
2015-06-08 01:34:33 +12:00
}
return result ;
}
2016-12-27 05:30:17 +13:00
private HttpWebResponse GetResponse ( HttpMethod method , string url , Stream data = null , string contentType = null , Dictionary < string , string > args = null ,
NameValueCollection headers = null , CookieCollection cookies = null )
{
IsUploading = true ;
StopUploadRequested = false ;
try
{
2017-09-24 20:14:27 +13:00
url = URLHelpers . CreateQuery ( url , args ) ;
2016-12-27 05:30:17 +13:00
long length = 0 ;
if ( data ! = null )
{
length = data . Length ;
}
HttpWebRequest request = PrepareWebRequest ( method , url , headers , cookies , contentType , length ) ;
if ( length > 0 )
{
using ( Stream requestStream = request . GetRequestStream ( ) )
{
if ( ! TransferData ( data , requestStream ) )
{
return null ;
}
}
}
return ( HttpWebResponse ) request . GetResponse ( ) ;
}
catch ( Exception e )
{
if ( ! StopUploadRequested )
{
2017-01-23 19:32:30 +13:00
AddWebError ( e , url ) ;
2016-12-27 05:30:17 +13:00
}
}
finally
{
currentRequest = null ;
IsUploading = false ;
}
return null ;
}
2015-06-08 01:34:33 +12:00
#region Helper methods
2016-06-29 12:50:16 +12:00
private HttpWebRequest PrepareWebRequest ( HttpMethod method , string url , NameValueCollection headers = null , CookieCollection cookies = null , string contentType = null , long contentLength = 0 )
2015-06-08 01:34:33 +12:00
{
HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( url ) ;
request . Method = method . ToString ( ) ;
2015-10-13 22:26:16 +13:00
2016-06-29 12:50:16 +12:00
if ( headers ! = null )
2015-10-13 22:26:16 +13:00
{
2016-06-29 12:50:16 +12:00
if ( headers [ "Accept" ] ! = null )
{
request . Accept = headers [ "Accept" ] ;
headers . Remove ( "Accept" ) ;
}
2017-01-27 22:27:33 +13:00
if ( headers [ "Content-Length" ] ! = null )
{
request . ContentLength = Convert . ToInt32 ( headers [ "Content-Length" ] ) ;
headers . Remove ( "Content-Length" ) ;
}
2016-06-29 12:50:16 +12:00
request . Headers . Add ( headers ) ;
2015-10-13 22:26:16 +13:00
}
2015-06-08 01:34:33 +12:00
request . CookieContainer = new CookieContainer ( ) ;
if ( cookies ! = null ) request . CookieContainer . Add ( cookies ) ;
IWebProxy proxy = HelpersOptions . CurrentProxy . GetWebProxy ( ) ;
if ( proxy ! = null ) request . Proxy = proxy ;
2017-03-12 09:54:42 +13:00
request . UserAgent = ShareXResources . UserAgent ;
2016-06-29 12:50:16 +12:00
request . ContentType = contentType ;
if ( contentLength > 0 )
{
request . AllowWriteStreamBuffering = HelpersOptions . CurrentProxy . IsValidProxy ( ) ;
2017-10-14 19:34:35 +13:00
if ( method = = HttpMethod . GET )
{
request . CachePolicy = new HttpRequestCachePolicy ( HttpRequestCacheLevel . NoCacheNoStore ) ;
}
2016-06-29 12:50:16 +12:00
request . ContentLength = contentLength ;
request . Pipelined = false ;
request . Timeout = - 1 ;
}
else
{
request . KeepAlive = false ;
}
2015-06-08 01:34:33 +12:00
currentRequest = request ;
return request ;
}
protected bool TransferData ( Stream dataStream , Stream requestStream )
{
if ( dataStream . CanSeek )
{
dataStream . Position = 0 ;
}
ProgressManager progress = new ProgressManager ( dataStream . Length ) ;
int length = ( int ) Math . Min ( BufferSize , dataStream . Length ) ;
byte [ ] buffer = new byte [ length ] ;
int bytesRead ;
while ( ! StopUploadRequested & & ( bytesRead = dataStream . Read ( buffer , 0 , length ) ) > 0 )
{
requestStream . Write ( buffer , 0 , bytesRead ) ;
if ( AllowReportProgress & & progress . UpdateProgress ( bytesRead ) )
{
OnProgressChanged ( progress ) ;
}
}
return ! StopUploadRequested ;
}
private string CreateBoundary ( )
{
2015-10-01 00:16:47 +13:00
return new string ( '-' , 20 ) + DateTime . Now . Ticks . ToString ( "x" ) ;
2015-06-08 01:34:33 +12:00
}
private byte [ ] MakeInputContent ( string boundary , string name , string value )
{
string format = string . Format ( "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n" , boundary , name , value ) ;
return Encoding . UTF8 . GetBytes ( format ) ;
}
private byte [ ] MakeInputContent ( string boundary , Dictionary < string , string > contents , bool isFinal = true )
{
using ( MemoryStream stream = new MemoryStream ( ) )
{
if ( string . IsNullOrEmpty ( boundary ) ) boundary = CreateBoundary ( ) ;
byte [ ] bytes ;
if ( contents ! = null )
{
foreach ( KeyValuePair < string , string > content in contents )
{
if ( ! string . IsNullOrEmpty ( content . Key ) & & ! string . IsNullOrEmpty ( content . Value ) )
{
bytes = MakeInputContent ( boundary , content . Key , content . Value ) ;
stream . Write ( bytes , 0 , bytes . Length ) ;
}
}
if ( isFinal )
{
bytes = MakeFinalBoundary ( boundary ) ;
stream . Write ( bytes , 0 , bytes . Length ) ;
}
}
return stream . ToArray ( ) ;
}
}
private byte [ ] MakeFileInputContentOpen ( string boundary , string fileFormName , string fileName )
{
string format = string . Format ( "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n" ,
boundary , fileFormName , fileName , Helpers . GetMimeType ( fileName ) ) ;
return Encoding . UTF8 . GetBytes ( format ) ;
}
private byte [ ] MakeFileInputContentOpen ( string boundary , string fileFormName , string fileName , string metadata )
{
string format = "" ;
if ( metadata ! = null )
{
2016-06-29 13:09:31 +12:00
format = string . Format ( "--{0}\r\nContent-Type: {1}; charset=UTF-8\r\n\r\n{2}\r\n\r\n" , boundary , ContentTypeJSON , metadata ) ;
2015-06-08 01:34:33 +12:00
}
else
{
format = string . Format ( "--{0}\r\nContent-Type: {1}\r\n\r\n" , boundary , Helpers . GetMimeType ( fileName ) ) ;
}
return Encoding . UTF8 . GetBytes ( format ) ;
}
private byte [ ] MakeFileInputContentClose ( string boundary )
{
return Encoding . UTF8 . GetBytes ( string . Format ( "\r\n--{0}--\r\n" , boundary ) ) ;
}
private byte [ ] MakeFinalBoundary ( string boundary )
{
return Encoding . UTF8 . GetBytes ( string . Format ( "--{0}--\r\n" , boundary ) ) ;
}
private string ResponseToString ( WebResponse response , ResponseType responseType = ResponseType . Text )
{
if ( response ! = null )
{
using ( response )
{
switch ( responseType )
{
case ResponseType . Text :
2016-12-27 06:23:29 +13:00
using ( Stream responseStream = response . GetResponseStream ( ) )
using ( StreamReader reader = new StreamReader ( responseStream , Encoding . UTF8 ) )
2015-06-08 01:34:33 +12:00
{
return reader . ReadToEnd ( ) ;
}
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 ( ) ;
2015-10-20 13:26:16 +13:00
case ResponseType . LocationHeader :
2015-10-13 22:26:16 +13:00
return response . Headers [ "Location" ] ;
2015-06-08 01:34:33 +12:00
}
}
}
return null ;
}
protected NameValueCollection CreateAuthenticationHeader ( string username , string password )
{
string authInfo = username + ":" + password ;
authInfo = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( authInfo ) ) ;
NameValueCollection headers = new NameValueCollection ( ) ;
headers [ "Authorization" ] = "Basic " + authInfo ;
return headers ;
}
2017-01-23 19:32:30 +13:00
private string AddWebError ( Exception e , string url )
2015-06-08 01:34:33 +12:00
{
string response = null ;
if ( Errors ! = null & & e ! = null )
{
2017-01-23 19:32:30 +13:00
StringBuilder sb = new StringBuilder ( ) ;
sb . AppendLine ( "Message:" ) ;
sb . AppendLine ( e . Message ) ;
if ( ! string . IsNullOrEmpty ( url ) )
{
sb . AppendLine ( ) ;
sb . AppendLine ( "Request URL:" ) ;
2017-08-07 04:18:30 +12:00
sb . AppendLine ( URLHelpers . RemoveQuery ( url ) ) ;
2017-01-23 19:32:30 +13:00
}
2015-06-08 01:34:33 +12:00
if ( e is WebException )
{
try
{
response = ResponseToString ( ( ( WebException ) e ) . Response ) ;
if ( ! string . IsNullOrEmpty ( response ) )
{
2017-01-23 19:32:30 +13:00
sb . AppendLine ( ) ;
sb . AppendLine ( "Response:" ) ;
sb . AppendLine ( response ) ;
2015-06-08 01:34:33 +12:00
}
}
catch
{
}
}
2017-01-23 19:32:30 +13:00
sb . AppendLine ( ) ;
sb . AppendLine ( "Stack trace:" ) ;
sb . AppendLine ( e . StackTrace ) ;
2015-06-08 01:34:33 +12:00
2017-01-23 19:32:30 +13:00
string errorText = sb . ToString ( ) . Trim ( ) ;
2015-06-08 01:34:33 +12:00
Errors . Add ( errorText ) ;
2017-01-01 11:41:06 +13:00
DebugHelper . WriteLine ( "Error:\r\n" + errorText ) ;
2015-06-08 01:34:33 +12:00
}
return response ;
}
2017-10-17 04:01:58 +13:00
private void WriteVerboseLog ( string url , Dictionary < string , string > args , NameValueCollection headers , string response )
{
if ( verboseLogger = = null )
{
verboseLogger = new Logger ( VerboseLogsPath )
{
MessageFormat = "Date: {0:yyyy-MM-dd HH:mm:ss.fff}\r\n{1}" ,
2017-10-17 06:22:28 +13:00
StringWrite = false
2017-10-17 04:01:58 +13:00
} ;
}
StringBuilder sb = new StringBuilder ( ) ;
sb . AppendLine ( "URL: " + ( url ? ? "" ) ) ;
if ( args ! = null & & args . Count > 0 )
{
sb . AppendLine ( "Arguments:" ) ;
foreach ( KeyValuePair < string , string > arg in args )
{
sb . AppendLine ( $" Name: {arg.Key}, Value: {arg.Value}" ) ;
}
}
if ( headers ! = null & & headers . Count > 0 )
{
sb . AppendLine ( "Headers:" ) ;
foreach ( string key in headers )
{
string value = headers [ key ] ;
sb . AppendLine ( $" Name: {key}, Value: {value}" ) ;
}
}
2017-10-17 06:22:28 +13:00
sb . AppendLine ( "Response:" ) ;
if ( ! string . IsNullOrEmpty ( response ) )
{
sb . AppendLine ( response ) ;
}
2017-10-17 04:01:58 +13:00
sb . Append ( new string ( '-' , 30 ) ) ;
verboseLogger . WriteLine ( sb . ToString ( ) ) ;
}
2015-06-08 01:34:33 +12:00
#endregion Helper methods
#region OAuth methods
protected string GetAuthorizationURL ( string requestTokenURL , string authorizeURL , OAuthInfo authInfo ,
Dictionary < string , string > customParameters = null , HttpMethod httpMethod = HttpMethod . GET )
{
string url = OAuthManager . GenerateQuery ( requestTokenURL , customParameters , httpMethod , authInfo ) ;
string response = SendRequest ( httpMethod , url ) ;
if ( ! string . IsNullOrEmpty ( response ) )
{
return OAuthManager . GetAuthorizationURL ( response , authInfo , authorizeURL ) ;
}
return null ;
}
protected bool GetAccessToken ( string accessTokenURL , OAuthInfo authInfo , HttpMethod httpMethod = HttpMethod . GET )
{
return GetAccessTokenEx ( accessTokenURL , authInfo , httpMethod ) ! = null ;
}
protected NameValueCollection GetAccessTokenEx ( string accessTokenURL , OAuthInfo authInfo , HttpMethod httpMethod = HttpMethod . GET )
{
if ( string . IsNullOrEmpty ( authInfo . AuthToken ) | | string . IsNullOrEmpty ( authInfo . AuthSecret ) )
{
throw new Exception ( "Auth infos missing. Open Authorization URL first." ) ;
}
string url = OAuthManager . GenerateQuery ( accessTokenURL , null , httpMethod , authInfo ) ;
string response = SendRequest ( httpMethod , url ) ;
if ( ! string . IsNullOrEmpty ( response ) )
{
return OAuthManager . ParseAccessTokenResponse ( response , authInfo ) ;
}
return null ;
}
#endregion OAuth methods
}
2013-11-03 23:53:49 +13:00
}