Hi,
I am trying to download the belo URLs
[
^]
[
^]
I am able to download the above URLs with out mentioning the Proxy.
If I set the proxy I am getting the error message "Error Too many automatic redirections were attempted" .
Please let me know if you need more inforamtion.
Thanks,
Siddi.
What I have tried:
Below are the different options I have tried:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
namespace AutoRedirect
{
class Program
{
static string pilotSecuritiesFilePath = @"C:\FINRA\pilotSecuritiesProxy.txt";//GetPilotSecuritiesFilePath();
static string pilotChangesFilePath = @"C:\FINRA\pilotChangesProxy.txt";//GetPilotChangesFilePath();
static string masterURL = "http://tsp.finra.org/finra_org/ticksizepilot/TSPrePilotSecurities.txt";
static string changeURL = "http://tsp.finra.org/finra_org/ticksizepilot/TSPrePilotChanges.txt";
static string sSource;
static string sLog;
static string sEvent;
static string filePath = @"C:\FINRA\Log.txt";
static void Main(string[] args)
{
sSource = "AutoRedirectApp";
sLog = "Application";
sEvent = "AutoRedirect";
if (File.Exists(filePath))
{
File.Delete(filePath);
}
//if (!EventLog.SourceExists(sSource))
// EventLog.CreateEventSource(sSource, sLog);
DownloadPilotSecuritiesWithoutProxy();
DownloadPilotSecurities();
DownloadUsingWebRequestWithoutProxy();
DownloadUsingWebRequestWithProxy();
DownloadUsingWebRequestWithProxyMaxRedirections();
DownloadUsingWebRequestWithProxyWithBaseURL();
}
private static void DownloadPilotSecuritiesWithoutProxy()
{
//LogMessage("Download using WebClient Without Proxy: Start");
try
{
using (WebClient webClient = new WebClient())
{
//webClient.BaseAddress = "http://www.finra.org/";
webClient.DownloadFile(masterURL, pilotSecuritiesFilePath);
webClient.DownloadFile(changeURL, pilotChangesFilePath);
}
}
catch (Exception ex)
{
EventLog.WriteEntry(sSource, ex.Message);
//LogMessage(ex.Message);
LogMessage("Download using WebClient Without Proxy: Error " + ex.Message);
}
LogMessage("Download using WebClient Without Proxy: Completed");
}
private static void DownloadPilotSecurities()
{
// LogMessage("Download using WebClient with Proxy: Started");
try
{
using (WebClient webClient = new WebClient())
{
WebProxy wp = new WebProxy("http://SomeProxy.pac");
webClient.Proxy = wp;
//webClient.BaseAddress = "http://www.finra.org/";
webClient.DownloadFile(masterURL, pilotSecuritiesFilePath);
webClient.DownloadFile(changeURL, pilotChangesFilePath);
}
LogMessage("Download using WebClient with Proxy: Completed");
}
catch (Exception ex)
{
//EventLog.WriteEntry(sSource, ex.Message);
Console.WriteLine(ex.Message);
//LogMessage(ex.Message);
LogMessage("Download using WebClient with Proxy: Error " + ex.Message);
}
}
private static void DownloadUsingWebRequestWithoutProxy()
{
// LogMessage("Download Using WebRequest without Proxy: Started");
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(masterURL);
string res;
try
{
webReq.Method = "GET";
using (WebResponse response = webReq.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
res = reader.ReadToEnd();
}
}
File.AppendAllText(pilotSecuritiesFilePath, res);
LogMessage("Download Using WebRequest without Proxy: Completed");
}
catch (Exception ex)
{
LogMessage("Download Using WebRequest without Proxy: Error " + ex.Message);
}
}
private static void DownloadUsingWebRequestWithProxy()
{
// LogMessage("Download Using WebRequest without Proxy: Started");
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(masterURL);
CookieContainer cookieContainer = new CookieContainer();
string res;
try
{
webReq.Method = "GET";
WebProxy wp = new WebProxy("http://SomeProxy.pac");
webReq.Proxy = wp;
webReq.CookieContainer = cookieContainer;
using (WebResponse response = webReq.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
res = reader.ReadToEnd();
}
}
File.AppendAllText(pilotSecuritiesFilePath, res);
LogMessage("Download Using WebRequest with Proxy: Completed");
}
catch (Exception ex)
{
LogMessage("Download Using WebRequest with Proxy: Error " + ex.Message);
}
}
private static void DownloadUsingWebRequestWithProxyMaxRedirections()
{
// LogMessage("Download Using WebRequest without Proxy: Started");
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(masterURL);
CookieContainer cookieContainer = new CookieContainer();
string res;
try
{
webReq.Method = "GET";
WebProxy wp = new WebProxy("http://SomeProxy.pac");
webReq.Proxy = wp;
webReq.CookieContainer = cookieContainer;
webReq.MaximumAutomaticRedirections = 10000;
using (WebResponse response = webReq.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
res = reader.ReadToEnd();
}
}
File.AppendAllText(pilotSecuritiesFilePath, res);
LogMessage("Download Using WebRequest with Proxy with Max Redirections: Completed");
}
catch (Exception ex)
{
LogMessage("Download Using WebRequest with Proxy with Max Redirections: Error " + ex.Message);
}
}
private static void LogMessage(string message)
{
File.AppendAllText(filePath, Environment.NewLine+message);
}
private static void DownloadUsingWebRequestWithProxyWithBaseURL()
{
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create("http://www.finra.org/");
CookieContainer cookieContainer = new CookieContainer();
string res;
try
{
webReq.Method = "GET";
//webReq.Proxy = wp;
webReq.CookieContainer = cookieContainer;
//webReq.MaximumAutomaticRedirections = 10000;
using (WebResponse response = webReq.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
res = reader.ReadToEnd();
}
}
webReq = (HttpWebRequest)WebRequest.Create("http://tsp.finra.org/finra_org/ticksizepilot/TSPrePilotSecurities.txt");
webReq.CookieContainer = cookieContainer;
webReq.Method = "GET";
WebProxy wp = new WebProxy("http://SomeProxy.pac");
webReq.Proxy = wp;
using(WebResponse response2 = webReq.GetResponse())
{
using (Stream stream = response2.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
res = reader.ReadToEnd();
}
}
File.AppendAllText(pilotSecuritiesFilePath, res);
LogMessage("Download Using WebRequest with Proxy with Base URL: Completed");
}
catch (Exception ex)
{
LogMessage("Download Using WebRequest with Proxy with Base URL : Error " + ex.Message);
}
}
}
}