2013-05-26 95 views
0

我想寫一個C#客戶端來使用Web服務,之後我添加服務引用,並得到一個代理類,我寫這樣的代碼:如何設置soapHeader在c#中進行身份驗證?

 private void button1_Click(object sender, EventArgs e) 
     { 

      servicePortTypeClient sc = new servicePortTypeClient(); 

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

但是當我運行這段代碼,我得到一個例外中:授權錯誤的參數

有如何使用這個Web服務的一些代碼,但是代碼是由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); 

我想也許我有一些錯誤如何使用serv進行authenricate呃,但我不知道如何編寫代碼來做到這一點,任何人都可以弄明白嗎?下面

是服務的日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> 

回答

0

您可以發送您的憑據這樣的WCF服務,

servicePortTypeClient sc = new servicePortTypeClient(); 

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

string stID=sc.getTableByTitle("Equipment"); 
+0

謝謝你,我已經嘗試此,但仍然得到「授權的錯誤參數」的例外,是否表示我的用戶名或密碼不正確? – yangl

+0

這是第三方的網絡服務,所以我只能得到wsdl文件,在wsdl中,我沒有找到任何「soaphead」定義,但該服務的api文件確實需要用戶名和密碼進行身份驗證,可以告訴任何人我是什麼原因? – yangl

相關問題