21

I need to connect to some public wcf service, but there is some proxy between me and service. If i use default proxy settings such as

<system.net>
  <defaultProxy useDefaultCredentials="true" />
</system.net>

or

HttpWebRequest.DefaultWebProxy

it works perfectly fine but i don't need to set proxy settings for entire application, i need to set it for specific connection. So how I can do that?

I saw ProxyAddress property

(client.Endpoint.Binding as BasicHttpBinding).ProxyAddress

but there is no any properties for credentials... I was thinking to somehow modify HttpWebRequest, but I do not know how to get it...

Solved

Thank you all for your answers.

Answer of AntonK suitable for solving my problem.

At the time when this question was actual, I solved it in the same way, but without the use of web.config and wrote this method

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;
}
4

3 回答 3

8

这是一篇处理这个问题的文章。

http://blogs.msdn.com/b/stcheng/archive/2008/12/03/wcf-how-to-supply-dedicated-credentials-for-webproxy-authentication.aspx

总而言之,这就是如何在 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>
于 2015-01-29T03:21:56.200 回答
0

我找到了解决方案。您必须将 WCF 更新到最新版本。

转到 NuGet 包管理器 -> 更新 WCF 的所有相关项目 URL 必须有:

System.ServiceModel.Security
System.ServiceModel.NetTcp
System.ServiceModel.Http

这适用于 .net core 2.1 版本。

于 2020-01-31T04:59:56.247 回答
-1

你可以试试这个

HttpWebRequest request = HttpWebRequest.Create("URI") as HttpWebRequest;
var proxy = new WebProxy(HttpWebRequest.GetSystemWebProxy().GetProxy(request.RequestUri), true);
proxy.Credentials = new NetworkCredential(proxyUserName, proxyPassword, DomainName);
request.Proxy = proxy;

希望能帮助到你

于 2013-07-31T11:01:52.140 回答