zoukankan      html  css  js  c++  java
  • wcf 代理实例

    通过过代理调用 wcf服务 

    using
    Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.ServiceModel; using System.Threading.Tasks; namespace wcfproxy.Common { public interface IProxy { T SetProxy<T>(); } public class Proxy : IProxy { private readonly IProxyOptions _proxyOptions; public Proxy(IOptionsSnapshot<IProxyOptions> proxyOptionsAccessor) { _proxyOptions = proxyOptionsAccessor.Value; } public T SetProxy<T>() { return GetHttpsProxy<T>(); }
    //HTTPS 请求
    private T GetHttpsProxy<T>() { var bindingHttps = new BasicHttpsBinding(); bindingHttps.UseDefaultWebProxy = false; string proxyAdress = _proxyOptions.ProcyAdress; bindingHttps.ProxyAddress = new Uri(proxyAdress);//代理 bindingHttps.Security.Mode = BasicHttpsSecurityMode.Transport; bindingHttps.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; bindingHttps.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic; var endpointAddress = new EndpointAddress(_proxyOptions.EndpointAddress); var factory = new ChannelFactory<T>(bindingHttps, endpointAddress);
    //安全证书 factory.Credentials.ServiceCertificate.SslCertificateAuthentication
    = new System.ServiceModel.Security.X509ServiceCertificateAuthentication(); factory.Credentials.ServiceCertificate.SslCertificateAuthentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None; var serviceClient = (T)factory.CreateChannel(); return serviceClient; }
    //http 请求
    private T GetHttpProxy<T>() { var binding = new BasicHttpBinding(); binding.UseDefaultWebProxy = false; string proxyAdress = _proxyOptions.ProcyAdress; binding.ProxyAddress = new Uri(proxyAdress);//代理 binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic; var endpointAddress = new EndpointAddress(_proxyOptions.EndpointAddress); var factory = new ChannelFactory<T>(binding, endpointAddress); var serviceClient = (T)factory.CreateChannel(); return serviceClient; } } public static class Util { public static void SetCertificatePolicy() { ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate; } private static bool RemoteCertificateValidate( object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) { // trust any certificate!!! System.Console.WriteLine("Warning, trust any certificate"); return true; } } }
       public class IProxyOptions
        {
            /// <summary>
            /// 代理地址
            /// </summary>
            public string ProcyAdress { get; set; }
    
            /// <summary>
            /// EndpointAddress
            /// </summary>
            public string EndpointAddress { get; set; }
        }
  • 相关阅读:
    poj 1007:DNA Sorting(水题,字符串逆序数排序)
    蓝桥杯北京之旅【第二篇·2014.5.31】
    poj 1006:Biorhythms(水题,经典题,中国剩余定理)
    蓝桥杯北京之旅【第一篇·2014.5.30】
    【2014年6月份日常记录表(2014.6.1—6.30,30天)】
    poj 1002:487-3279(水题,提高题 / hash)
    ThreadPoolExecutor机制探索-我们到底能走多远系列(41)
    ibatis批量操作补充
    Maven管理 划分模块
    顺序队列实现任务以此执行-任务调度系列2
  • 原文地址:https://www.cnblogs.com/cicada/p/10868078.html
Copyright © 2011-2022 走看看