示例#1
0
        /// <summary>
        /// Makes a POST request and creates a resource for the given URL and a request body.
        /// </summary>
        /// <param name="url">Service URL passed by the user.</param>
        /// <param name="requestBody">It contains the request body parameters to make the POST request.</param>
        /// <returns>HttpResponseMessage which contains the data in the form of JSON .</returns>
        /// <exception cref="BooksException">Throws the Exception with error messege return from the server.</exception>
        public static HttpResponseMessage post(string url, Dictionary <object, object> requestBody)
        {
            Console.WriteLine("ZohoHttpClient.post:{0}", url);
            var client = getClient();
            List <KeyValuePair <string, string> > contentBody = new List <KeyValuePair <string, string> >();

            foreach (var requestbodyParam in requestBody)
            {
                Console.WriteLine("{0} => {1}", requestbodyParam.Key.ToString(), requestbodyParam.Value.ToString());
                var temp = new KeyValuePair <string, string>(requestbodyParam.Key.ToString(), requestbodyParam.Value.ToString());
                contentBody.Add(temp);
            }
            var content = new ZohoFormUrlEncodedContent(contentBody);

            try
            {
                var responce = client.PostAsync(url, content).Result;
                if (responce.IsSuccessStatusCode)
                {
                    return(responce);
                }
                else
                {
                    throw new BooksException(ErrorParser.getErrorMessage(responce));
                }
            }
            catch (HttpRequestException ex)
            {
                throw new BooksException(String.Format("Error posting data to {0}", url), ex);
            }
        }
示例#2
0
        private static byte[] GetContentByteArray(IEnumerable <KeyValuePair <string, string> > nameValueCollection)
        {
            if (nameValueCollection == null)
            {
                throw new ArgumentNullException("nameValueCollection");
            }
            StringBuilder stringBuilder = new StringBuilder();

            foreach (KeyValuePair <string, string> current in nameValueCollection)
            {
                if (stringBuilder.Length > 0)
                {
                    stringBuilder.Append('&');
                }

                stringBuilder.Append(ZohoFormUrlEncodedContent.Encode(current.Key));
                stringBuilder.Append('=');
                stringBuilder.Append(ZohoFormUrlEncodedContent.Encode(current.Value));
            }
            return(Encoding.Default.GetBytes(stringBuilder.ToString()));
        }
示例#3
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="nameValueCollection"></param>
 public ZohoFormUrlEncodedContent(IEnumerable <KeyValuePair <string, string> > nameValueCollection)
     : base(ZohoFormUrlEncodedContent.GetContentByteArray(nameValueCollection))
 {
     base.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
 }