Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
}
}


}
}
Posted
Updated 25-May-16 1:08am
v2
Comments
Richard MacCutchan 19-May-16 3:26am    
Don't use the proxy.
Member 10890643 19-May-16 3:28am    
The requirement is to use the proxy and download the file.
Richard MacCutchan 19-May-16 4:12am    
Then you need to fix the proxy server settings to stop the redirection error. This is not a programming issue.
Member 10890643 19-May-16 5:21am    
Thank you Richard, if possible Can you please give me some more information like why this issue is occurring..and what settings of Proxy server need to be configured..so that it will be useful for me to inform more details to our team.. Thanks,Siddi
Richard MacCutchan 19-May-16 5:35am    
Sorry, I have no experience of setting up proxies. Google will help you.

1 solution

C#
Hi More information can be found at the below URLs:

Using PAC files proxy
Unable to download a file when proxy is mentioned in WebClient or HttpWebRequest[^]

Thanks,
Siddi.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900