DLS Full HTTP POST Example

using System;
using System.Text;
using System.IO;
using System.Net;
using System.IO.Compression;
using System.Collections.Specialized;

namespace TechnetSamples
{
class Program
{
static void Main(string[] args)
{
//You must request a ticket using HTTPS protocol
string URLAuth = "https://technet.rapaport.com/HTTP/Authenticate.aspx";
WebClient webClient = new WebClient();

NameValueCollection formData = new NameValueCollection();
formData["Username"] = "myUser";
formData["Password"] = "myPassword";
byte[] responseBytes = webClient.UploadValues(URLAuth, "POST", formData);
string ResultAuth = Encoding.UTF8.GetString(responseBytes);

//After receiving an encrypted ticket, you can use it to authenticate your session.
//Now you can choose to change the protocol to HTTP so it works faster.

//Download File
string URL = "http://technet.rapaport.com/HTTP/DLS/GetFile.aspx";
WebRequest webRequest = WebRequest.Create(URL);

webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
Stream reqStream = webRequest.GetRequestStream();
string postData = "ticket=" + ResultAuth;
byte[] postArray = Encoding.ASCII.GetBytes(postData);
reqStream.Write(postArray, 0, postArray.Length);
reqStream.Close();

HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();;

Stream stream = webResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string content = reader.ReadToEnd();

byte [] fileBytes = Encoding.ASCII.GetBytes(content);

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/csv";
response.AddHeader("Content-Disposition", "attachment; filename=fileName.csv;");

Response.BinaryWrite(fileBytes);
response.Flush();
response.End();

webResponse.Close();
}
}
}