Beispiel #1
0
        //public static byte[] bmpToByteArray(Bitmap bmp, bool needRecycle)
        //{
        //    var output = new MemoryStream();
        //    bmp.Compress(Bitmap.CompressFormat.Png, 100, output);
        //    if (needRecycle)
        //    {
        //        bmp.Recycle();
        //    }

        //    byte[] result = output.ToArray();
        //    try
        //    {
        //        output.Close();
        //    }
        //    catch (Exception e)
        //    {

        //    }

        //    return result;
        //}


        public static byte[] httpGet(string url)
        {
            if (url == null || url.Length == 0)
            {
                Log.Error(Tag, "httpGet, url is null");
                return null;
            }

            IHttpClient httpClient = getNewHttpClient();
            HttpGet httpGet = new HttpGet(url);

            try
            {
                IHttpResponse resp = httpClient.Execute(httpGet);
                if (resp.StatusLine.StatusCode != Org.Apache.Http.HttpStatus.ScOk)
                {
                    Log.Error(Tag, "httpGet fail, status code = " + resp.StatusLine.StatusCode);
                    return null;
                }

                return EntityUtils.ToByteArray(resp.Entity);
            }
            catch (Exception e)
            {
                Log.Error(Tag, "httpGet exception, e = " + e.Message);
                System.Console.WriteLine(e.StackTrace);
                return null;
            }
        }
 /// <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;
     }
 }