将wsa标头添加到SOAP调用

时间:2018-09-11 10:05:45

标签: c# soap soaphttpclientprotocol

在调用SOAP WS时,我们必须在SOAP标头中指定wsa。看起来像这样:

            <env:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
                           <wsa:To env:mustUnderstand="true">_enpoint_</wsa:To>
                           <wsa:Action>_action_</wsa:Action>
            </env:Header>

我们已经使用wsdl.exe从wsdl中自动生成了代理

为避免不得不修改自动生成的代码,我想添加一个新的局部类来完成这项工作。我喜欢this approach这样做,所以我得到了以下代码:

public partial class WebserviceProxy
{
    private const string DEFAULT_WSA_NAMESPACE = "wsa";

    private string _wsaNamespace;

    public string WsaNamespace
    {
        get => _wsaNamespace ?? (_wsaNamespace = DEFAULT_WSA_NAMESPACE);
        set => _wsaNamespace = value;
    }

    public string To { get; set; }

    public string Action { get; set; }

    protected override WebRequest GetWebRequest(Uri uri)
    {
        WebRequest request = base.GetWebRequest(uri);

        request.Headers.Add($"{WsaNamespace}:{nameof(To)}", To);
        request.Headers.Add($"{WsaNamespace}:{nameof(Action)}", Action);

        return request;
    }
}

问题是该代码无法正常工作,并且在调用base.GetWebRequest(uri)时出现异常:

System.ArgumentException: Specified value has invalid HTTP Header characters.
Parameter name: name
   at System.Net.WebHeaderCollection.CheckBadChars(String name, Boolean isHeaderValue)
   at System.Net.WebHeaderCollection.Add(String name, String value)
   at IcamWebserviceProxy.GetWebRequest(Uri uri)

由于我不确定自己走的路是否正确,因此我在这里寻求帮助。

即使解决了上述问题,我仍然必须将正确的wsa命名空间指定为Heder属性。

[edit] URI(针对在同一台计算机上运行的SoapUI端点进行了测试):

-       uri {http://localhost:9000/mockDataExchangeAdminservice}    System.Uri
        AbsolutePath    "/mockDataExchangeAdminservice" string
        AbsoluteUri "http://localhost:9000/mockDataExchangeAdminservice"    string
        AllowIdn    false   bool
        Authority   "localhost:9000"    string
        DnsSafeHost "localhost" string
        Fragment    ""  string
        HasAuthority    true    bool
        Host    "localhost" string
        HostNameType    Dns System.UriHostNameType
        HostType    DnsHostType System.Uri.Flags
        IdnHost "localhost" string
        IsAbsoluteUri   true    bool
        IsDefaultPort   false   bool
        IsDosPath   false   bool
        IsFile  false   bool
        IsImplicitFile  false   bool
        IsLoopback  true    bool
        IsNotAbsoluteUri    false   bool
        IsUnc   false   bool
        IsUncOrDosPath  false   bool
        IsUncPath   false   bool
        LocalPath   "/mockDataExchangeAdminservice" string
        OriginalString  "http://localhost:9000/mockDataExchangeAdminservice"    string
        OriginalStringSwitched  false   bool
        PathAndQuery    "/mockDataExchangeAdminservice" string
        Port    9000    int
        PrivateAbsolutePath "/mockDataExchangeAdminservice" string
        Query   ""  string
        Scheme  "http"  string
        SecuredPathIndex    0   ushort
+       Segments    {string[2]} string[]
+       Syntax  {System.UriParser.BuiltInUriParser} System.UriParser {System.UriParser.BuiltInUriParser}
        UserDrivenParsing   false   bool
        UserEscaped false   bool
        UserInfo    ""  string
        m_DnsSafeHost   null    string
        m_Flags DnsHostType | AuthorityFound | LoopbackHost | NotDefaultPort | MinimalUriInfoSet | AllUriInfoSet | HostUnicodeNormalized | RestUnicodeNormalized    System.Uri.Flags
+       m_Info  {System.Uri.UriInfo}    System.Uri.UriInfo
        m_String    "http://localhost:9000/mockDataExchangeAdminservice"    string
+       m_Syntax    {System.UriParser.BuiltInUriParser} System.UriParser {System.UriParser.BuiltInUriParser}
        m_iriParsing    true    bool
        m_originalUnicodeString null    string
+       Static members  

[edit2]返回webRequest时,从HttpWebClientProtocol.GetWebRequest抛出异常(最后一行):

protected override WebRequest GetWebRequest(Uri uri)
{
  WebRequest webRequest = base.GetWebRequest(uri);
  HttpWebRequest httpWebRequest = webRequest as HttpWebRequest;
  if (httpWebRequest != null)
  {
    httpWebRequest.UserAgent = this.UserAgent;
    httpWebRequest.AllowAutoRedirect = this.allowAutoRedirect;
    httpWebRequest.AutomaticDecompression = this.enableDecompression ? DecompressionMethods.GZip : DecompressionMethods.None;
    httpWebRequest.AllowWriteStreamBuffering = true;
    httpWebRequest.SendChunked = false;
    if (this.unsafeAuthenticatedConnectionSharing != httpWebRequest.UnsafeAuthenticatedConnectionSharing)
      httpWebRequest.UnsafeAuthenticatedConnectionSharing = this.unsafeAuthenticatedConnectionSharing;
    if (this.proxy != null)
      httpWebRequest.Proxy = this.proxy;
    if (this.clientCertificates != null && this.clientCertificates.Count > 0)
      httpWebRequest.ClientCertificates.AddRange(this.clientCertificates);
    httpWebRequest.CookieContainer = this.cookieJar;
  }
  return webRequest;
}

0 个答案:

没有答案