Home >WeChat Applet >WeChat Development >WeChat public platform development: Web proxy function

WeChat public platform development: Web proxy function

高洛峰
高洛峰Original
2017-02-27 13:47:232366browse

Starting from Senparc.Weixin.dll v4.5.7, we provide a Web proxy function to facilitate applications in restricted LANs to call interfaces smoothly.

The relevant modifications are in Senparc.Weixin/Utilities/HttpUtility/RequestUtility.cs:

#region 代理

        private static WebProxy _webproxy = null;

        /// <summary>
        /// 设置Web代理
        /// </summary>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        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);
            }
        }

        /// <summary>
        /// 清除Web代理状态
        /// </summary>
        public static void RemoveHttpProxy()
        {
            _webproxy = null;
        }

        #endregion

The usage method is as follows:

1. Set the proxy message before calling the interface (Only needed once globally):

//设置
RequestUtility.SetHttpProxy("http://192.168.1.130","8088","username","pwd");

2. When the interface method is called normally, this proxy parameter will be automatically applied.

When the proxy status needs to be cleared, the RemoveHttpProxy() method is executed globally:

//清除
RequestUtility.RemoveHttpProxy();


For more articles related to WeChat public platform development: Web proxy function, please pay attention PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn