2016-06-07 88 views
0

我必須爲肥皂web服務插入此頭文件客戶端請求。我該怎麼辦?如何在c#中的客戶端請求中插入此soap頭文件

<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"> 
    <wsse:Security soap:mustUnderstand="true" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
    <wsse:UsernameToken wsu:Id="UsernameToken-A116E02D59482B66FF14652216221791"><wsse:Username>test</wsse:Username> 
    <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">test2</wsse:Password> 
    <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">7og8drnSMbkytwYPlDrCCg==</wsse:Nonce><wsu:Created>2016-06-06T14:00:22.177Z</wsu:Created> 
    </wsse:UsernameToken></wsse:Security> 
    <wsa:Action soap:mustUnderstand="1">http://tempuri.org/IQuantityService/LoadDebitAmount</wsa:Action></soap:Header> 

客戶端代碼:我使用的WSHttpBinding肥皂網絡service.So我不得不在above.How發送用肥皂頭請求我能做什麼?

private void btnLoadAmountTest_Click(object sender, EventArgs e) 
    { 
     System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; }; 

     try 
     { 

      WSHttpBinding userNameBinding = new WSHttpBinding(); 
      userNameBinding.Security.Mode = SecurityMode.Transport; 
      userNameBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; 

      EndpointAddress endpointAddress = new EndpointAddress(portB.ToString()); 

      QuantityServiceClient client = new ServiceReferenceLoadQuantity.QuantityServiceClient(userNameBinding, endpointAddress); 
       client.ChannelFactory.Endpoint.Behaviors.Remove<System.ServiceModel.Description.ClientCredentials>(); 
      client.ChannelFactory.Endpoint.Behaviors.Add(new CustomCredentials()); 
      client.ClientCredentials.UserName.UserName = txtUser2.Text; 
      client.ClientCredentials.UserName.Password = txtPsw2.Text; 


      //using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) 
      //{ 

      // var httpRequestProperty = new HttpRequestMessageProperty(); 
      // httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = 
      //  "Basic " + 
      //  Convert.ToBase64String(Encoding.ASCII.GetBytes(
      //    client.ClientCredentials.UserName.UserName + ":" + 
      //    client.ClientCredentials.UserName.Password)); 

      // OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = 
      //    httpRequestProperty; 


      // // client.ClientCredentials.SupportInteractive = true; 





      //using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) 
      //{ 

      // OperationContext.Current.OutgoingMessageHeaders.Add(
      //  new SecurityHeader("UsernameToken-49", client.ClientCredentials.UserName.UserName, client.ClientCredentials.UserName.Password)); 
       OperationResult result = new OperationResult(); 


       result = client.LoadDebitAmount(txtLoadAmountCust.Text, txtLoadAmountDep.Text, txtLoadAmountPlate.Text, txtLoadAmountFType.Text, txtLoadAmountFCode.Text, txtLoadAmountAmt.Text, txtLoadAmountInvNo.Text, txtLoadAmountInvDate.Text); 

       txtLoadAmountOut.Text = result.ReturnCode.ToString() + " " + result.ReturnMessage.ToString(); 
       // ServiceReference1.GlobalWeatherSoapClient soap = new ServiceReference1.GlobalWeatherSoapClient(); 

       //txtLoadQuantityOutText.Text= soap.GetWeather("Istanbul","Turkey"); 
       client.Close(); 
      //} 
      //} 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error: " + ex.Message); 

     } 
    } 
+0

您想使用哪種客戶端語言? js/jquery。請分享你的工作... – Saadi

+0

不,我使用C#窗體窗體應用程序。 –

+0

但你提到了客戶端請求。不是C#服務器端? – Saadi

回答

0

您可以使用屬性[MessageHeader]定義頁眉和屬性[MessageBodyMember]來定義SOAP消息的身體。在您的WCF服務方法定義中使用[OperationContract]添加YourClass作爲參數。然後在你的客戶端只需以YourClass作爲參數調用這個方法。

[MessageContract(IsWrapped = false)] 
    public class YourClass 
    { 
     [MessageHeader] 
     public string HeaderName; 

     [MessageBodyMember(Order = 1)] 
     public string BodyVariable; 
    } 

另一種方式來添加信息標題:

在代理類:

MessageHeader<string> header = new MessageHeader<string>("YourHeaderValue"); 
    OperationContextScope contextScope = new OperationContextScope(InnerChannel);     
OperationContext.Current.OutgoingMessageHeaders.Add(header.GetUntypedHeader("String","System")); 

在服務類:

OperationContext context = OperationContext.Current; 
var HeaderValue = context.IncomingMessageHeaders.GetHeader<string>("String", "System"); 
+0

確定後,如何添加肥皂頭信息? –

+0

我真的推薦創建樣板化的Visual Studio WCF解決方案,並嘗試在那裏首先完成。這是WCF服務使用的一個很好的基本示例。 – Nick

+0

尼克我已經有web service.But我寫一個客戶端項目進行testing.Normally我使用soapui.But我寫了一個測試工具。所以我必須發送webservice這個請求在客戶端。 –