代码积累-Common
2018-06-22 07:55:15来源:未知 阅读 ()
新建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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Asp.net MVC SignalR来做实时Web聊天实例代码 2020-03-29
- 为你介绍asp.net的执行事务代码 2020-03-18
- HTML标签删除HTML示例代码 2020-03-08
- MVC异步分页代码分享 2020-02-21
- asp.net网站实现接入QQ登录示例代码 2020-02-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash