Thursday, 16 February 2006

WebRequest/WebResponse and saving a downloaded binary

It took waay longer than I expected to find this code on Google... Sure you can use WebClient with it's DownloadFile() method... but if you want to fiddle with timeout settings, etc. then you need to drop back to the WebRequest/WebResponse classes.

Thanks to this GoogleGroups response download binary file using c# webrequest/webresponse I was able to get it working.
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objRequest.Timeout = 600000;
objRequest.Credentials = CredentialCache.DefaultCredentials;
WebResponse objResponse = objRequest.GetResponse();
byte[] buffer = new byte[32768];
using (Stream input = objResponse.GetResponseStream())
{
using (FileStream output = new FileStream (fileloc, FileMode.CreateNew))
{
int bytesRead;
while ( (bytesRead=input.Read (buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
There are all sorts of red-herrings around this task - BinarySteam, ResponseEncoding, etc... but in the end it was relatively simple.

Tuesday, 14 February 2006

SQL Server Reporting Services RS.EXE (#1)

Automating the deployment of reports (*.rdl) and data sources (*.rds) using NAnt and RS Utility (rs.exe) has been relatively straight forward. There are a few tricks, which I'll post later; but for now, the following error had me stumped for a while, and didn't return anything useful on Google.

The required field DataSource is missing from the input structure

In my case, I tracked it down to sample code I'd copied from
MSDN: ReportingService.SetReportDataSources.

I'm using VB.NET (argh!), and the dataSources array declaration in this sample code is the cause of the error.
Dim dataSources(1) As DataSource
actually creates a two-element array BUT we only set one element
dataSources[0] = ds;
so when this line executes
rs.SetReportDataSources("/SampleReports/Product Catalog", dataSources)
it is actually sending an array where the zeroth element is set to a valid object, but the first element is NULL... hence the (somewhat cryptic) "required field DataSource is missing" error.

The fix?
Dim dataSources(0) As DataSource

Sunday, 5 February 2006

JSON - Wikipedia, the free encyclopedia

Russ pointed me to JSON - JavaScript Object Notation... a Javascript 'feature' which I'd never heard of (but maybe I've seen examples of it, and not understood what they "did").

Seems a lot like the Anonymous types and Object Initializers that are mooted for C# 3.0. Sorta.