From c199d0ddbdccb229e122c11d7d93fe35d4fff08d Mon Sep 17 00:00:00 2001 From: j502647092 Date: Thu, 13 Aug 2015 16:14:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=8A=A9=E6=89=8B=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E4=B8=8A=E4=BC=A0...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WeixinHelper/App.config | 6 + WeixinHelper/Properties/AssemblyInfo.cs | 36 ++++++ WeixinHelper/UploadHelper.cs | 155 ++++++++++++++++++++++++ WeixinHelper/WeixinHelper.csproj | 60 +++++++++ 4 files changed, 257 insertions(+) create mode 100644 WeixinHelper/App.config create mode 100644 WeixinHelper/Properties/AssemblyInfo.cs create mode 100644 WeixinHelper/UploadHelper.cs create mode 100644 WeixinHelper/WeixinHelper.csproj diff --git a/WeixinHelper/App.config b/WeixinHelper/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/WeixinHelper/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/WeixinHelper/Properties/AssemblyInfo.cs b/WeixinHelper/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..bf1bedb --- /dev/null +++ b/WeixinHelper/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("WXUpload")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("WXUpload")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +//将 ComVisible 设置为 false 将使此程序集中的类型 +//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("5bd99169-4eb4-4f4e-a22b-da7e052d9e37")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/WeixinHelper/UploadHelper.cs b/WeixinHelper/UploadHelper.cs new file mode 100644 index 0000000..78968f7 --- /dev/null +++ b/WeixinHelper/UploadHelper.cs @@ -0,0 +1,155 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Mime; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace WeixinHelper +{ + class UploadHelper + { + /// + /// 上传微信永久图片 + /// + /// Token + /// 文件名称 + /// 文件字节流 + /// TX返回的Json数据 + public static string UploadImage(string access_token, string filename, byte[] filebytes) + { + string CRLF = "\r\n"; + string BOUNDARY = "----" + DateTime.Now.Ticks.ToString("x"); + try + { + string url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + access_token; + + StringBuilder sb = new StringBuilder(); + sb.Append("--"); + sb.Append(BOUNDARY); + sb.Append(CRLF); + sb.Append("Content-Disposition: form-data; name=\"media\"; filename=\"3.png\""); + sb.Append(CRLF); + sb.Append("Content-Type: image/png"); + sb.Append(CRLF); + sb.Append(CRLF); + + string header = sb.ToString(); + string foot = CRLF + "--" + BOUNDARY + "--" + CRLF; + + Console.WriteLine(header); + Console.WriteLine(foot); + + byte[] headerbytes = Encoding.UTF8.GetBytes(header); + byte[] footbytes = Encoding.UTF8.GetBytes(foot); + + List datalist = new List(); + + datalist.AddRange(headerbytes); + datalist.AddRange(filebytes); + datalist.AddRange(footbytes); + + byte[] data = datalist.ToArray(); + + return GetHtmlFromPost(url, data, BOUNDARY); + } + catch (Exception) + { + return string.Empty; + } + } + + /// + /// 获取玩家数据流 + /// + /// 文件路径 + /// 文件数据流 + public byte[] File2Bytes(string path) + { + if (!File.Exists(path)) + { + return new byte[0]; + } + + FileInfo fi = new FileInfo(path); + byte[] buff = new byte[fi.Length]; + + FileStream fs = fi.OpenRead(); + fs.Read(buff, 0, Convert.ToInt32(fs.Length)); + fs.Close(); + + return buff; + } + + /// + /// 提供通过POST方法获取页面的方法 + /// + /// 请求的URL + /// POST数据 + /// BOUNDARY + /// + public static string GetHtmlFromPost(string urlString, byte[] postDatabytes, string BOUNDARY) + { + //定义局部变量 + HttpWebRequest httpWebRequest = null; + HttpWebResponse httpWebResponse = null; + Stream inputStream = null; + Stream outputStream = null; + StreamReader streamReader = null; + string htmlString = string.Empty; + //转换POST数据 + //建立页面请求 + try + { + httpWebRequest = WebRequest.Create(urlString) as HttpWebRequest; + } + //处理异常 + catch (Exception ex) + { + throw new Exception("建立页面请求时发生错误!", ex); + } + //指定请求处理方式 + httpWebRequest.Method = "POST"; + httpWebRequest.KeepAlive = true; + httpWebRequest.ContentType = "multipart/form-data; boundary=" + BOUNDARY; + httpWebRequest.ContentLength = postDatabytes.Length; + //向服务器传送数据 + try + { + inputStream = httpWebRequest.GetRequestStream(); + inputStream.Write(postDatabytes, 0, postDatabytes.Length); + } + //处理异常 + catch (Exception ex) + { + throw new Exception("发送POST数据时发生错误!", ex); + } + finally + { + inputStream.Close(); + } + //接受服务器返回信息 + try + { + httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; + outputStream = httpWebResponse.GetResponseStream(); + streamReader = new StreamReader(outputStream, Encoding.UTF8); + htmlString = streamReader.ReadToEnd(); + } + //处理异常 + catch (Exception ex) + { + throw new Exception("接受服务器返回页面时发生错误!", ex); + } + finally + { + streamReader.Close(); + } + return htmlString; + } + } +} diff --git a/WeixinHelper/WeixinHelper.csproj b/WeixinHelper/WeixinHelper.csproj new file mode 100644 index 0000000..68cad16 --- /dev/null +++ b/WeixinHelper/WeixinHelper.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {5BD99169-4EB4-4F4E-A22B-DA7E052D9E37} + Exe + Properties + WeixinHelper + WeixinHelper + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file