Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Net;
using System.IO;
using System.Text;
namespace AconexSampleSheet
{
class Program
{
static void Main(string[] args)
{
// Sample request values.
string documentId = args[0];
string filename = args[1] + args[2];
string uri = "https://uk1.aconex.co.uk/api/projects/268441385/register/" + documentId;
string userName = "abcd";
string password = "abcd";
string method = "GET";
string response = "";
bool error = false;
bool aconexError = false;
string errorMessage = "";
// XML response from Aconex stored in parameter “response”
// If aconexError flag is raised, response will contain Aconex error message
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
string cred = Convert.ToBase64String(Encoding.UTF8.GetBytes(userName + ":" + password));
request.Headers.Set("X-Application-Key", "abcd");
request.Headers.Set(HttpRequestHeader.Authorization, "Basic "+ cred);
request.Method = method;
request.AutomaticDecompression = DecompressionMethods.GZip|DecompressionMethods.Deflate;
try
{
FileStream writeStream = new FileStream(filename,FileMode.Create, FileAccess.Write);
readWriteStream(request.GetResponse().GetResponseStream(), writeStream);
writeStream.Close();
}
catch (WebException e)
{
using (WebResponse webResponse = e.Response)
{
try
{
using (Stream data = webResponse.GetResponseStream())
{
response = new
StreamReader(data).ReadToEnd();
aconexError = true;
}
}
catch (Exception ex)
{
error = true;
errorMessage ="Please check your internet connection.\n"+ e.Message + "\n" + ex.Message;
}
}
}
// Write result
if (aconexError)
Console.WriteLine(response);
if (error)
Console.WriteLine(errorMessage);
else
Console.WriteLine("File downloaded");
}
// Read and write response data in pieces
static void readWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
try
{
bytesRead = readStream.Read(buffer, 0, Length);
}
catch (IOException ex)
{
if (bytesRead < 256)
bytesRead = 0;
else
throw new IOException(ex.Message);
}
}
readStream.Close();
writeStream.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.