现在互联网这么发达,我们做的项目也是需要从网络上获取信息(即下载数据),或者将本地的数据上传到服务器上(即上传数据)。所以,对网络的操作也很重要和基础,今天分享的教程,就是关于网络编程(network)的,就是https://这个串。
1、获取Url地址的前缀
这个方法主要用于判断主机?比如是服务器调用,还是本机localhost测试?
string urlPath = "https://webapi.navisoft.com.cn/api/getUser?userCode=vincent.q"; string urlPrefix = UrlHelper.GetUrlPrefix(urlPath); //结果:https://webapi.navisoft.com.cn/api/getUser
2、获取Url的参数部分
就是获取?后面的参数,根据参数值执行不同的业务逻辑
string urlPath1 = "https://webapi.navisoft.com.cn/api/getUser?userCode=vincent.q"; Dictionary<string, string> dicP1 = UrlHelper.GetUrlParam(urlPath1); string json1 = JsonHelper.GetJsonByObject(dicP1); //结果:{"userCode":"vincent.q"}
3、组装Url的参数部分
不需要我们再StringBuilder的拼接了,直接使用键值对即可
Dictionary<string, string> dicP2 = new Dictionary<string, string>(); dicP2.Add("code", "1001"); dicP2.Add("name", "小Q"); dicP2.Add("sex", "male"); dicP2.Add("year", "40"); string urlP2 = UrlHelper.ComboUrlParam(dicP2); //结果:code=1001&name=小Q&sex=male&year=40
4、获取分页页数
获取的数据量很大时,需要用到分页来优化性能。这个方法用来确定分页?即根据每页多少数据确定需要分多少页
int pageSize = 1000; long totalCount = 12345; List<long> listPage = UrlHelper.GetPageParam(totalCount, pageSize); //结果:共有13页
5、Encode和Decode加密和解密
第三方的开放平台,一般都需要这样。尤其是中文参数较多的时候
string urlP21 = UrlHelper.UrlEncode(urlP2); //结果:code%3d1001%26name%3d%e5%b0%8fQ%26sex%3dmale%26year%3d40 string urlP22 = UrlHelper.UrlDecode(urlP21); //结果:code=1001&name=小Q&sex=male&year=40
6、下载方法
要看下载的是什么类型的数据?比如是二进制、字符串、还是图片
string byteUrl = null; byte[] bytes1 = UrlHelper.DownloadData(byteUrl); string imageUrl = null; System.Drawing.Image image = UrlHelper.DownloadImage(imageUrl); string fileUrl = null; string localFile = null; UrlHelper.DownloadFile(fileUrl, localFile);
7、任意的上传下载方法
这2个方法之前也分享过,这里再分享一次。不同之处是增加了Post流数据的方法
/// <summary> /// 提交GET方法至指定的Url,解析返回的数据 /// </summary> /// <param name="url"></param> /// <param name="charset"></param> /// <returns></returns> [ShortDescription("根据Get方式请求URL下载数据")] public static string WebMethodGetData(string url, string charset = "utf-8") { WebResponse response = null; Stream receiveStream = null; try { WebClientEx wc = new WebClientEx(); byte[] byteResponse = wc.DownloadData(url); wc.Dispose(); //及时释放、避免第二次下载时、奇怪的"操作超时"的问题 //字符集为UTF8 string responseStr = Encoding.GetEncoding(charset).GetString(byteResponse); return responseStr; } catch (WebException ex) { string errorMessage = ex.Message; //需判断response是否==null response = ex.Response; if (response == null) { dynamic objErrorData = new { message = errorMessage, subErrors = "" }; string jsonErrorData = JsonHelper.GetJsonByObject(objErrorData); //throw new CustomApiJsonException(jsonErrorData, url, ex); throw ex; } else { receiveStream = response.GetResponseStream(); Encoding encode = Encoding.GetEncoding(charset); StreamReader sr = new StreamReader(receiveStream, encode); char[] readbuffer = new char[2048]; int n = sr.Read(readbuffer, 0, 2048); string result = string.Empty; while (n > 0) { string str = new string(readbuffer, 0, n); result += str; n = sr.Read(readbuffer, 0, 2048); } response.Close(); //此处将错误信息和json信息同时返回 dynamic objErrorData = new { message = errorMessage, subErrors = result }; string jsonErrorData = JsonHelper.GetJsonByObject(objErrorData); //throw new CustomApiJsonException(jsonErrorData, url, ex); throw ex; } } finally { //使用完后、释放资源 if (response != null) response.Close(); if (receiveStream != null) receiveStream.Close(); } } /// <summary> /// 提交POST方法至指定的Url,解析返回的数据 /// 参考地址:http://www.cnblogs.com/teikey/p/3373882.html /// </summary> /// <param name="url"></param> /// <param name="requestParam"></param> /// <param name="charset"></param> /// <param name="contentType"></param> /// <param name="dicHeader"></param> /// <returns></returns> [ShortDescription("根据Post方式请求URL下载数据")] public static string WebMethodPostData(string url, string requestParam, string charset = "utf-8", string contentType = "application/x-www-form-urlencoded",Dictionary<string,string>dicHeader=null) { WebRequest hr = null; WebResponse response = null; Stream receiveStreamIn = null; Stream receiveStreamOut = null; StreamReader srIn = null; StreamReader srOut = null; try { //创建访问对象 hr = HttpWebRequest.Create(url); hr.Timeout = requestTimeOut; //拼接参数 byte[] buf = System.Text.Encoding.GetEncoding(charset).GetBytes(requestParam); //hr.ContentType = "application/x-www-form-urlencoded"; hr.ContentType = contentType; hr.ContentLength = buf.Length; hr.Method = "POST"; if (dicHeader != null) { foreach (string sKey in dicHeader.Keys) { string sValue = dicHeader[sKey]; hr.Headers.Add(sKey, sValue); } } System.IO.Stream RequestStream = hr.GetRequestStream(); RequestStream.Write(buf, 0, buf.Length); RequestStream.Close(); response = hr.GetResponse(); receiveStreamIn = response.GetResponseStream(); Encoding encode = Encoding.GetEncoding(charset); srIn = new StreamReader(receiveStreamIn, encode); char[] readbuffer = new char[2048]; int n = srIn.Read(readbuffer, 0, 2048); string result = string.Empty; while (n > 0) { string str = new string(readbuffer, 0, n); result += str; n = srIn.Read(readbuffer, 0, 2048); } response.Close(); //注:此处用完以后立即关闭 //如果未取得Json数据,则抛出异常 if (string.IsNullOrEmpty(result)) { //throw new CustomApiException("消息长度:" + response.ContentLength + "消息内容:" + response.ToString(), url); List<string> listEx = new List<string>(); listEx.Add("消息长度:" + response.ContentLength); listEx.Add("消息内容:" + response.ToString()); listEx.Add("Url地址:" + url); string exs = string.Join(Environment.NewLine, listEx); throw new Exception(exs); } return result; } catch (WebException ex) { string errorMessage = ex.Message; //需判断response是否==null response = ex.Response; if (response == null) { dynamic objErrorData = new { message = errorMessage, subErrors = "" }; string jsonErrorData = JsonHelper.GetJsonByObject(objErrorData); //throw new CustomApiJsonException(jsonErrorData, url + "?" + requestParam, ex); throw ex; } else { receiveStreamOut = response.GetResponseStream(); Encoding encode = Encoding.GetEncoding(charset); srOut = new StreamReader(receiveStreamOut, encode); char[] readbuffer = new char[2048]; int n = srOut.Read(readbuffer, 0, 2048); string result = string.Empty; while (n > 0) { string str = new string(readbuffer, 0, n); result += str; n = srOut.Read(readbuffer, 0, 2048); } response.Close(); //此处将错误信息和json信息同时返回 dynamic objErrorData = new { message = errorMessage, subErrors = result }; string jsonErrorData = JsonHelper.GetJsonByObject(objErrorData); //throw new CustomApiJsonException(jsonErrorData, url + "?" + requestParam, ex); throw ex; } } finally { //http://www.cnblogs.com/huangtailang/archive/2013/07/09/3180651.html if (hr != null) hr.Abort();//中止请求 hr = null; //需要写下Null if (response != null) response.Close(); if (receiveStreamIn != null) receiveStreamIn.Close(); if (receiveStreamOut != null) receiveStreamOut.Close(); if (srIn != null) srIn.Close(); if (srOut != null) srOut.Close(); System.GC.Collect();//强制垃圾回收,并释放资源 } } /// <summary> /// 生成图片流的字符串参数值(目前用于速卖通图片上传) /// </summary> /// <param name="url"></param> /// <param name="stream"></param> /// <param name="contentType"></param> /// <returns></returns> [ShortDescription("根据Post方式请求URL下载数据")] public static string WebMethodPostData(string url, Stream stream, string contentType) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = requestTimeOut; try { request.Method = "POST"; request.Accept = "*/*"; request.KeepAlive = true; request.ContentType = contentType; request.Credentials = System.Net.CredentialCache.DefaultCredentials; request.ContentLength = stream.Length; using (Stream requestStream = request.GetRequestStream()) { //将内存流数据读取位置归零 stream.Position = 0; byte[] tempBuffer = new byte[stream.Length]; stream.Read(tempBuffer, 0, tempBuffer.Length); stream.Close(); //将内存流中的buffer写入到请求写入流 requestStream.Write(tempBuffer, 0, tempBuffer.Length); requestStream.Close(); //获取到上传请求的响应 WebResponse response = request.GetResponse(); //获取到上传返回的数据 StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8); string content = reader.ReadToEnd(); response.Close(); request = null; return content; } } finally { if (request != null) request.Abort();//中止请求 request = null; //需要写下Null System.GC.Collect();//强制垃圾回收,并释放资源 } }
关于数据采集的常用操作,这里先抛个引子,强烈推荐HtmlAgilityPack这个组件,非常好用。各种XPath操作都支持,只要看得到的数据,都能采集到。下次我们详细讲下数据采集这块
相关阅读
文章评论