开发者

Proxy credentials for programmatically web service proxy class

开发者 https://www.devze.com 2023-03-25 19:02 出处:网络
I am having problem to consume a WebService programmatically, using the WSDL under a squid proxy. My application is build in c# .net. I compile an Assembly from the XML, after import the service descr

I am having problem to consume a WebService programmatically, using the WSDL under a squid proxy. My application is build in c# .net. I compile an Assembly from the XML, after import the service descripton using this:

// a namespace and compile unit are开发者_如何学C needed by importer
        CodeNamespace codeNamespace = new CodeNamespace();
        CodeCompileUnit codeUnit = new CodeCompileUnit();

        codeUnit.Namespaces.Add(codeNamespace);

        ServiceDescriptionImportWarnings importWarnings = descriptionImporter.Import(codeNamespace, codeUnit);

        if (importWarnings == 0) // no warnings
        {
            // create a c# compiler
            CodeDomProvider compiler = CodeDomProvider.CreateProvider("CSharp");

            // include the assembly references needed to compile
            string[] references = new string[2] { "System.Web.Services.dll", "System.Xml.dll" };

            CompilerParameters parameters = new CompilerParameters(references);

            // compile into assembly
            CompilerResults results = compiler.CompileAssemblyFromDom(parameters, codeUnit);

            foreach (CompilerError oops in results.Errors)
            {
                // trap these errors and make them available to exception object
                throw new Exception("Compilation Error Creating Assembly");
            }

            // all done....
            return results.CompiledAssembly;
        }
        else
        {
            // warnings issued from importers, something wrong with WSDL
            throw new Exception("Invalid WSDL");
        }

The problem is when i call the method Invoke(obj, args). Proxy cut the connection, if i call the WSDL using external address, like http://My_external_ip/my_webService.asmx. If i call using internal address, works fine.

When i add a webReference, manually, i use to do some thing like:

WebService WS = new WebService();
WS.Proxy = Proxy.credentials;

it work, but i couldn't find where to give the proxy credentials when using Assembly.

Thanks guys.


@Various,

You probebly want to write some code like this

WebService WS = new WebService();
WS.Proxy = wwwproxy("http://someproxy:8080";



  WebProxy wwwproxy(string ptProxyURI) 
{
        var aProxy = New WebProxy;
        aProxy.Credentials = CredentialCache.DefaultCredentials;
        aProxy.BypassProxyOnLocal = True;
        aProxy.Address = New Uri(ptProxyURI);
        Return aProxy;
}

Hope it helps.

Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号