我需要连接到一些公共wcf服务,但我和服务之间有一些代理.如果我使用默认代理设置,如
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
Run Code Online (Sandbox Code Playgroud)
要么
HttpWebRequest.DefaultWebProxy
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但我不需要为整个应用程序设置代理设置,我需要为特定连接设置它.那我该怎么做呢?
我看到了ProxyAddress属性
(client.Endpoint.Binding as BasicHttpBinding).ProxyAddress
Run Code Online (Sandbox Code Playgroud)
但是凭证没有任何属性......我想以某种方式修改HttpWebRequest,但我不知道怎么弄它...
解决了
谢谢大家的答案.
AntonK的答案适合解决我的问题.
在这个问题是实际的时候,我以同样的方式解决了它,但没有使用web.config并编写了这个方法
void SetProxySettings<TChannel>(ClientBase<TChannel> client,
bool useProxy, string address, int port, string login, string password)
where TChannel : class
{
if (!useProxy) return;
var b = client.Endpoint.Binding as BasicHttpBinding;
if (b == null)
{
System.Diagnostics.Debug.WriteLine("Binding of this endpoint is not BasicHttpBinding");
return;
}
b.ProxyAddress = new Uri(string.Format("http://{0}:{1}", address, port));
b.UseDefaultWebProxy = false; // !!!
b.Security.Mode = BasicHttpSecurityMode.Transport;
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; // !!!
b.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic; // !!!
if (client.ClientCredentials == null) return;
client.ClientCredentials.UserName.UserName = login;
client.ClientCredentials.UserName.Password = password;
}
Run Code Online (Sandbox Code Playgroud)
这是一篇关于这个问题的文章.
总之,这是如何在web.config中为特定服务设置代理.在绑定配置中,设置proxyAddress ="http:// myproxy:8080"并设置useDefaultWebProxy ="false"
<bindings>
<basicHttpBinding>
<binding name="SubscriberFulfilmentServiceSOAP12Binding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="false"
proxyAddress="http://myproxy:8080"
messageEncoding="Text">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647"
maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17200 次 |
| 最近记录: |