selva
2006-11-08 06:19:17 UTC
i use the following code in c# for detecting the PAC file
Automatically....
if i give the url of the PAC file explicitly then it returning me the
appropriate proxy....
if i use WINHTTP_AUTOPROXY_AUTO_DETECT flag set it's returning me the
error 12180...
please help me regarding this issue....
Class Proxytest.cs
using System;
using System.Net;
using System.IO;
using WinHttp;
namespace PacProxyUsage
{
/// <summary>
/// Summary description for ProxyTest.
/// </summary>
class ProxyTest
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Stream RequestStream = null;
string DestinationUrl = "http://www.google.co.in";
string PacUri = "http://wpad/wpad.dat";
// Create test request
WebRequest TestRequest = WebRequest.Create ( DestinationUrl );
WinHttpRequest req=new WinHttpRequest();
// Optain Proxy address for the URL
string ProxyAddresForUrl = Proxy.GetProxyForUrlUsingPac (
DestinationUrl, PacUri );
if ( ProxyAddresForUrl != null ){
Console.WriteLine ("Found Proxy: {0}",
ProxyAddresForUrl );
TestRequest.Proxy = new WebProxy ( ProxyAddresForUrl )
;
}else{
Console.WriteLine ( "Proxy Not Found. Send request
directly." );
}
try {
//req.Open("POST","http://www.google.com",false);
//req.SetCredentials( "e363757" ,"Gocomegocome1983" , 1 );
WebResponse TestResponse = TestRequest.GetResponse();
//req.Send(0);
//Console.WriteLine(req.Status);
Console.WriteLine ( "Request was successful" );
}catch ( Exception ex ){
Console.WriteLine ( "Error: {0}", ex.Message );
}finally{
if ( RequestStream != null ){
RequestStream.Close();
}
}
Console.ReadLine();
}
}
}
Class Proxy.cs
using System;
using WinHttp;
namespace PacProxyUsage
{
/// <summary>
/// Summary description for Proxy.
/// </summary>
public class Proxy
{
public Proxy()
{
}
/// <summary>
/// Return proxy for requested Url
/// </summary>
/// <param name="sUrl">url</param>
/// <param name="sPacUri">Uri to PAC file</param>
/// <returns>proxy info</returns>
public static string GetProxyForUrlUsingPac ( string
DestinationUrl,String PacUri ){
IntPtr WinHttpSession = Win32Api.WinHttpOpen("User",
Win32Api.WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
IntPtr.Zero, IntPtr.Zero, 0);
Win32Api.WINHTTP_AUTOPROXY_OPTIONS ProxyOptions = new
Win32Api.WINHTTP_AUTOPROXY_OPTIONS();
Win32Api.WINHTTP_PROXY_INFO ProxyInfo = new
Win32Api.WINHTTP_PROXY_INFO();
ProxyOptions.dwFlags =
Win32Api.WINHTTP_AUTOPROXY_CONFIG_URL;
//ProxyOptions.dwFlags =
Win32Api.WINHTTP_AUTOPROXY_AUTO_DETECT ;
//ProxyOptions.dwAutoDetectFlags =
(Win32Api.WINHTTP_AUTO_DETECT_TYPE_DHCP |
Win32Api.WINHTTP_AUTO_DETECT_TYPE_DNS_A);
ProxyOptions.lpszAutoConfigUrl = PacUri;
// string result;
// Get Proxy
bool IsSuccess = Win32Api.WinHttpGetProxyForUrl(
WinHttpSession, DestinationUrl, ref ProxyOptions, ref
ProxyInfo );
Console.WriteLine("Error: {0}", Win32Api.GetLastError() );
Win32Api.WinHttpCloseHandle(WinHttpSession);
if ( IsSuccess ){
return ProxyInfo.lpszProxy;
}else {
Console.WriteLine("Error: {0}", Win32Api.GetLastError()
);
return null;
}
}
}
}
Regards
BHARATHI
Automatically....
if i give the url of the PAC file explicitly then it returning me the
appropriate proxy....
if i use WINHTTP_AUTOPROXY_AUTO_DETECT flag set it's returning me the
error 12180...
please help me regarding this issue....
Class Proxytest.cs
using System;
using System.Net;
using System.IO;
using WinHttp;
namespace PacProxyUsage
{
/// <summary>
/// Summary description for ProxyTest.
/// </summary>
class ProxyTest
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Stream RequestStream = null;
string DestinationUrl = "http://www.google.co.in";
string PacUri = "http://wpad/wpad.dat";
// Create test request
WebRequest TestRequest = WebRequest.Create ( DestinationUrl );
WinHttpRequest req=new WinHttpRequest();
// Optain Proxy address for the URL
string ProxyAddresForUrl = Proxy.GetProxyForUrlUsingPac (
DestinationUrl, PacUri );
if ( ProxyAddresForUrl != null ){
Console.WriteLine ("Found Proxy: {0}",
ProxyAddresForUrl );
TestRequest.Proxy = new WebProxy ( ProxyAddresForUrl )
;
}else{
Console.WriteLine ( "Proxy Not Found. Send request
directly." );
}
try {
//req.Open("POST","http://www.google.com",false);
//req.SetCredentials( "e363757" ,"Gocomegocome1983" , 1 );
WebResponse TestResponse = TestRequest.GetResponse();
//req.Send(0);
//Console.WriteLine(req.Status);
Console.WriteLine ( "Request was successful" );
}catch ( Exception ex ){
Console.WriteLine ( "Error: {0}", ex.Message );
}finally{
if ( RequestStream != null ){
RequestStream.Close();
}
}
Console.ReadLine();
}
}
}
Class Proxy.cs
using System;
using WinHttp;
namespace PacProxyUsage
{
/// <summary>
/// Summary description for Proxy.
/// </summary>
public class Proxy
{
public Proxy()
{
}
/// <summary>
/// Return proxy for requested Url
/// </summary>
/// <param name="sUrl">url</param>
/// <param name="sPacUri">Uri to PAC file</param>
/// <returns>proxy info</returns>
public static string GetProxyForUrlUsingPac ( string
DestinationUrl,String PacUri ){
IntPtr WinHttpSession = Win32Api.WinHttpOpen("User",
Win32Api.WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
IntPtr.Zero, IntPtr.Zero, 0);
Win32Api.WINHTTP_AUTOPROXY_OPTIONS ProxyOptions = new
Win32Api.WINHTTP_AUTOPROXY_OPTIONS();
Win32Api.WINHTTP_PROXY_INFO ProxyInfo = new
Win32Api.WINHTTP_PROXY_INFO();
ProxyOptions.dwFlags =
Win32Api.WINHTTP_AUTOPROXY_CONFIG_URL;
//ProxyOptions.dwFlags =
Win32Api.WINHTTP_AUTOPROXY_AUTO_DETECT ;
//ProxyOptions.dwAutoDetectFlags =
(Win32Api.WINHTTP_AUTO_DETECT_TYPE_DHCP |
Win32Api.WINHTTP_AUTO_DETECT_TYPE_DNS_A);
ProxyOptions.lpszAutoConfigUrl = PacUri;
// string result;
// Get Proxy
bool IsSuccess = Win32Api.WinHttpGetProxyForUrl(
WinHttpSession, DestinationUrl, ref ProxyOptions, ref
ProxyInfo );
Console.WriteLine("Error: {0}", Win32Api.GetLastError() );
Win32Api.WinHttpCloseHandle(WinHttpSession);
if ( IsSuccess ){
return ProxyInfo.lpszProxy;
}else {
Console.WriteLine("Error: {0}", Win32Api.GetLastError()
);
return null;
}
}
}
}
Regards
BHARATHI