示例#1
0
        public void JsonPostTest()
        {
            #region arrange
            DateTime dateTime    = new DateTime(2020, 8, 31, 13, 23, 10);
            long     timestamp   = 1598873142327;
            DateTime dateTime_ts = DateTimeOffset.FromUnixTimeMilliseconds(timestamp).DateTime.ToLocalTime();

            string alpha = "12,34";
            string beta  = "56,78";
            string body  = "[{\"datetime\":\"" + dateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ") + "\"," +
                           "\"datetime_ts\":\"" + dateTime_ts.ToString("yyyy-MM-ddTHH:mm:ss.fffZ") + "\"," +
                           "\"alpha\":" + alpha.Replace(",", ".") + "," +
                           "\"beta\":" + beta.Replace(",", ".") + "}]";

            string          endPoint        = @"https://postman-echo.com/post";
            object          id              = "1234";
            CallMethod      callMethod      = CallMethod.POST;
            CallContentType callContentType = CallContentType.json;
            #endregion

            #region act
            ICall                       call   = new HttpCall();
            IResponseParser             parser = new SimpleParser();
            Dictionary <string, object> actual = call.doCall(id, callMethod, callContentType, endPoint, parser, body);
            #endregion

            #region assert
            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.Count == 1);
            Assert.IsTrue(actual.ContainsKey("Response"));
            #endregion
        }
示例#2
0
        public static string getCallContentType(CallContentType type)
        {
            if (type == CallContentType.plain)
            {
                return("text/plain");
            }
            if (type == CallContentType.xml)
            {
                return("text/xml");
            }
            if (type == CallContentType.json)
            {
                return("application/json");
            }

            return(null);
        }
示例#3
0
        public Dictionary <string, object> doCall(
            object id,
            CallMethod _CallMethod,
            CallContentType _CallContentType,
            string url,
            IResponseParser parser,
            string body = null,
            Dictionary <string, object> parameters = null)
        {
            #region variables
            string endPoint = url;
            string payload  = body;
            #endregion
            #region try
            try
            {
                #region preconditions
                if (id == null)
                {
                    id = DateTime.Now.Ticks + "_" + Random.generate(10000, 99999);
                }
                if (string.IsNullOrEmpty(url))
                {
                    throw new Exception("Missing mandatory parameters");
                }
                if ((_CallMethod == CallMethod.POST) && (string.IsNullOrEmpty(body)))
                {
                    throw new Exception("Missing mandatory parameters");
                }
                #endregion

                #region endPoint / payload
                if ((_CallMethod == CallMethod.GET) && (url.Contains("?")))
                {
                    int index = url.IndexOf("?");
                    endPoint = url.Substring(0, index);
                    payload  = url.Substring(index + 1);
                }
                else if ((_CallMethod == CallMethod.GET) && (!string.IsNullOrEmpty(body)))
                {
                    url += "?" + body;
                    body = null;
                }
                #endregion

                #region parameters
                if (parameters != null)
                {
                    if (parameters.ContainsKey("ByPassCert"))
                    {
                        ServicePointManager.ServerCertificateValidationCallback = new
                                                                                  RemoteCertificateValidationCallback
                                                                                  (
                            delegate { return(true); }
                                                                                  );
                    }
                }
                #endregion

                #region request
                HttpWebRequest _WebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                _WebRequest.ServicePoint.ConnectionLimit = 1000;
                _WebRequest.Timeout     = 5000; _WebRequest.ReadWriteTimeout = 10000;
                _WebRequest.Method      = _CallMethod.ToString();
                _WebRequest.ContentType = (!string.IsNullOrEmpty(CallContentTypeUtility.getCallContentType(_CallContentType))) ?
                                          CallContentTypeUtility.getCallContentType(_CallContentType) :
                                          CallContentTypeUtility.getCallContentType(CallContentType.plain);
                _WebRequest.AllowAutoRedirect = false;

                if (_CallMethod == CallMethod.POST)
                {
                    byte[] data = Encoding.ASCII.GetBytes(body);
                    _WebRequest.ContentLength = data.Length;
                    Stream requestStream = _WebRequest.GetRequestStream();
                    requestStream.Write(data, 0, data.Length);
                    requestStream.Close();
                }
                #endregion

                #region response
                string          _response      = null;
                HttpWebResponse response       = (HttpWebResponse)_WebRequest.GetResponse();
                Stream          responseStream = response.GetResponseStream();
                using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default))
                    _response = myStreamReader.ReadToEnd();
                responseStream.Close();
                response.Close();
                #endregion

                return(parser.parse(_response));
            }
            #endregion
            #region catch
            catch (Exception ex)
            {
                string error = "Error in function " + this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name + " - " + ex.Message;
                System.Diagnostics.Trace.TraceError(error);
                return(new Dictionary <string, object>()
                {
                    { "Error", error },
                });
            }
            #endregion
        }