0

我想编写 ac# 客户端来使用 Web 服务,在添加服务引用并获取代理类之后,我编写如下代码:

        private void button1_Click(object sender, EventArgs e)
        {

            servicePortTypeClient sc = new servicePortTypeClient();

            string stID=sc.getTableByTitle("Equipment");
        }

但是当我运行这段代码时,我得到一个异常:错误的授权参数

有一些如何使用这个网络服务的代码,但是代码是用php编写的,下面是示例:

  $client = new SoapClient(
    'https://rtu2/mvtspro/extensions/service/?wsdl',
    array(
       'timeout' => 1000
     )
   );
   $headers = array();
   $headers[] = new  SoapHeader('http://merasystems.com/auth','Login','admin');
   $headers[] = new  SoapHeader('http://merasystems.com/auth','Password','admin');
   $client->__setSoapHeaders($headers);

我想也许我对如何与服务器进行身份验证有一些错误,但我不知道如何编写代码来做到这一点,有人能弄清楚吗?

以下是该服务的 wsdl 文档:

        <s:complexType name="rowset">
            <s:complexContent>
                <s:restriction base="ns:Array">
                    <s:sequence>
                        <s:element name="item" type="ns3:row" maxOccurs="unbounded"/>
                    </s:sequence>
                    <s:attribute ref="ns:arrayType" arrayType="ns3:row[]" />
                </s:restriction>
            </s:complexContent>
        </s:complexType>

        <s:complexType name="row">
            <s:complexContent>
                <s:restriction base="ns:Array">
                    <s:sequence>
                        <s:element name="item" type="ns3:column" maxOccurs="unbounded"/>
                    </s:sequence>
                    <s:attribute ref="ns:arrayType" arrayType="ns3:column[]" />
                </s:restriction>
            </s:complexContent>
        </s:complexType>

        <s:complexType name="column">
            <s:all>
                <s:element name="name" type="s:string"/>
                <s:element name="value" type="s:string"/>
            </s:all>
        </s:complexType>

        <s:complexType name="sort">
            <s:complexContent>
                <s:restriction base="ns:Array">
                    <s:sequence>
                        <s:element name="item" type="ns3:sort_item" maxOccurs="unbounded"/>
                    </s:sequence>
                    <s:attribute ref="ns:arrayType" arrayType="ns3:sort_item[]" />
                </s:restriction>
            </s:complexContent>
        </s:complexType>

        <s:complexType name="sort_item">
            <s:all>
                <s:element name="column" type="s:string"/>
                <s:element name="dir" type="ns3:order_type"/>
            </s:all>
        </s:complexType>

        <s:simpleType name="order_type">
            <s:restriction base="s:string">
                <s:enumeration value="asc"/>
                <s:enumeration value="desc"/>
            </s:restriction>
        </s:simpleType>

        <s:complexType name="filter_childs_arr">
            <s:complexContent>
                <s:restriction base="ns:Array">
                    <s:sequence>
                        <s:element name="item" type="ns3:filter" maxOccurs="unbounded"/>
                    </s:sequence>
                    <s:attribute ref="ns:arrayType" arrayType="ns3:filter[]" />
                </s:restriction>
            </s:complexContent>
        </s:complexType>

        <s:complexType name="filter">
            <s:all>
                <s:element name="type" type="s:string"/>
                <s:element name="column" type="s:string" nillable="true"/>
                <s:element name="operator" type="s:string" nillable="true"/>
                <s:element name="value" type="s:string" nillable="true"/>
                <s:element name="childs" type="ns3:filter_childs_arr" nillable="true"/>
            </s:all>
        </s:complexType>

    </s:schema>
</wsdl:types>
<message name="insertRowsetRequest">
    <part name="p_table_hi" type="s:string"/>
    <part name="p_rowset" type="ns3:rowset"/>
</message>
<message name="insertRowsetResponse">
    <part name="result" type="s:integer"/>
</message>
<message name="updateRowsetRequest">
    <part name="p_table_hi" type="s:string"/>
    <part name="p_rowset" type="ns3:rowset"/>
    <part name="p_filter" type="ns3:filter"/>
</message>
<message name="updateRowsetResponse">
    <part name="result" type="s:integer"/>
</message>
<message name="deleteRowsetRequest">
    <part name="p_table_hi" type="s:string"/>
    <part name="p_rowset" type="ns3:rowset"/>
    <part name="p_filter" type="ns3:filter"/>
</message>
<message name="deleteRowsetResponse">
    <part name="result" type="s:integer"/>
</message>
<message name="selectRowsetRequest">
    <part name="p_table_hi" type="s:string"/>
    <part name="p_filter" type="ns3:filter"/>
    <part name="p_sort" type="ns3:sort"/>
    <part name="p_limit" type="s:integer"/>
    <part name="p_offset" type="s:integer"/>
</message>
<message name="selectRowsetResponse">
    <part name="result" type="s:anyType"/>
</message>
<message name="countRowsetRequest">
    <part name="p_table_hi" type="s:string"/>
    <part name="p_filter" type="ns3:filter"/>
</message>
<message name="countRowsetResponse">
    <part name="result" type="s:integer"/>
</message>
<message name="getTableByTitleRequest">
    <part name="p_title" type="s:string"/>
</message>
<message name="getTableByTitleResponse">
    <part name="result" type="s:string"/>
</message>
<message name="describeColumnsRequest">
    <part name="p_table_hi" type="s:string"/>
</message>
<message name="describeColumnsResponse">
    <part name="result" type="s:anyType"/>
</message>
<message name="getColumnLookupRequest">
    <part name="p_table_hi" type="s:string"/>
    <part name="p_column_nm" type="s:string"/>
</message>
<message name="getColumnLookupResponse">
    <part name="result" type="s:anyType"/>
</message>
<portType name="servicePortType">
    <operation name="insertRowset">
        <input message="ns3:insertRowsetRequest"/>
        <output message="ns3:insertRowsetResponse"/>
    </operation>
    <operation name="updateRowset">
        <input message="ns3:updateRowsetRequest"/>
        <output message="ns3:updateRowsetResponse"/>
    </operation>
    <operation name="deleteRowset">
        <input message="ns3:deleteRowsetRequest"/>
        <output message="ns3:deleteRowsetResponse"/>
    </operation>
    <operation name="selectRowset">
        <input message="ns3:selectRowsetRequest"/>
        <output message="ns3:selectRowsetResponse"/>
    </operation>
    <operation name="countRowset">
        <input message="ns3:countRowsetRequest"/>
        <output message="ns3:countRowsetResponse"/>
    </operation>
    <operation name="getTableByTitle">
        <input message="ns3:getTableByTitleRequest"/>
        <output message="ns3:getTableByTitleResponse"/>
    </operation>
    <operation name="describeColumns">
        <input message="ns3:describeColumnsRequest"/>
        <output message="ns3:describeColumnsResponse"/>
    </operation>
    <operation name="getColumnLookup">
        <input message="ns3:getColumnLookupRequest"/>
        <output message="ns3:getColumnLookupResponse"/>
    </operation>
</portType>
<binding name="servicePortBinding" type="ns3:servicePortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="insertRowset">
        <soap:operation soapAction=""/>
        <input>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </input>
        <output>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </output>
    </operation>
    <operation name="updateRowset">
        <soap:operation soapAction=""/>
        <input>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </input>
        <output>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </output>
    </operation>
    <operation name="deleteRowset">
        <soap:operation soapAction=""/>
        <input>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </input>
        <output>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </output>
    </operation>
    <operation name="selectRowset">
        <soap:operation soapAction=""/>
        <input>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </input>
        <output>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </output>
    </operation>
    <operation name="countRowset">
        <soap:operation soapAction=""/>
        <input>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </input>
        <output>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </output>
    </operation>
    <operation name="getTableByTitle">
        <soap:operation soapAction=""/>
        <input>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </input>
        <output>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </output>
    </operation>
    <operation name="describeColumns">
        <soap:operation soapAction=""/>
        <input>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </input>
        <output>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </output>
    </operation>
    <operation name="getColumnLookup">
        <soap:operation soapAction=""/>
        <input>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </input>
        <output>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://mfisoft.ru/soap"/>
        </output>
    </operation>
</binding>
<service name="Service">
    <port name="ServicePortType" binding="ns3:servicePortBinding">
        <soap:address location="https://111.111.111.20/service/service.php?soap"/>
    </port>
</service>

4

1 回答 1

0

您可以像这样将您的凭据发送到 WCF 服务,

servicePortTypeClient sc = new servicePortTypeClient();

sc.ClientCredentials.UserName.UserName = yourUserName;
sc.ClientCredentials.UserName.Password = yourPassword

string stID=sc.getTableByTitle("Equipment");
于 2013-05-26T09:36:51.253 回答