Background
I have to use the proxy server specified by users in my application. Right now, I check that the input proxy contains ".pac" or not. If yes, I'll get the real proxy from pac file. (http://www.codeproject.com/Articles/12168/Using-PAC-files-proxy) Otherwise, I just set the input proxy to WebProxy.
public HttpWebRequest CreateRequest(string url, string proxy)
{
var request = WebRequest.Create(url);
if (proxy.ToLower().Contains(".pac"))
{
string realProxy = GetProxyFromPac(url, proxy);
request.Proxy = (string.IsNullOrEmpty(realProxy))
? new WebProxy()
: new WebProxy(realProxy, true);
}
else
{
request.Proxy = new WebProxy(proxy, true);
}
return request as HttpWebRequest;
}
And use the request like this.
var request = CreateRequest(url, proxy);
var response = request.GetResponse() as HttpWebResponse;
Problem
When the server redirects url to another url, I'll get request timeout.
Example:
- The input url URL
- The input proxy PAC_P
- GetProxyFromPac(PAC_P, URL) return P
- The redirected url REAL_URL
- GetProxyFromPac(PAC_P, REAL_URL) return PP
I found that it's because I set the proxy to P in the request and it will be used for all urls (including redirected urls) but REAL_URL is not accessible via P. I have to get PP from the REAL_URL and PAC_P and use PP to request to REAL_URL.
The first solution in my head is to get a new proxy every time the request is redirected and manually request to the redirected url.
var request = CreateRequest(url, proxy);
request.AllowAutoRedirect = false;
var response = request.GetResponse() as HttpWebResponse;
while (response.StatusCode == HttpStatusCode.Redirect ||
response.StatusCode == HttpStatusCode.Moved)
{
request = CreateRequest(response.Headers["Location"], proxy);
response = request.GetResponse() as HttpWebResponse;
}
Question
I think there should be an elegant way to handle this. It seems to be an extremely general case. Do you have any idea?