internal HttpWebResponse(HttpWebRequest webRequest, IHttpResponse response #if ANDROID_8P , AndroidHttpClient client #endif ) { _webRequest = webRequest; _response = response; #if ANDROID_8P this.client = client; #endif _entity = _response.GetEntity(); ThrowExceptionIfProtocolError(); }
/// <summary> /// Closes the response stream. /// </summary> public override void Close() { if (_responseStream != null) { _responseStream.Close(); _responseStream = null; } #if ANDROID_8P if (client != null) { client.Close(); client = null; } #endif }
/// <summary> /// Perform the low level request and return the resulting content. /// </summary> private static string PerformRequest(string iataCode) { var uri = string.Format("http://services.faa.gov/airport/status/{0}?format=application/json", iataCode); var client = AndroidHttpClient.NewInstance("AirportInfo"); var request = new HttpGet(uri); try { var response = client.Execute(request); var content = response.GetEntity().GetContent(); var reader = new BufferedReader(new InputStreamReader(content)); var builder = new StringBuilder(); string line; while ((line = reader.ReadLine()) != null) { builder.Append(line); } return(builder.ToString()); } catch (Exception ex) { return(null); } }
/* * /// <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 }