Пример #1
0
        public static byte[] httpPost(string url, string entity)
        {
            if (url == null || url.Length == 0)
            {
                return null;
            }

            IHttpClient httpClient = getNewHttpClient();

            HttpPost httpPost = new HttpPost(url);

            try
            {
                httpPost.Entity = new StringEntity(entity);
                httpPost.SetHeader("Accept", "application/json");
                httpPost.SetHeader("Content-type", "application/json");

                IHttpResponse resp = httpClient.Execute(httpPost);
                if (resp.StatusLine.StatusCode != Org.Apache.Http.HttpStatus.ScOk)
                {
                    return null;
                }

                return EntityUtils.ToByteArray(resp.Entity);
            }
            catch (Exception e)
            {
                return null;
            }
        }
Пример #2
0
        public static byte[] httpPost(String url, String entity)
        {
            if (url == null || url.Length == 0)
            {
                Log.Error(Tag, "httpPost, url is null");
                return null;
            }

            IHttpClient httpClient = getNewHttpClient();

            HttpPost httpPost = new HttpPost(url);

            try
            {
                httpPost.Entity = new StringEntity(entity);
                httpPost.SetHeader("Accept", "application/json");
                httpPost.SetHeader("Content-type", "application/json");

                IHttpResponse resp = httpClient.Execute(httpPost);
                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, "httpPost exception, e = " + e.Message);
                System.Console.WriteLine(e.StackTrace);
                return null;
            }
        }