JeffreySu / WeiXinMPSDK

微信全平台 .NET SDK, Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 8.0。已支持微信公众号、小程序、小游戏、微信支付、企业微信/企业号、开放平台、JSSDK、微信周边等全平台。 WeChat SDK for C#.
https://weixin.senparc.com
Apache License 2.0
8.43k stars 4.35k forks source link

希望增加request的代理功能 #130

Closed NoneDay closed 8 years ago

NoneDay commented 8 years ago

我的微信服务器是在内网上,如果要访问外部腾讯的网址,必须要加代理。我现在是手工修改程序然后编译的。希望能在RequestUtility 的正式版本中能直接加上类似代码。以后我就更新sdk就不用手工改了。不知道能不能响应我的需求。 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); String proxy_host = System.Configuration.ConfigurationManager.AppSettings["PROXY_HOST"]; String proxy_port = System.Configuration.ConfigurationManager.AppSettings["PROXY_PORT"];

        if (!String.IsNullOrEmpty(proxy_host )
        {
            WebProxy myProxy = new WebProxy(proxy_host  ,proxy_port );
            request.Proxy = myProxy;
        }
jiehanlin commented 8 years ago

我的做法是: public static class RequestUtility { private static WebProxy _webproxy = null; public static void SetHttpProxy(string host, string port, string username, string password) { ICredentials cred; cred = new NetworkCredential(username, password); if (!string.IsNullOrEmpty(host)) { _webproxy = new WebProxy(host + ":" + port ?? "80", true, null, cred); } }

    #region 同步方法

    /// <summary>
    /// 使用Get方法获取字符串结果(没有加入Cookie)
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static string HttpGet(string url, Encoding encoding = null)
    {
        WebClient wc = new WebClient();
        wc.Proxy = _webproxy;
        wc.Encoding = encoding ?? Encoding.UTF8;

//用户调用SetHttpProxy设置代理或取消代理。

JeffreySu commented 8 years ago

@lzmch @jiehanlin 非常感谢两位的建议,Senparc.Weixin.dll v4.5.7中已经加入了web代理的功能。欢迎多提意见,感谢!