1
0
mirror of https://e.coding.net/circlecloud/WeixinHelper.git synced 2024-12-26 20:38:46 +00:00

微信助手项目上传...

This commit is contained in:
j502647092 2015-08-13 16:14:57 +08:00
parent 0185c75c7f
commit c199d0ddbd
4 changed files with 257 additions and 0 deletions

6
WeixinHelper/App.config Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>

View File

@ -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")]

View File

@ -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
{
/// <summary>
/// 上传微信永久图片
/// </summary>
/// <param name="access_token">Token</param>
/// <param name="filename">文件名称</param>
/// <param name="filebytes">文件字节流</param>
/// <returns>TX返回的Json数据</returns>
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<byte> datalist = new List<byte>();
datalist.AddRange(headerbytes);
datalist.AddRange(filebytes);
datalist.AddRange(footbytes);
byte[] data = datalist.ToArray<byte>();
return GetHtmlFromPost(url, data, BOUNDARY);
}
catch (Exception)
{
return string.Empty;
}
}
/// <summary>
/// 获取玩家数据流
/// </summary>
/// <param name="path">文件路径</param>
/// <returns>文件数据流</returns>
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;
}
/// <summary>
/// 提供通过POST方法获取页面的方法
/// </summary>
/// <param name="urlString">请求的URL</param>
/// <param name="postDatabytes">POST数据</param>
/// <param name="BOUNDARY">BOUNDARY</param>
/// <returns></returns>
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;
}
}
}

View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{5BD99169-4EB4-4F4E-A22B-DA7E052D9E37}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>WeixinHelper</RootNamespace>
<AssemblyName>WeixinHelper</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="UploadHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>