c# - 更改c# WebReference url地址

标签 c# web-services soap sqlanywhere sybase-asa

你好, 我有 2 个客户端和 2 个不同的服务器。 生成 wsdl 类后,我在 SoapHttpClientProtocol consructor 中相应地更改客户端的 url 地址。

来自

this.Url = "http://10.0.3.5:88/SomeName/dish

this.Url = "http://192.168.20.5:88/SomeOtherName/dish

但我无法在运行时更改 SoapDocumentMethodAttribute。如果不更改它,我的方法不会只返回 null 的 DataSet。更改属性中的所有地址后,一切正常。

[System.Web.Services.Protocols.SoapDocumentMethodAttribute( "http://10.0.3.5:88/SomeName/EuroSoft/ProductTransferExecute", RequestNamespace = "http://10.0.3.5:88/SomeName/dish", ResponseNamespace = "http://10.0.3.5:88/SomeName/dish", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = 

System.Web.Services.Protocols.SoapParameterStyle.Wrapped )]
public System.Data.DataSet ProductTransferExecute( [System.Xml.Serialization.XmlElementAttribute( IsNullable = true )] string department, [System.Xml.Serialization.XmlElementAttribute( IsNullable = true )] string XMLproducts, out int sqlcode ) {}

服务由 Sybase Anywhere 9 数据库生成。是否可以动态更改它?什么需要完全相同才能起作用?

最佳答案

创建一个 CustomSoapHttpClientProtocol:

public class CustomSoapHttpClientProtocol : SoapHttpClientProtocol
{
    public string SoapActionUrl { get; private set; }

    public CustomSoapHttpClientProtocol(string soapActionUrl)
    {
        this.SoapActionUrl = soapActionUrl;
    }
    protected override WebResponse GetWebResponse(WebRequest request)
    {
        const string soapAction = "SOAPAction";
        if (request.Headers.Count > 0 && request.Headers.AllKeys.Contains(soapAction))
        {
            request.Headers[soapAction] = SoapActionUrl;
        }
        WebResponse response = base.GetWebResponse(request);
        return response;
    }

然后在您的代理类中将 SoapHttpClientProtocol 替换为您的 CustomSoapHttpClientProtocol。

关于c# - 更改c# WebReference url地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17697900/

相关文章:

c# 如何让我的列表基于 1 而不是基于 0

C# foreach 与功能性每个

c# - 抽象类还是局部的?

java - 在java中部署Web服务

ruby - 如何解析来自 ruby​​ 客户端的 SOAP 响应?

c# - 使用用户名 token 和客户端证书的 WCF SOAP 调用

c# - 如何通过代码在运行时添加迁移

web-services - Sharepoint 上的 Windows 身份验证

c# - 如何处理一些异步的 TcpClient 响应?

PHP SOAP 传输文件