Web services 使用URL调用web服务方法

Web services 使用URL调用web服务方法,web-services,Web Services,因此,我有一个中央web服务,负责管理其他服务。这些服务使用它们的URL在主WS中注册,从而生成它们自己的web服务 我现在需要做的是从中央web服务调用子web服务。我在谷歌上搜索过如何做到这一点,但我只能找到 我希望注册任何web服务,而不是像我找到的解决方案中建议的那样创建web引用 如何在不使用web引用的情况下完成此操作?Alka 如果您使用的是web服务,而不是WCF,则可以在web.config中更改要访问的web服务的URL,也可以通过代理上的URL在代码中更改此URL 乙二醇

因此,我有一个中央web服务,负责管理其他服务。这些服务使用它们的URL在主WS中注册,从而生成它们自己的web服务

我现在需要做的是从中央web服务调用子web服务。我在谷歌上搜索过如何做到这一点,但我只能找到

我希望注册任何web服务,而不是像我找到的解决方案中建议的那样创建web引用

如何在不使用web引用的情况下完成此操作?

Alka

如果您使用的是web服务,而不是WCF,则可以在web.config中更改要访问的web服务的URL,也可以通过代理上的URL在代码中更改此URL

乙二醇


您还可以创建自己的web服务代理并从此处处理web服务重定向。

您可能可以在开发时添加一个web引用,使visual studio能够发现web服务并具有工作智能感知

但是,在代码中,可以动态创建对象

假设您需要使用一个名为TestSoapClient的对象来访问web服务。如果您想使用web引用中的URL创建它,您只需要这样做

TestSoapClient testSoapClient = new TestSoapClient();
该代码将使用默认URL,即添加web引用时指向的URL

如果要使用运行时指定的URL动态创建TestSoapClient对象,请执行以下操作:

        XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas();
        readerQuotas.MaxDepth = 32;
        readerQuotas.MaxStringContentLength = 8192;
        readerQuotas.MaxArrayLength = 16384;
        readerQuotas.MaxBytesPerRead = 4096;
        readerQuotas.MaxNameTableCharCount = 16384;

        BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
        basicHttpBinding.Name = BindingName;
        basicHttpBinding.CloseTimeout = new TimeSpan(0, 1, 0);
        basicHttpBinding.OpenTimeout = new TimeSpan(0, 1, 0);
        basicHttpBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);
        basicHttpBinding.SendTimeout = new TimeSpan(0, 1, 0);
        basicHttpBinding.AllowCookies = false;
        basicHttpBinding.BypassProxyOnLocal = false;
        basicHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
        basicHttpBinding.MaxBufferSize = 65536;
        basicHttpBinding.MaxBufferPoolSize = 524288;
        basicHttpBinding.MaxReceivedMessageSize = 65536;
        basicHttpBinding.MessageEncoding = WSMessageEncoding.Text;
        basicHttpBinding.TextEncoding = Encoding.UTF8;
        basicHttpBinding.TransferMode = TransferMode.Buffered;
        basicHttpBinding.UseDefaultWebProxy = true;
        basicHttpBinding.ReaderQuotas = readerQuotas;
        basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
        basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

        EndpointAddress endpointAddress = new EndpointAddress("YourDynamicUrl");

        TestSoapClient testSoapClient = new TestSoapClient(basicHttpBinding, endpointAddress);
这样,web引用URL的值和配置文件中的值就不会在运行时使用。

好的,问题解决了

我的解决方案是使用web引用并将代理URL更改为我想要的服务。通过这种方式,我可以动态访问我的web服务


谢谢你的回答。

谢谢你的快速回答。尽管我仍然对此感到困惑-我是否必须创建一个服务的web引用来完成您所说的?您可以使用上述解决方案,如果您查看web引用代码,您将发现一个名为reference.cs的文件,这是web服务使用的代码,如果您想创建自己的web服务代理,这是一个很好的起点。
        XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas();
        readerQuotas.MaxDepth = 32;
        readerQuotas.MaxStringContentLength = 8192;
        readerQuotas.MaxArrayLength = 16384;
        readerQuotas.MaxBytesPerRead = 4096;
        readerQuotas.MaxNameTableCharCount = 16384;

        BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
        basicHttpBinding.Name = BindingName;
        basicHttpBinding.CloseTimeout = new TimeSpan(0, 1, 0);
        basicHttpBinding.OpenTimeout = new TimeSpan(0, 1, 0);
        basicHttpBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);
        basicHttpBinding.SendTimeout = new TimeSpan(0, 1, 0);
        basicHttpBinding.AllowCookies = false;
        basicHttpBinding.BypassProxyOnLocal = false;
        basicHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
        basicHttpBinding.MaxBufferSize = 65536;
        basicHttpBinding.MaxBufferPoolSize = 524288;
        basicHttpBinding.MaxReceivedMessageSize = 65536;
        basicHttpBinding.MessageEncoding = WSMessageEncoding.Text;
        basicHttpBinding.TextEncoding = Encoding.UTF8;
        basicHttpBinding.TransferMode = TransferMode.Buffered;
        basicHttpBinding.UseDefaultWebProxy = true;
        basicHttpBinding.ReaderQuotas = readerQuotas;
        basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
        basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

        EndpointAddress endpointAddress = new EndpointAddress("YourDynamicUrl");

        TestSoapClient testSoapClient = new TestSoapClient(basicHttpBinding, endpointAddress);