ABP框架中微服务跨域调用其它服务接口

AjaxResponse为ABP自动包装的JSON格式
    /// 
    /// 通过地址和参数取得返回OutPut数据
    /// 
    /// 
    /// 请求参数
    /// 
    public static OutPut ResultGet(string url, object input = null)
    {
        var data = Get(url, input);
        var response = data.JsonToObject();

        if (response.Success == false)
        {
            var str = data.JsonToObject();
            throw new Exception(str.Error.Message);
        }
        return response.Result.ToString().JsonToObject();
    }

    /// 
    /// 通过地址和参数取得返回OutPut数据
    /// 
    /// 
    /// 请求参数
    /// 
    public static OutPut ResultPost(string url, object input = null)
    {
        var data = Post(url, input);
        var response = data.JsonToObject();

        if (response.Success == false)
        {
            var str = data.JsonToObject();
            throw new Exception(str.Error.Message);
        }
        return response.Result.ToString().JsonToObject();
    }

    /// 
    ///通过地址和我反序列后的JSON取得返回GeneralOutPut数据
    /// 
    /// 请求地址
    /// 请求参数 
    /// 请求返回的结果
    private static string Get(string url, object input = null)
    {
        var properties = input?.GetType().GetProperties();
        var parm = string.Empty;
        foreach (var propertie in properties)
        {
            var name = propertie.Name;
            var value = input.GetType().GetProperty(name).GetValue(input);//直接根据属性的名字获取其值  

            parm += $"&{ name}={value}";
        }

        parm = parm.Trim('&');
        url = $"{url}?{parm}";

        // Prepare web request...  
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";
        request.ContentType = "application/json";

        return HttpRequest(request);
    }

    ///   
    /// 指定Post地址使用Get 方式获取全部字符串  
    ///   
    /// 请求后台地址  
    ///   
    public static string Post(string url, object input)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";

        #region 添加Post 参数  
        var parm = Serialization.JsonSerialize(input);
        byte[] data = Encoding.UTF8.GetBytes(parm);
        req.ContentLength = data.Length;
        using (Stream reqStream = req.GetRequestStream())
        {
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();
        }
        #endregion

        return HttpRequest(req);
    }

    /// 
    /// 请求的主体部分(由此完成请求并返回请求结果)
    /// 
    /// 请求的对象 
    /// 请求返回结果
    private static string HttpRequest(HttpWebRequest request)
    {
        HttpWebResponse response = null;

        try
        {
            response = (HttpWebResponse)request.GetResponse();
        }
        catch (WebException ex)
        {
            response = (HttpWebResponse)ex.Response;
        }

        string result = string.Empty;
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            result = reader.ReadToEnd();
        }

        return result;
    }

    /// 
    /// URL拼写完整性检查
    /// 
    /// 待检查的URL
    private static string UrlCheck(string url)
    {
        if (!url.StartsWith("http://") && !url.StartsWith("https://"))
            url = "http://" + url;

        return url;
    }

 

转载于:https://www.cnblogs.com/bluesky0122/p/9170515.html

你可能感兴趣的:(c#,json)