通过过代理调用 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; }
}

最新文章

  1. MongoDB安装并随windows开机自启
  2. Docker:Ubuntu下的安装
  3. 模板方法模式(Template Method)
  4. jQuery插件编写笔记
  5. 把复杂json解析成javabean
  6. Magic skills of vim from zhihu
  7. poj 1840 Eqs (hash)
  8. 系统虚拟化学习笔记——PCI设备
  9. 78 Subsets(求子集Medium)
  10. Eclipse 快捷键整理
  11. layout_weight相关知识
  12. unity 实现流光效果
  13. jdk源码阅读笔记-LinkedHashMap
  14. mysql修改连接数
  15. 科大讯飞语音合成api
  16. 触发full gc的条件
  17. Codeforces 698A - Vacations - [简单DP]
  18. react +webpack 配置px2rem
  19. pytorch使用tensorboardX进行网络可视化
  20. C#爬取京东手机数据+PowerBI数据可视化展示

热门文章

  1. 【死磕Java并发】—–J.U.C之AQS(一篇就够了)
  2. C++ 中的 const 类型变量
  3. Android 监听软键盘按键的三种方式
  4. win7注册表常用设置
  5. MONyog使用图解(一)-数据库性能监控工具
  6. TensorFlow 实现深度神经网络 —— Denoising Autoencoder
  7. 常用Java开发工具类
  8. SCM文章9类:外部中断示例程序
  9. Java、JVM、JRE、JDK等组件的理解
  10. 正割函数(sec)