尝试使用 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 Service References(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
你的 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>
希望这有帮助。 祝你好运。