Example #1
0
        public async System.Threading.Tasks.Task <RawResponse> GetResponse(JObject source)
        {
            string        fullUrl     = BaseApiUrl.TrimEnd(new char[] { '/', ' ' });
            StringBuilder bodyBuilder = new StringBuilder();
            JToken        requestJson = source.SelectToken("$.body");

            if (requestJson != null)
            {
                #region 准备调用所需要的各类输入参数
                if (!string.IsNullOrWhiteSpace(RouteDataTemplate))
                {
                    string[] routeParameters = RouteDataTemplate.Trim().Split(
                        '/', StringSplitOptions.RemoveEmptyEntries);
                    foreach (string route in routeParameters)
                    {
                        fullUrl = fullUrl + "/" + System.Web.HttpUtility.UrlEncode(
                            requestJson.SelectToken("$." + EsbPostBodySections.ROUTE +
                                                    "." + route).Value <string>());
                    }
                }
                if (!string.IsNullOrWhiteSpace(QueryStringTemplate))
                {
                    string[] queryParameters = QueryStringTemplate.Trim()
                                               .TrimStart('?').Split('&', StringSplitOptions.RemoveEmptyEntries);
                    fullUrl = fullUrl + "?";
                    for (int i = 0; i < queryParameters.Length; i++)
                    {
                        string query = queryParameters[i].Trim().TrimEnd('=');
                        fullUrl = fullUrl + query + "=" + System.Web.HttpUtility.UrlEncode(
                            requestJson.SelectToken("$." + EsbPostBodySections.QUERY +
                                                    "." + query).Value <string>());
                    }
                    fullUrl.TrimEnd('&');
                }
                if (JsonBodyTemplate != null)
                {
                    //向api提交请求的RequestBody中JSON优先
                    using (JsonWriter writer = new JsonTextWriter(new StringWriter(bodyBuilder)))
                    {
                        writer.Formatting = Formatting.Indented;
                        using (JTokenReader jtReader = new JTokenReader(requestJson.SelectToken("$." + EsbPostBodySections.JSON)))
                        {
                            writer.WriteToken(jtReader);
                        }
                        writer.Close();
                    }
                }
                else if (!string.IsNullOrWhiteSpace(FormBodyTemplate))
                {
                    //如果没有JSON但是有FORM
                    string[] formParameters = FormBodyTemplate.Trim()
                                              .TrimStart('?').Split('&', StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < formParameters.Length; i++)
                    {
                        string parameter = formParameters[i].Trim().TrimEnd('=');
                        bodyBuilder.Append(parameter);
                        bodyBuilder.Append('=');
                        bodyBuilder.Append(System.Web.HttpUtility.UrlEncode(
                                               requestJson.SelectToken("$." + EsbPostBodySections.FORM +
                                                                       "." + parameter).Value <string>()));
                        bodyBuilder.Append('&');
                    }
                    if (bodyBuilder.Length > 0)
                    {
                        bodyBuilder.Remove(bodyBuilder.Length - 1, 1);
                    }
                }
                #endregion
            }

            #region 准备请求
            WebRequest apiRequest;
            if (fullUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                apiRequest = HttpWebRequest.CreateHttp(fullUrl);
                ((HttpWebRequest)apiRequest).ServerCertificateValidationCallback
                    = (s, c, e, t) => true;
            }
            else
            {
                apiRequest = WebRequest.Create(fullUrl);
            }
            apiRequest.Method = Method.ToString();
            foreach (var header in Headers)
            {
                apiRequest.Headers.Add(header.Key, header.Value);
            }
            byte[] buffer = Encoding.UTF8.GetBytes(bodyBuilder.ToString());
            apiRequest.ContentLength = buffer.Length;
            using (Stream apiStream = apiRequest.GetRequestStream())
            {
                apiStream.Write(buffer, 0, buffer.Length);
                apiStream.Close();
            }
            #endregion

            using (WebResponse webResponse = await apiRequest.GetResponseAsync())
            {
                return(new RawResponse(webResponse, fullUrl));
            }
        }
Example #2
0
        public string GetPostSample()
        {
            StringBuilder bodyBuilder = new StringBuilder();

            using (JsonWriter writer = new JsonTextWriter(new StringWriter(bodyBuilder)))
            {
                writer.Formatting = Formatting.Indented;
                writer.WriteStartObject();
                if (!string.IsNullOrWhiteSpace(RouteDataTemplate))
                {
                    StringHelper.WriteRouteQueryFormJson(writer, EsbPostBodySections.ROUTE, RouteDataTemplate.Trim(), '/', "路由值");
                }
                if (!string.IsNullOrWhiteSpace(QueryStringTemplate))
                {
                    StringHelper.WriteRouteQueryFormJson(writer, EsbPostBodySections.QUERY, QueryStringTemplate.Trim().TrimStart('?'), '&', "查询字符串值");
                }
                if (JsonBodyTemplate != null)
                {
                    //向api提交请求的RequestBody中JSON优先
                    writer.WritePropertyName(EsbPostBodySections.JSON);
                    JsonBodyTemplate.WriteJsonBody(writer);
                }
                else if (!string.IsNullOrWhiteSpace(FormBodyTemplate))
                {
                    //如果没有JSON但是有FORM
                    StringHelper.WriteRouteQueryFormJson(writer, EsbPostBodySections.FORM, FormBodyTemplate.Trim().TrimStart('?'), '&', "表单数据值");
                }
                writer.WriteEndObject();
                writer.Flush();
            }
            return(bodyBuilder.ToString());
        }