예제 #1
0
        public string CallService(string body, string url, string requestType, string contentType)
        {
            var visurl = _visUrl + string.Format("/callService");

            var result  = new CallServiceResponseObj();
            var request = new CallServiceRequestObj
            {
                Body           = body,
                EndpointMethod = url,
                Headers        = new List <Header>
                {
                    new Header("content-type", contentType)
                },
                RequestType = requestType
            };

            WebHeaderCollection headers = new WebHeaderCollection();

            headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8");

            var json     = request.ToJson();
            var response = WebRequestHelper.Post(visurl, request.ToJson(), headers);

            return(response.Body);
        }
        public virtual CallServiceResponseObj CallService(CallServiceRequestObj data)
        {
            var result = new CallServiceResponseObj();

            var url = data.EndpointMethod;

            log.Info(string.Format("Sending REST request to service: {0} {1}", data.RequestType, url));

            string headers          = string.Empty;
            var    headerCollection = new WebHeaderCollection();

            if (data.Headers != null)
            {
                foreach (var h in data.Headers)
                {
                    headers += h + " ";
                    headerCollection.Add(h.Key, h.Value);
                }
            }

            log.Info(string.Format("- using headers: {0}", headers));

            WebRequestHelper.WebResponse response = null;

            if (data.RequestType == "GET")
            {
                response = WebRequestHelper.Get(url, headerCollection, true);
            }
            else if (data.RequestType == "POST")
            {
                response = WebRequestHelper.Post(url, data.Body, headerCollection, true);
            }
            else if (data.RequestType == "DELETE")
            {
                response = WebRequestHelper.Delete(url, data.Body, headerCollection, true);
            }
            else if (data.RequestType == "PUT")
            {
                response = WebRequestHelper.Put(url, data.Body, headerCollection, true);
            }
            else
            {
                throw new Exception(string.Format("The request type {0} is not supported.", data.RequestType));
            }

            result.Body       = response.Body;
            result.StatusCode = (int)response.HttpStatusCode;
            return(result);
        }