Пример #1
0
        // 保证OpenUrl在主线程调用
        public static HttpClient OpenUrl <T>(string url, T listener, long filePos, Action <HttpClient, HttpListenerStatus> OnEnd = null,
                                             Action <HttpClient> OnProcess = null, float connectTimeOut = 5.0f, float readTimeOut = 5.0f, string postStr = "", bool isKeepAlive = true, List <X509Certificate> certs = null) where T : HttpClientResponse
        {
            if (string.IsNullOrEmpty(url) || listener == null || filePos < 0)
            {
                return(null);
            }

            HttpClient         ret      = CreateHttpClient();
            HttpClientCallBack callBack = null;

            if (OnEnd != null || OnProcess != null)
            {
                callBack           = new HttpClientCallBack();
                callBack.OnEnd     = OnEnd;
                callBack.OnProcess = OnProcess;
            }
            ret.UserData = callBack;
            byte[] postBuf = null;
            if (!string.IsNullOrEmpty(postStr))
            {
                try {
                    postBuf = System.Text.Encoding.UTF8.GetBytes(postStr);
                } catch {
                    postBuf = null;
                }
            }

            HttpClientType clientType = postBuf != null ? HttpClientType.httpPost : HttpClientType.httpGet;

            ret.Init(url, listener, filePos, connectTimeOut, readTimeOut, clientType, postBuf, isKeepAlive, certs);
            m_LinkList.AddLast(ret.LinkNode);

            return(ret);
        }
Пример #2
0
 private byte[] GeneratorPostBuf(HttpClientType clientType, string postStr)
 {
     if (clientType == HttpClientType.httpGet || string.IsNullOrEmpty(postStr))
     {
         return(null);
     }
     byte[] ret = System.Text.Encoding.UTF8.GetBytes(postStr);
     return(ret);
 }
Пример #3
0
        public IProxyHttpClient Create(HttpClientType type)
        {
            switch (type)
            {
            case HttpClientType.HttpClient:
                return(new ProxyHttpClient(new HttpClientHandler()));
            }

            throw new Exception($"{type} is an unknown http client type");
        }
Пример #4
0
        public void Init(string url, IHttpClientListener listener, long filePos, float connectTimeOut, float readTimeOut = 5.0f,
                         HttpClientType clientType = HttpClientType.httpGet, byte[] postBuf = null)
        {
            m_Url         = url;
            m_TimeOut     = connectTimeOut;
            m_ReadTimeOut = readTimeOut;
            m_Listener    = listener;
            m_FilePos     = filePos;
            m_ClientType  = clientType;
            m_PostBuf     = postBuf;
            ResetReadTimeOut();
            ResetConnectTimeOut();

            CheckServicePoint();
            // Get
            Start();
        }
Пример #5
0
        public void Init(string url, IHttpClientListener listener, long filePos, float connectTimeOut, float readTimeOut = 5.0f,
                         HttpClientType clientType = HttpClientType.httpGet, byte[] postBuf = null, bool isKeepAlive = true, List <X509Certificate> certs = null)
        {
            m_Url         = url;
            m_TimeOut     = connectTimeOut;
            m_ReadTimeOut = readTimeOut;
            m_Listener    = listener;
            m_FilePos     = filePos;
            m_ClientType  = clientType;
            m_PostBuf     = postBuf;
            m_IsKeppAlive = isKeepAlive;
            m_Certs       = certs;
            ResetReadTimeOut();
            ResetConnectTimeOut();

            CheckServicePoint();
            // Get
            Start();
        }
        public BaseHttpClient(ITestOutputHelper testOutputHelper, HttpClientType httpClientType)
        {
            this.testOutputHelper = testOutputHelper ?? throw new ArgumentNullException(nameof(testOutputHelper));

            var handler = new HttpClientHandler {
                ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
            };

            switch (httpClientType)
            {
            case HttpClientType.StandardHttpClient:

                httpClient = new HttpClient(handler);
                break;

            default:
                throw new HttpRequestException($"Http client type {httpClientType} not defined");
            }
        }
Пример #7
0
 public HttpClient(string url, IHttpClientListener listener, float connectTimeOut, float readTimeOut = 5.0f,
                   HttpClientType clientType = HttpClientType.httpGet, string postStr = "")
 {
     Init(url, listener, 0, connectTimeOut, readTimeOut, clientType, GeneratorPostBuf(clientType, postStr));
 }
Пример #8
0
 public HttpClient(string url, IHttpClientListener listener, float connectTimeOut, float readTimeOut = 5.0f,
                   HttpClientType clientType = HttpClientType.httpGet, string postStr = "", bool isKeepAlive = true, List <X509Certificate> certs = null)
 {
     Init(url, listener, 0, connectTimeOut, readTimeOut, clientType, GeneratorPostBuf(clientType, postStr), isKeepAlive, certs);
 }