代码积累-Common

2018-06-22 07:55:15来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

新建Common类库

/// <summary>
/// string的扩展
/// </summary>
public static class StringExt
{
    /// <summary>
    /// 格式清理 转义"\" 去掉{}<>#$等特殊字符
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string FormatClear(this string text)
    {
        text = text.Replace("\n\t\t\t\t\t", "")
            .Replace("\n\r", "\\n\\r")
            .Replace("\r\n", "\\r\\n")
            .Replace("\r", "\\r")
            .Replace("\t", "")
            .Replace("\n", "\\n")
            .Replace("{", "")
            .Replace("}", "")
            .Replace("<", "")
            .Replace(">", "")
            .Replace(",", "")
            .Replace(":", "")
            .Replace("\"", "\\\"")
            .Replace("#", "")
            .Replace("%", "")
            .Replace("$", "")
            .Replace("^", "")
            .Replace("*", "");
        return text;
    }
}
/// <summary>
/// web请求扩展
/// </summary>
public static class WebRequestExt
{


    /// <summary>
    /// Get方式发起请求 返回处理结果 (HttpWebRequest提交Get信息)
    /// </summary>
    /// <param name="url">请求地址</param>
    /// <returns></returns>
    public static string GetData(string url)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";
        request.ContentType = "text/html;charset=UTF-8";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
        string retstring = myStreamReader.ReadToEnd();
        myStreamReader.Close();
        myResponseStream.Close();
        return retstring;
    }


    /// <summary>
    /// Post方式带参数发起请求 返回处理结果 (HttpWebRequest提交POST信息)
    /// </summary>
    /// <param name="jsonData">请求的参数-json格式</param>
    /// <param name="url">请求的地址</param>
    /// <returns></returns>
    public static string PostData(string jsonData, string url)
    {
        //将提交的字符串数据转换成字节数组
        byte[] postData = Encoding.UTF8.GetBytes(jsonData);
        //设置提交的相关参数
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        //提交方式
        request.Method = "POST";
        //编码格式
        request.ContentType = "text/html;charset=UTF-8";
        request.ContentLength = postData.Length;
        // 提交请求数据
        Stream outputStream = request.GetRequestStream();
        outputStream.Write(postData, 0, postData.Length);
        outputStream.Close();
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream, Encoding.Default);
        string srcString = reader.ReadToEnd();
        reader.Close();
        responseStream.Close();
        outputStream.Close();
        return srcString;
    }
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Highsoft.Highcharts 5.0.6439.38401 key

下一篇:.NET开源Protobuf-net组件修炼手册