views:

27

answers:

1

Is it possible to execute the Javascript in a PAC file from a .Net windows application to return the proxy server?

+2  A: 

Since a proxy auto-config file is just a JavaScript source file, and you need to call a method in it, you could use the JScript .NET compiler (available in code via the JScriptCodeProvider) and/or interpreter (via JScriptEvaluate).

Compiler: Here's sample code to compile JScript .NET code to a class, and then call a method on that class, which might be a good fit for what you're looking for. Note that it uses Reflection, so it might give you troubles if you're in a partial-trust environment like ASP.NET. I'm not sure whether the Reflection is required for this kind of access, or if it's just an implementation detail; if that's an issue for you, you could research it further, or you could use the interpreter instead.

Interpreter: Here's an example that interprets JScript code using Microsoft.JScript.Eval.JScriptEvaluate. Since your PAC file is defining one or more functions, it won't return anything useful. You would need to append a line at the end that calls FindProxyForURL with the arguments you want; then you could evaluate that entire string and get your result. Or it's possible that you could eval the PAC file, which would declare the function, and then you could eval a call to that function (I haven't used JScriptEvaluate so I don't know whether globals carry over from one call to the next).

Joe White