Store http response headers, body and status code
示例#1
0
        /// <summary>
        /// Processes the http response into a <see cref="pokitdokcsharp.ResponseData"/> object.
        /// </summary>
        /// <returns>The response as a <see cref="pokitdokcsharp.ResponseData"/> object.</returns>
        /// <param name="response">Response.</param>
        private ResponseData ProcessResponse(HttpWebResponse response)
        {
            if (!(_responseData is ResponseData))
            {
                _responseData = new ResponseData();
            }
            else
            {
                _responseData.init();
            }

            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                _responseData.body = (string)sr.ReadToEnd().Trim();
            }
            foreach (string key in response.Headers.AllKeys)
            {
                _responseData.header.Add(key, response.Headers[key]);
            }
            _responseData.status      = (int)response.StatusCode;
            _responseData.status_code = (int)response.StatusCode;

            return(_responseData);
        }
        /// <summary>
        /// Applies the response data to extract usage, data and error members.
        /// </summary>
        /// <returns>The http response as a <see cref="pokitdokcsharp.ResponseData"/> object.</returns>
        /// <param name="response">The a <see cref="pokitdokcsharp.ResponseData"/> object returned from raw API calls.</param>
        private ResponseData applyResponse(ResponseData response)
        {
            dynamic responseObject = null;

            try
            {
                responseObject = JsonConvert.DeserializeObject(response.body);
            }
            catch (Exception)
            {
                _usage = null;
                _data = null;
                _errors = "Error deserializing response body.";
                return response;
            }

            if (responseObject == null)
            {
                _usage = null;
                _data = null;
                _errors = null;
            } else {
                _usage = responseObject ["meta"];
                _data = responseObject ["data"];
                if (_data is Newtonsoft.Json.Linq.JObject) {
                    _errors = _data ["errors"];
                } else {
                    _errors = null;
                }
            }

            return response;
        }
        /// <summary>
        /// Processes the http response into a <see cref="pokitdokcsharp.ResponseData"/> object.
        /// </summary>
        /// <returns>The response as a <see cref="pokitdokcsharp.ResponseData"/> object.</returns>
        /// <param name="response">Response.</param>
        private ResponseData ProcessResponse(HttpWebResponse response)
        {
            if (!(_responseData is ResponseData))
            {
                _responseData = new ResponseData();
            }
            else
            {
                _responseData.init();
            }

            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                _responseData.body = (string) sr.ReadToEnd().Trim();
            }
            foreach (string key in response.Headers.AllKeys)
            {
                _responseData.header.Add(key, response.Headers[key]);
            }
            _responseData.status = (int) response.StatusCode;

            return _responseData;
        }
示例#4
0
 private static void print_response(ResponseData resp, string query_type = "response body")
 {
     if (resp.status == 200) // this is the HTTP status code
     {
         foreach (KeyValuePair<string, string> entry in resp.header)
         {
             Console.WriteLine("response header <" + entry.Key + "> = " + entry.Value);
         }
         Console.WriteLine(query_type + "= " + resp.body + "\n\n");
     }
 }