using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using ServiceStack.Text; namespace WeiXin.Core { public class TFSHelper { private const int TIME_OUT = 30000; public class ReponseInfo { public string TFS_FILE_NAME { get; set; } } /// <summary> /// 保存图片 /// </summary> /// <param name="url">请求的接口</param> /// <param name="data">字节数组形式的pic信息</param> /// <returns>图片文件名称</returns> public static string Save(string url, byte[] data) { string result = null; HttpWebRequest request = null; HttpWebResponse response = null; Stream writer = null; Stream reader = null; StreamReader sr = null; try { request = (HttpWebRequest)WebRequest.Create(url); request.Date = DateTime.Now; request.Timeout = TIME_OUT; request.Method = "POST"; using (writer = request.GetRequestStream()) { writer.Write(data, 0, data.Length); } } catch (Exception ex) { throw new Exception("TFS存文件发送请求失败,原因:" + ex.Message); } finally { if (writer != null) { writer.Close(); } } try { response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { using (reader = response.GetResponseStream()) { if (reader != null) sr = new StreamReader(reader, Encoding.GetEncoding("UTF-8")); if (sr != null) result = sr.ReadToEnd(); } } } catch (Exception ex) { throw new Exception("TFS存文件接收数据失败,原因:" + ex.Message); } finally { if (sr != null) { sr.Close(); } if (reader != null) { reader.Close(); } if (response != null) { response.Close(); } } if (!string.IsNullOrEmpty(result)) { result = result.FromJson<ReponseInfo>().TFS_FILE_NAME;//从响应的json字符串中获取到值 } return result; } /// <summary> /// 获取图片 /// </summary> /// <param name="url">请求的接口</param> /// <param name="imgName">图片名称</param> /// <returns>字节数组类型的pic信息</returns> public static byte[] Take(string url, string imgName) { HttpWebRequest request = null; HttpWebResponse response = null; Stream reader = null; url = url + "/" + imgName; try { request = (HttpWebRequest)WebRequest.Create(url); request.Date = DateTime.Now; request.Timeout = TIME_OUT; request.Method = "GET"; //using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) //{ // writer.Write(imgName); //} } catch (Exception ex) { throw new Exception("TFS取文件发送请求失败,原因:" + ex.Message); } try { response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { using (reader = response.GetResponseStream()) { MemoryStream ms = new MemoryStream(); reader.CopyTo(ms); return ms.ToArray(); } } } catch (Exception ex) { throw new Exception("TFS取文件接受数据失败,原因:" + ex.Message); } finally { if (reader != null) { reader.Close(); } if (response != null) { response.Close(); } } return null; } /// <summary> /// 删除tfs文件 /// </summary> /// <param name="url">请求的接口</param> /// <returns>删除是否成功</returns> public static bool Delete(string url) { bool result = false; HttpWebRequest request = null; HttpWebResponse response = null; try { request = WebRequest.Create(url) as HttpWebRequest; request.Date = DateTime.Now; request.Timeout = TIME_OUT; request.Method = "DELETE"; } catch (Exception ex) { throw new Exception("TFS删除文件发送请求失败,原因:" + ex.Message); } try { response = request.GetResponse() as HttpWebResponse; if (response.StatusCode == HttpStatusCode.OK) { result = true; } } catch (Exception ex) { throw new Exception("TFS删除文件接受数据失败,原因:" + ex.Message); } finally { if (response != null) { response.Close(); } } return result; } } }
时间: 2024-10-08 18:44:01