如何为SoapClient分配特定的主机和URI

时间:2019-07-17 18:11:48

标签: c# web-services soap soap-client

尝试使用SoapClient之类的HttpClient。我使用多种不同的语言,因此如果我的代码示例在vb.net和c#之间触发器,我深表歉意。

我需要定位到特定IP的原因是我有多台服务器,并且想分别测试它们。

此代码适用于HttpClient。

private HttpClient request = new HttpClient() { DefaultRequestHeaders = { Host = "abc.google.com" } };
HttpResponseMessage responseFromApi = request.GetAsync(new Uri($"https://{server.IpAddress}/health/healthcheck.html")).ConfigureAwait(false).GetAwaiter().GetResult();

我有一个通过Visual Studio服务参考(WSDL)添加的SoapEndpoint。我希望仍然可以定位某些IP。此代码不起作用。有什么建议吗?

Dim wsRate As RateUtilitySoapClient = New RateUtilitySoapClient()
Dim addressHeader = Channels.AddressHeader.CreateAddressHeader("Host", "", tbEndPoint.Text)
Dim endpoint = New EndpointAddress(New Uri(String.Format("http://{0}/SoapEndpoint.asmx", IPToTarget)), New AddressHeader() {addressHeader})

wsRate.Endpoint.Address = endpoint

1 个答案:

答案 0 :(得分:0)

您的web.config应该具有带有绑定的system.serviceModel config部分。此绑定应具有一个相应的客户端端点配置,在这里可以设置所需的任何主机。

例如

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="BasicHttpBinding_IMyService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
        <security mode="Transport">
          <transport clientCredentialType="Certificate" />
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>
  <client>
    <endpoint address="https://myservice.local.myCompany.com/MyService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService" contract="MyService.IMyService" name="BasicHttpBinding_IMyService" />
  </client>
</system.serviceModel>

希望这会有所帮助。祝你好运。