Created
August 29, 2016 00:29
-
-
Save MorningZ/1a005ec02349a13b768a1798d005831b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.Net | |
Imports System.IO | |
Imports System.Net.Http | |
Imports System.Net.Http.Headers | |
Imports Newtonsoft.Json | |
''' <summary>Call the Dropbox API</summary> | |
''' <param name="i_AuthToken">Authentication Token string value</param> | |
''' <param name="i_Url">Url of Dropbox API endpoint</param> | |
''' <param name="i_Params">Dictionary of POST parameters (if needed)</param> | |
''' <remarks> | |
''' Requires .NET Framework 4.5 (when the HttpClient class was introduced) | |
''' https://www.dropbox.com/developers/documentation/http/documentation | |
''' </remarks> | |
Public Shared Function [Get_DropboxAPIv2](ByVal i_AuthToken As String, ByVal i_EndpointUrl As String, ByVal i_Params As Dictionary(Of String, Object)) As String | |
Dim ThisResultString As String = "Unknown response" | |
Dim ThisHttpClient As Http.HttpClient = New Http.HttpClient | |
Dim ThisHttpRequest As HttpRequestMessage = New HttpRequestMessage | |
Dim ThisHttpResponse As HttpResponseMessage | |
Try | |
If Params.Count = 0 Then ' POST w/o any parameters | |
' There is probably an eaier way, but this works! | |
ThisHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json") | |
ThisHttpRequest.RequestUri = New Uri(i_EndpointUrl) | |
ThisHttpRequest.Method = HttpMethod.Post | |
ThisHttpRequest.Headers.Add("Authorization", String.Concat("Bearer ", i_AuthToken)) | |
ThisHttpResponse = ThisHttpClient.SendAsync(ThisHttpRequest).Result | |
Else ' POST with parameters | |
ThisHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", String.Concat("Bearer ", i_AuthToken)) | |
Dim ThisPostContent As HttpContent = New StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(i_Params)) | |
ThisPostContent.Headers.ContentType = New MediaTypeHeaderValue("application/json") | |
ThisHttpResponse = ThisHttpClient.PostAsync(i_EndpointUrl, ThisPostContent).Result | |
End If | |
ThisResultString = ThisHttpResponse.Content.ReadAsStringAsync().Result | |
Catch ex As Exception | |
ThisResultString = String.Concat("Error: ", ex.Message) | |
End Try | |
Return ThisResultString | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment