コード例 #1
0
        public void request_with_body()
        {
            var frame = new HttpRequestBase("POST", "/?abc", "HTTP/1.1");

            frame.AddHeader("server", "Griffin.Networking");
            frame.AddHeader("X-Requested-With", "XHttpRequest");
            frame.ContentType = "text/plain";
            frame.Body        = new MemoryStream(Encoding.ASCII.GetBytes("hello queue a"));
            var expected = "POST /?abc HTTP/1.1\r\nserver: Griffin.Networking\r\nX-Requested-With: XHttpRequest\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\nhello queue a";
            var buffer   = new SocketBufferFake();

            var encoder = new HttpMessageEncoder();

            encoder.Prepare(frame);
            encoder.Send(buffer);
            var actual = Encoding.ASCII.GetString(buffer.Buffer, 0, buffer.Count);

            actual.Should().Be(expected);
        }
コード例 #2
0
        /*
         * /// <summary>
         * ///  Gets a Stream object to use to write request data and outputs the context.
         * /// </summary>
         * public Stream GetRequestStream(out TransportContext context);
         */

        /// <summary>
        /// Returns a response from an Internet resource.
        /// </summary>
        /// <remarks>
        /// You must close the returned response to avoid socket and memory leaks.
        /// </remarks>
        public override WebResponse GetResponse()
        {
#if ANDROID_8P
            var method = this.Method.ToLower();
            switch (method)
            {
            case "get":
                _request = new HttpGet();
                break;

            case "post":
                _request = new HttpPost();
                break;

            case "put":
                _request = new HttpPut();
                break;

            case "delete":
                _request = new HttpDelete();
                break;

            default:     //the switch below is to prevent a C# compiler bug with 7 case statements.
                switch (method)
                {
                case "options":
                    _request = new HttpOptions();
                    break;

                case "head":
                    _request = new HttpHead();
                    break;

                case "trace":
                    _request = new HttpTrace();
                    break;

                default:
                    throw new NotSupportedException(string.Format("Unsupported Method: {0}", this.Method));
                }
                break;
            }

            _request.SetURI(_requestUri);

            if (ContentLength != -1)
            {
                var entityRequest = _request as HttpEntityEnclosingRequestBase;
                if (entityRequest == null)
                {
                    throw new NotSupportedException(string.Format("Method: {0} does not support a request stream",
                                                                  this.Method));
                }
                if (_requestStream.Length < ContentLength)
                {
                    throw new ArgumentException(
                              string.Format("ContentLength={0}, however the request stream contains only {1} bytes",
                                            ContentLength, _requestStream.Length));
                }

                _requestStream.Seek(0L, SeekOrigin.Begin);
                entityRequest.Entity = new InputStreamEntity(_requestStream, ContentLength);
            }

            if (Headers != null)
            {
                var count = Headers.Count;
                for (var i = 0; i < count; i++)
                {
                    var key = Headers.GetKey(i);
                    if (key != "content-length") //The content length is set above.
                    {
                        _request.AddHeader(key, Headers.Get(i));
                    }
                }
            }

            AndroidHttpClient client = null;
            try
            {
                client = AndroidHttpClient.NewInstance(UserAgent);
                HttpClientParams.SetRedirecting(client.Params, this.AllowAutoRedirect);
                var response = client.Execute(_request);

                if (response == null)
                {
                    throw new Exception("Response is null");
                }

                var header = response.GetFirstHeader(WebHeaderCollection.HeaderToString(HttpResponseHeader.Location));
                var uri    = header != null ? header.Value : null;
                _address = new Uri(!string.IsNullOrEmpty(uri) ? uri : _requestUri.ToString());

                var result = new HttpWebResponse(this, response, client);
                client = null; // So we won't release it, HttpWebResponse should now release it.
                return(result);
            }
            finally
            {
                if (client != null)
                {
                    client.Close();
                }
            }
#else
            throw new NotSupportedException("The HttpWebRequest is not supported on Android v2.1, but available as of v2.2");
#endif
        }