Skip to main content
 首页 » 编程设计

c#之Web 服务请求返回 null

2024年02月20日36虾米姐

我正在尝试调用托管在 Java 服务器上的 Web 服务。我已经通过 Visual Studio 提供的 Web 服务创建工具创建了这些类。当我调用该方法时,我可以在 Fiddler 中看到它正在返回有效数据。但是,在我的 C# 代码中,我的结果为空。

如有任何帮助,我们将不胜感激。

C#调用代码:

RuleValidationResponseRule[] ruleResponse = rulesWebService.RuleValidation(ruleData, "LO"); 

Web 服务生成的代码:

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://www.example.org/LathamInt/RuleValidation",  
    RequestNamespace="http://www.example.org/LathamInt/",  
    ResponseNamespace="http://www.example.org/LathamInt/",  
    Use=System.Web.Services.Description.SoapBindingUse.Literal,  
    ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] 
[return: System.Xml.Serialization.XmlArrayAttribute("ruleValidationResp",  
    Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
[return: System.Xml.Serialization.XmlArrayItemAttribute("rule",  
    Form=System.Xml.Schema.XmlSchemaForm.Unqualified,  
    IsNullable=false)] 
public RuleValidationResponseRule[] RuleValidation([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]  
    RuleValidationRuleData ruleData,  
    [System.Xml.Serialization.XmlAttributeAttribute()] string company) { 
    object[] results = this.Invoke("RuleValidation", new object[] { 
                ruleData, 
                company}); 
    return ((RuleValidationResponseRule[])(results[0])); 
} 

传递给网络服务的 XML(来自 Fiddler):

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
        <RuleValidation company="LO" xmlns="http://www.example.org/LathamInt/"> 
            <ruleData xmlns=""> 
                <rule ruleID="GDSP" /> 
                <varData varName="GuideSpace" varValue="3" /> 
            </ruleData> 
        </RuleValidation> 
    </soap:Body> 
</soap:Envelope> 

从服务返回的 XML(来自 Fiddler):

<?xml version="1.0" encoding="utf-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lat="http://www.example.org/LathamInt/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
        <RuleValidationResponse> 
            <ruleValidationResp> 
                <rule result="0" ruleId="GDSP" status="success"> 
                    <expression pre="@GuideSpace > 3" post="3 > 3"/> 
                </rule> 
            </ruleValidationResp> 
        </RuleValidationResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

C# 生成代码中的响应定义:

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.17929")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.example.org/LathamInt/")] 
public partial class RuleValidationResponseRule { 
 
    private RuleValidationResponseRuleExpression expressionField; 
 
    private string resultField; 
 
    private string ruleIdField; 
 
    private string statusField; 
 
    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public RuleValidationResponseRuleExpression expression { 
        get { 
            return this.expressionField; 
        } 
        set { 
            this.expressionField = value; 
        } 
    } 
 
    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string result { 
        get { 
            return this.resultField; 
        } 
        set { 
            this.resultField = value; 
        } 
    } 
 
    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string ruleId { 
        get { 
            return this.ruleIdField; 
        } 
        set { 
            this.ruleIdField = value; 
        } 
    } 
 
    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string status { 
        get { 
            return this.statusField; 
        } 
        set { 
            this.statusField = value; 
        } 
    } 
} 

请您参考如下方法:

我相信我已经解决了它。

在返回的 XML 中,这一行

<RuleValidationResponse> 

表示返回标签没有命名空间。需要改成

<lat:RuleValidationResponse> 

要么那样,要么修改行

[System.Web.Services.Protocols.SoapDocumentMethodAttribute ("http://www.example.org/LathamInt/RuleValidation",  
    RequestNamespace="http://www.example.org/LathamInt/",  
    ResponseNamespace="http://www.example.org/LathamInt/",  //<-- ** Change this line 
    Use=System.Web.Services.Description.SoapBindingUse.Literal,  
    ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] 

到

[System.Web.Services.Protocols.SoapDocumentMethodAttribute ("http://www.example.org/LathamInt/RuleValidation",  
    RequestNamespace="http://www.example.org/LathamInt/",  
    ResponseNamespace="",  //<-- ** Change this line 
    Use=System.Web.Services.Description.SoapBindingUse.Literal,  
    ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]