Beispiel #1
0
        protected override object GetInvokeContext()
        {
            Uri uri = new Uri(url);

#if !(SILVERLIGHT || WINDOWS_PHONE) || SL2
            HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
#else
            HttpWebRequest request = WebRequestCreator.ClientHttp.Create(uri) as HttpWebRequest;
#endif
#if !(SILVERLIGHT || WINDOWS_PHONE)
            request.Credentials = credentials;
#if !Core
            request.Timeout     = timeout;
            request.SendChunked = false;
#if !(dotNET10 || dotNETCF10)
            request.ReadWriteTimeout = timeout;
#endif
            request.ProtocolVersion = HttpVersion.Version11;
            if (proxy != null)
            {
                request.Proxy = proxy;
            }
            request.KeepAlive = keepAlive;
            if (keepAlive)
            {
                request.Headers.Set("Keep-Alive", KeepAliveTimeout.ToString());
            }
#if !dotNETCF10
            request.ConnectionGroupName = connectionGroupName;
#if !(dotNET10 || dotNET11 || dotNETCF20)
            if (clientCertificates != null)
            {
                request.ClientCertificates = clientCertificates;
            }
#endif
#endif
#endif
#endif
#if (dotNET10 || dotNET11 || dotNETCF10)
            foreach (DictionaryEntry header in headers)
            {
                request.Headers[(string)header.Key] = (string)header.Value;
            }
#else
            foreach (KeyValuePair <string, string> header in headers)
            {
                request.Headers[header.Key] = header.Value;
            }
#endif
#if (PocketPC || Smartphone || WindowsCE)
            request.AllowWriteStreamBuffering = true;
            request.Headers["Cookie"]         = cookieManager.GetCookie(uri.Host,
                                                                        uri.AbsolutePath,
                                                                        uri.Scheme == "https");
#elif !SL2
            request.CookieContainer = cookieContainer;
#endif
            return(new HttpClientContext(request, null));
        }
Beispiel #2
0
    //获取HttpClient
    private HttpWebRequest GetRequest()
    {
        Uri            uri     = new Uri(this.uri);
        HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;

        request.Method      = "POST";
        request.Timeout     = timeout;
        request.SendChunked = false;
        request.ContentType = "application/hprose";

        request.Credentials = credentials;
        request.ServicePoint.ConnectionLimit = Int32.MaxValue;

        if (encoding != null)
        {
            request.Headers.Set("Accept-Encoding", encoding);
        }

        request.ReadWriteTimeout = timeout;
        request.ProtocolVersion  = HttpVersion.Version11;
        if (proxy != null)
        {
            request.Proxy = proxy;
        }

        request.KeepAlive = keepAlive;
        if (keepAlive)
        {
            request.Headers.Set("Keep-Alive", KeepAliveTimeout.ToString());
        }

        request.ConnectionGroupName = connectionGroupName;
        if (clientCertificates != null)
        {
            request.ClientCertificates = clientCertificates;
        }

        foreach (KeyValuePair <string, string> header in headers)
        {
            request.Headers[header.Key] = header.Value;
        }

        request.CookieContainer = cookieContainer;
        return(request);
    }
        public async Task <Stream> Transport(Stream request, Context context)
        {
            var clientContext = context as ClientContext;
            var uri           = clientContext.Uri;
            var timeout       = (int)clientContext.Timeout.TotalMilliseconds;
            var webRequest    = WebRequest.Create(uri) as HttpWebRequest;

            requests[webRequest] = null;
            try {
                webRequest.Method      = "POST";
                webRequest.ContentType = "application/hprose";
                if (Credentials != null)
                {
                    webRequest.Credentials = Credentials;
                }
                webRequest.Timeout     = timeout;
                webRequest.SendChunked = false;
                if (AcceptEncoding != null)
                {
                    webRequest.Headers.Set("Accept-Encoding", AcceptEncoding);
                }
                webRequest.ReadWriteTimeout = timeout;
                webRequest.ProtocolVersion  = HttpVersion.Version11;
                if (Proxy != null)
                {
                    webRequest.Proxy = Proxy;
                }
                webRequest.KeepAlive = KeepAlive;
                if (KeepAlive)
                {
                    webRequest.Headers.Set("Keep-Alive", KeepAliveTimeout.ToString());
                }
                webRequest.ConnectionGroupName = ConnectionGroupName;
                if (ClientCertificates != null)
                {
                    webRequest.ClientCertificates = ClientCertificates;
                }
                webRequest.Headers.Add(HttpRequestHeaders);
                if (context.Contains("httpRequestHeaders"))
                {
                    webRequest.Headers.Add(context["httpRequestHeaders"] as NameValueCollection);
                }
                webRequest.AllowWriteStreamBuffering = true;
                webRequest.Headers.Set("Cookie", cookieManager.GetCookie(uri.Host, uri.AbsolutePath, uri.Scheme == "https"));
                using (var ostream = await webRequest.GetRequestStreamAsync().ConfigureAwait(false)) {
                    await request.CopyToAsync(ostream).ConfigureAwait(false);

                    await ostream.FlushAsync().ConfigureAwait(false);
                }
                using (var webResponse = await webRequest.GetResponseAsync().ConfigureAwait(false) as HttpWebResponse) {
                    requests[webRequest]      = webResponse;
                    context["httpStatusCode"] = (int)webResponse.StatusCode;
                    context["httpStatusText"] = webResponse.StatusDescription;
                    if ((int)webResponse.StatusCode < 200 || (int)webResponse.StatusCode >= 300)
                    {
                        throw new Exception(((int)webResponse.StatusCode) + ":" + webResponse.StatusDescription);
                    }
                    var headers = new NameValueCollection(StringComparer.InvariantCultureIgnoreCase);
                    headers.Add(webResponse.Headers);
                    context["httpResponseHeaders"] = headers;
                    cookieManager.SetCookie(webResponse.Headers.GetValues("Set-Cookie"), uri.Host);
                    cookieManager.SetCookie(webResponse.Headers.GetValues("Set-Cookie2"), uri.Host);
                    int len      = (int)webResponse.ContentLength;
                    var response = (len > 0) ? new MemoryStream(len) : new MemoryStream();
                    using (var istream = webResponse.GetResponseStream()) {
                        string contentEncoding = webResponse.ContentEncoding.ToLower();
                        if (contentEncoding.IndexOf("deflate") > -1)
                        {
                            using (var deflateStream = new DeflateStream(istream, CompressionMode.Decompress)) {
                                await deflateStream.CopyToAsync(response).ConfigureAwait(false);
                            }
                        }
                        else if (contentEncoding.IndexOf("gzip") > -1)
                        {
                            using (var gzipStream = new GZipStream(istream, CompressionMode.Decompress)) {
                                await gzipStream.CopyToAsync(response).ConfigureAwait(false);
                            }
                        }
                        else
                        {
                            await istream.CopyToAsync(response).ConfigureAwait(false);
                        }
                    }
                    response.Position = 0;
                    return(response);
                }
            }
            finally {
                requests.Remove(webRequest, out var _);
            }
        }
Beispiel #4
0
        public async Task <Stream> Transport(Stream request, Context context)
        {
            Uri uri        = (context as ClientContext).Uri;
            var webRequest = WebRequest.Create(uri) as HttpWebRequest;

            requests[webRequest] = null;
            try {
                webRequest.Method      = "POST";
                webRequest.ContentType = "application/hprose";
                webRequest.Credentials = Credentials;
                webRequest.Timeout     = (int)Timeout.TotalMilliseconds;
                webRequest.SendChunked = false;
                if (AcceptEncoding != null)
                {
                    webRequest.Headers.Set("Accept-Encoding", AcceptEncoding);
                }
                webRequest.ReadWriteTimeout = (int)Timeout.TotalMilliseconds;
                webRequest.ProtocolVersion  = HttpVersion.Version11;
                if (Proxy != null)
                {
                    webRequest.Proxy = Proxy;
                }
                webRequest.KeepAlive = KeepAlive;
                if (KeepAlive)
                {
                    webRequest.Headers.Set("Keep-Alive", KeepAliveTimeout.ToString());
                }
                webRequest.ConnectionGroupName = ConnectionGroupName;
                if (ClientCertificates != null)
                {
                    webRequest.ClientCertificates = ClientCertificates;
                }
                foreach (var header in Headers)
                {
                    webRequest.Headers[header.Key] = header.Value;
                }
                webRequest.AllowWriteStreamBuffering = true;
                webRequest.Headers.Set("Cookie", cookieManager.GetCookie(uri.Host, uri.AbsolutePath, uri.Scheme == "https"));
                var ostream = await webRequest.GetRequestStreamAsync().ConfigureAwait(false);

                await request.CopyToAsync(ostream).ConfigureAwait(false);

                await ostream.FlushAsync().ConfigureAwait(false);

                ostream.Dispose();
                var webResponse = await webRequest.GetResponseAsync().ConfigureAwait(false) as HttpWebResponse;

                requests[webRequest] = webResponse;
                cookieManager.SetCookie(webResponse.Headers.GetValues("Set-Cookie"), uri.Host);
                cookieManager.SetCookie(webResponse.Headers.GetValues("Set-Cookie2"), uri.Host);
                var    istream         = webResponse.GetResponseStream();
                string contentEncoding = webResponse.ContentEncoding.ToLower();
                if (contentEncoding.IndexOf("deflate") > -1)
                {
                    istream = new DeflateStream(istream, CompressionMode.Decompress);
                }
                else if (contentEncoding.IndexOf("gzip") > -1)
                {
                    istream = new GZipStream(istream, CompressionMode.Decompress);
                }
                int len      = (int)webResponse.ContentLength;
                var response = (len > 0) ? new MemoryStream(len) : new MemoryStream();
                await istream.CopyToAsync(response).ConfigureAwait(false);

                istream.Dispose();
                webResponse.Close();
                return(response);
            }
            finally {
                requests.Remove(webRequest, out var _);
            }
        }
        private HttpWebRequest GetRequest()
        {
            Uri uri = new Uri(this.uri);

#if !WINDOWS_PHONE
            HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
#else
            HttpWebRequest request = WebRequestCreator.ClientHttp.Create(uri) as HttpWebRequest;
#endif
            request.Method      = "POST";
            request.ContentType = "application/hprose";
#if !(SILVERLIGHT || WINDOWS_PHONE || PORTABLE)
            request.Credentials = credentials;
#if !Core
#if !(PocketPC || Smartphone || WindowsCE || dotNETMF)
            request.ServicePoint.ConnectionLimit = Int32.MaxValue;
#endif
            request.Timeout     = timeout;
            request.SendChunked = false;
            if (encoding != null)
            {
                request.Headers.Set("Accept-Encoding", encoding);
            }
#if !(dotNET10 || dotNETCF10)
            request.ReadWriteTimeout = timeout;
#endif
            request.ProtocolVersion = HttpVersion.Version11;
            if (proxy != null)
            {
                request.Proxy = proxy;
            }
            request.KeepAlive = keepAlive;
            if (keepAlive)
            {
                request.Headers.Set("Keep-Alive", KeepAliveTimeout.ToString());
            }
#if !dotNETCF10
            request.ConnectionGroupName = connectionGroupName;
#if !(dotNET10 || dotNET11 || dotNETCF20 || UNITY_WEBPLAYER)
            if (clientCertificates != null)
            {
#if dotNETMF
                request.HttpsAuthentCerts = clientCertificates;
#else
                request.ClientCertificates = clientCertificates;
#endif
            }
#endif
#endif
#endif
#endif
#if (dotNET10 || dotNET11 || dotNETCF10)
            foreach (DictionaryEntry header in headers)
            {
                request.Headers[(string)header.Key] = (string)header.Value;
            }
#elif dotNETMF
            string[] allkeys = headers.AllKeys;
            foreach (string key in allkeys)
            {
                request.Headers.Add(key, headers[key]);
            }
#else
            foreach (KeyValuePair <string, string> header in headers)
            {
                request.Headers[header.Key] = header.Value;
            }
#endif
#if (PocketPC || Smartphone || WindowsCE || dotNETMF)
            request.AllowWriteStreamBuffering = true;
#if dotNETMF
            request.Headers.Add("Cookie", cookieManager.GetCookie(uri.Host,
                                                                  uri.AbsolutePath,
                                                                  uri.Scheme == "https"));
#else
            request.Headers["Cookie"] = cookieManager.GetCookie(uri.Host,
                                                                uri.AbsolutePath,
                                                                uri.Scheme == "https");
#endif
#elif !SILVERLIGHT
            request.CookieContainer = cookieContainer;
#endif
            return(request);
        }