AUTHOR: Stephania Tapela
DATE POSTED: July 07, 2017
Article information
Article relates to
Telerik Report Server
Created by
Stef, Telerik
Last modified
July 7, 2017
class
Program
{
static
HttpClient client =
new
HttpClient();
string
reportServerAddress =
"http://localhost:83/"
;
serverREStAPI = reportServerAddress +
"api/reportserver/"
void
Main(
[] args)
Console.WriteLine(
"Demo started"
);
RunAsync().Wait();
"Demo ended"
Console.ReadLine();
}
async Task RunAsync()
// Our .NET Client
client.BaseAddress =
Uri(serverREStAPI);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
MediaTypeWithQualityHeaderValue(
"application/json"
));
//log the user to allow further operations
var userToken = LogIn(
"admin"
,
"admin123"
//authorize further requests
client.DefaultRequestHeaders.Authorization =
AuthenticationHeaderValue(
"Bearer"
, userToken);
//list all reports
var reports = await GetReportsAsync(serverREStAPI +
"reports"
//get document id for first report
var firstReportId = reports[0][
"Id"
];
var documentID = await ExportReportAsync(serverREStAPI +
"documents"
"PDF"
, firstReportId,
""
//download PDF from documentID
DownloadPDF(serverREStAPI +
"documents/"
, documentID,
false
//log user
LogIn(
usernameInput,
passwordInput)
var data =
FormUrlEncodedContent(
[]{
KeyValuePair<
>(
"grant_type"
"password"
),
"username"
, usernameInput ),
, passwordInput )
});
// "grant_type=password&username=" + usernameInput + "&password=" + passwordInput;
HttpResponseMessage response = client.PostAsync(reportServerAddress +
"Token"
, data).Result;
response.EnsureSuccessStatusCode();
var serializer =
System.Web.Script.Serialization.JavaScriptSerializer();
dynamic result = serializer.DeserializeObject(response.Content.ReadAsStringAsync().Result);
return
result[
"access_token"
//list categories
async Task<List<Dictionary<
>>> GetReportsAsync(
path)
List<Dictionary<
>> reports =
null
HttpResponseMessage response = await client.GetAsync(path);
if
(response.IsSuccessStatusCode)
reports = await response.Content.ReadAsAsync<List<Dictionary<
>>>();
reports;
//export in PDF a report
async Task<
> ExportReportAsync(
path,
format,
reportId,
parameterValuesJSONSerieliazed)
{ ReportId = reportId, Format = format, ParameterValues = parameterValuesJSONSerieliazed };
HttpResponseMessage response = await client.PostAsJsonAsync(path, data);
"DocumentId"
//download PDF
DownloadPDF(
documentID,
bool
asAttachment)
var queryString =
(asAttachment)
queryString +=
"?content-disposition=attachment"
fileName =
"test1.pdf"
folderName = System.IO.Path.GetTempPath();
filePath = System.IO.Path.Combine(folderName, fileName);
using
(System.Net.WebClient myWebClient =
System.Net.WebClient())
myWebClient.DownloadFile(path + documentID + queryString, filePath);
System.Diagnostics.Process.Start(filePath);
Resources Buy Try