IHttpRequestEncodingBuilder IHttpRequestContentTypeBuilder.WithContentType(WebRequestContentType contentType)
        {
            string contentTypeName;

            var isSucceed = _contentTypes.TryGetValue(contentType, out contentTypeName);

            if (!isSucceed)
                throw new Exception();

            _contentType = contentTypeName;

            return this;
        }
Beispiel #2
0
        /// <summary>
        /// Generates a DataSet given a string
        /// </summary>
        /// <param name="strResponse"></param>
        /// <returns></returns>
        public static DataSet GenerateDataSet(String strResponse, WebRequestContentType contentType = WebRequestContentType.JSON)
        {
            DataSet dsResponse = new DataSet();

            if (strResponse != "")
            {
                XmlDocument xmlDocument = new XmlDocument();

                switch (contentType)
                {
                case WebRequestContentType.XML:
                    xmlDocument.LoadXml(strResponse);
                    break;

                case WebRequestContentType.JSON:
                default:
                    if (strResponse.StartsWith("[") && strResponse.EndsWith("]"))
                    {
                        strResponse = $"{{\"root\":{strResponse}}}";
                    }
                    //Conversion to JObject is to prevent automatic DateTime columns because they break when the value is an empty string
                    JObject jResponse = JsonConvert.DeserializeObject <JObject>(strResponse, new JsonSerializerSettings {
                        DateParseHandling = DateParseHandling.None
                    });

                    //Handle Array with one value
                    if (jResponse.Count == 1 && jResponse["root"] is JArray && ((JArray)jResponse["root"]).Count == 1 && ((JArray)jResponse["root"])[0] is JValue)
                    {
                        DataTable dt = new DataTable("root");
                        dt.Columns.Add("root_Text");
                        dt.Rows.Add(new object[] { ((JArray)jResponse["root"])[0] });
                        dsResponse.Tables.Add(dt);
                        return(dsResponse);
                    }
                    else
                    {
                        xmlDocument = (XmlDocument)JsonConvert.DeserializeXmlNode(jResponse.ToString(), "root");
                    }
                    break;
                }

                dsResponse.ReadXmlTryFix(xmlDocument);
            }

            return(dsResponse);
        }
Beispiel #3
0
 /// <summary>
 /// Generates as DataSet given an HttpResponseMessage input
 /// </summary>
 /// <param name="httpResponse"></param>
 /// <returns></returns>
 public static DataSet GenerateDataSet(HttpResponseMessage httpResponse, WebRequestContentType contentType = WebRequestContentType.JSON)
 {
     return(GenerateDataSet(httpResponse.Content.ReadAsStringAsync().Result, contentType));
 }
 public WebRequestHelper(IServiceItemResult result, Uri url, NetworkSetting networkSetting, WebRequestContentType contentType, bool gzipRequest)
 {
     this.result = result;
     this.url = url;
     this.networkSetting = networkSetting;
     this.contentType = contentType;
     this.gZipRequest = gzipRequest;
 }
 public WebRequestHelper(IServiceItemResult result, Uri url, NetworkSetting networkSetting, WebRequestContentType contentType, Encoding encoding)
 {
     this.result = result;
     this.url = url;
     this.networkSetting = networkSetting;
     this.contentType = contentType;
     this.encoding = encoding;
 }
 public WebRequestHelper(IServiceItemResult result, Uri url, NetworkSetting networkSetting, WebRequestContentType contentType, bool gzipRequest)
 {
     this.result         = result;
     this.url            = url;
     this.networkSetting = networkSetting;
     this.contentType    = contentType;
     this.gZipRequest    = gzipRequest;
 }
 public WebRequestHelper(IServiceItemResult result, Uri url, NetworkSetting networkSetting, WebRequestContentType contentType, Encoding encoding)
 {
     this.result         = result;
     this.url            = url;
     this.networkSetting = networkSetting;
     this.contentType    = contentType;
     this.encoding       = encoding;
 }