using System.Net;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
...
using (HttpClient httpClient = new HttpClient())
{
// Get Session Id
string logonUri = "http://localhost:8000/Api/LogOn/";
var logonOptions = new
{
accountName = "admin",
password = "1234",
cultureName = string.Empty,
deleteOtherSessions = true,
isWindowsLogOn = false
};
JavaScriptSerializer serializer = new JavaScriptSerializer();
var requestBodyAsString = serializer.Serialize(logonOptions);
StringContent content =
new StringContent(
requestBodyAsString,
Encoding.UTF8,
"application/json"
);
string jsonString = string.Empty;
using (var response = httpClient.PostAsync(logonUri, content).Result)
{
Console.WriteLine("LogOn - Success");
jsonString =
response.Content.ReadAsStringAsync().Result;
}
// ************************************************************************
// Upload the transfer file.
// ************************************************************************
var obj = (Dictionary<string, object>)serializer.DeserializeObject(jsonString);
string sessionId = obj["sessionId"].ToString();
string url = "http://localhost:8000/api/transfer/file/";
var multiPartRequestBody =
new System.Net.Http.MultipartFormDataContent();
multiPartRequestBody.Headers.ContentType.MediaType = "multipart/form-data";
string filePath = @"C:\Temp\SomeExportFile.dbie";
string fileName = new FileInfo(filePath).Name;
Guid[] transferFileIds = null;
using (Stream fileStream = System.IO.File.OpenRead(filePath))
{
using (StreamContent streamContent = new StreamContent(fileStream))
{
// Add an Authorization header
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", sessionId);
multiPartRequestBody.Add(streamContent, fileName, fileName);
using (var response = httpClient.PostAsync(url, multiPartRequestBody).Result)
{
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("Upload the transfer file - Success");
// An array of Guids.
string jsonObject = response.Content.ReadAsStringAsync().Result;
transferFileIds = (Guid[])serializer.Deserialize(jsonObject, typeof(Guid[]));
}
}
}
}
string transferFileId = transferFileIds.First().ToString();
// ************************************************************************
// Import the transfer
// ************************************************************************
url = "http://localhost:8000/API/Transfer/Import/";
// Define the request body
HttpContent requestBody = null;
requestBody = new StringContent(@"
{
""importConfiguration"":
{
""includeTokens"":false,
""__classType"":""dundas.transfer.ImportConfig"",
""transferFileUncPath"":"""",
""transferFileId"":""" + transferFileId + @""",
""source"":""InternalStorageId"",
""importEntityTransferVersionPolicy"":""CurrentVersionOnly"",
""groupMembershipTransferPolicy"":""DoNotImport"",
""includeUserProjects"":true,
""includeReferencedItems"":true,
""includeCustomAttributes"":false,
""accountIds"":[],
""groupIds"":[],
""tenantIds"":[],
""customAttributeIds"":[],
""fileSystemEntryIds"":[""bd767e1f-1fd2-4411-8b70-af9fdfd763e3""],
""appSettingTransferSpecs"":[],
""includeCubeData"":false,
""includeContextualData"":false,
""includeResourceData"":true,
""overwriteDataConnectorSettings"":false
},
""includeSubentriesInResultDetails"":false
}
}",
Encoding.UTF8,
"application/json"
);
using (var response = httpClient.PostAsync(url, requestBody).Result)
{
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("Import the transfer - Success");
//HTTP Status code as well as a <b>Dundas.BI.WebApi.Models.TransferResultData</b>
//object or a error message.
string jsonObject = response.Content.ReadAsStringAsync().Result;
}
}
}