Example #1
0
        /// <summary>
        /// 返回response
        /// </summary>
        /// <param name="domain"></param>
        /// <param name="actionName"></param>
        /// <param name="parameters"></param>
        /// <param name="header"></param>
        /// <returns></returns>
        public static IResultResponse GetResponse(string domain, string actionName, Dictionary <string, string> parameters, Dictionary <string, string> header = null)
        {
            IResultResponse result = null;

            try
            {
                string msg           = string.Empty;
                string reqParameters = GetSign(actionName, parameters, FormatType.Json);


                NetResponse response = null;

                NetRequest request = new NetRequest();
                request.SetTimeOut(10000);
                request.SetTryTimes(1);
                if (header != null)
                {
                    foreach (KeyValuePair <string, string> kv in header)
                    {
                        request.AddHeader(kv.Key, kv.Value);
                    }
                }

                if (!domain.StartsWith("http"))
                {
                    domain = "http://" + domain;
                }
                string url = domain + "/Route.axd";

                response = request.Post(url, reqParameters, string.Empty, out msg);

                if (response != null)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        result = ParseResponseData(response.Content);
                    }
                    else
                    {
                        result = ResultResponse.ExceptionResult(response.StatusDescription);
                    }
                }
                else if (!msg.IsNullOrEmpty())
                {
                    result = ResultResponse.ExceptionResult("数据接口请求错误:" + msg + "; 请求接口名: " + actionName);
                    new Exceptions("数据接口请求错误:" + msg + "; 请求接口名: " + actionName);
                }
                else
                {
                    result = ResultResponse.ExceptionResult("数据接口请求错误,请求接口名: " + actionName);
                }
            }
            catch (Exception ex)
            {
                result = ResultResponse.ExceptionResult(ex, ex.Message);
                new Exceptions(ex.Message, ex);
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// 获取远程内容
        /// </summary>
        /// <param name="path"></param>
        /// <param name="dic"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string GetRemoteContent(string url, string apiName, Dictionary <string, string> dic, out string msg)
        {
            try
            {
                NetResponse response = null;
                NetRequest  request  = new NetRequest();
                request.SetTryTimes(1);

                string data = GetSign(apiName, dic);
                response = request.Post(url, data, string.Empty, out msg);

                if (response != null)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        if (response.Header != null)
                        {
                            string contentType = response.Header.Get("ContentType");
                        }

                        return(response.Content);
                    }
                }

                return("{\"code\": " + response.StatusCode + ",\"msg\":\"" + msg + "\"}");
            }
            catch (Exception e)
            {
                msg = e.Message;
                return("");
            }
        }