Files
g.hnyhua.cn/Mtxfw.shop/_Admin/admin_weixin_add.aspx.cs

876 lines
37 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Linq;
using System.Collections.Specialized;
using System.Web.Script.Serialization;
namespace Mtxfw.shop._Admin
{
public partial class admin_weixin_add : Mtxfw.Utility.AdminPage
{
public Mtxfw.DAL.Article daoArt = new Mtxfw.DAL.Article();
public Mtxfw.Utility.Config config = new Mtxfw.Utility.Config();
public String TitName, strwtype = "0", strwtype2 = "0", strtitle, strBody = "", strimg="";
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!IsPostBack)
{
wtype.Attributes.Add("onchange", "onwtype()");
wtype2.Attributes.Add("onchange", "onwtype2()");
Hidd_Url.Value = Request.UrlReferrer.ToString();
}
if (Request.QueryString["ParentID"] != null)
{
if (Request.QueryString["action"] == "add")
{
TitName = "添加";
if (!IsPostBack)
{
Btn_Update.Visible = false;
}
}
else
{
TitName = "更新";
if (!IsPostBack)
{
Btn_Insert.Visible = false;
}
Mtxfw.Model.Article model = daoArt.GetModel(int.Parse(Request.QueryString["ID"]));
if (model != null)
{
if (!IsPostBack)
{
Text_Title.Text = model.Title;
Text_KeyWords.Text = model.KeyWords;
Text_Paths.Text = model.Paths;
Text_Paths2.Text = model.Paths2;
Text_Content2.Text = model.Content2;
hiddenphoto.Value = model.Author;
hiddenphoto2.Value = model.Author;
for (int i = 0; i < wtype.Items.Count; i++)
{
if (wtype.Items[i].Value == model.wtype.ToString())
{
wtype.Items[i].Selected = true;
}
else
{
wtype.Items[i].Selected = false;
}
}
for (int i = 0; i < wtype2.Items.Count; i++)
{
if (wtype2.Items[i].Value == model.wtype2.ToString())
{
wtype2.Items[i].Selected = true;
}
else
{
wtype2.Items[i].Selected = false;
}
}
if (model.Seef == 1)
{
myhh.Checked = true;
}
}
strBody = model.Content;
strwtype = model.wtype.ToString();
strwtype2 = model.wtype2.ToString();
if (hiddenphoto.Value != "")
{
strimg = "<li id='picli0'><img src='/GetFiles.ashx?image=" + hiddenphoto.Value + "&itsize=100x100&itmode=cut' width='100' class='pic'/><a href=javascript:DeletePic('" + hiddenphoto.Value + "',0)>删除</a></li>";
}
}
}
if (Request.QueryString["ParentID"].ToString() == "12")
{
strtitle = "英文版栏目";
}
}
Btn_Insert.Click += delegate(object s, EventArgs ex)
{
try
{
Mtxfw.Model.Article model = new Mtxfw.Model.Article();
model.senderid = 0;
model.receiverid = 0;
model.Title = Text_Title.Text;
model.KeyWords = Text_KeyWords.Text;
model.ParentID = int.Parse(Request.QueryString["ParentID"].ToString());
model.CategoryId = "0";
model.AddTime = DateTime.Now;
model.Author = hiddenphoto.Value;
model.thumb_media_id = "";
string access_token = Mtxfw.Utility.Common.getaccess_token(HttpContext.Current, config.webappKey, config.webappSecret, config);
if (hiddenphoto.Value!=""&& hiddenphoto.Value!=hiddenphoto2.Value)
{
if ((wtype.SelectedItem.Value == "3" && wtype2.SelectedItem.Value == "1") || (wtype.SelectedItem.Value == "4" && wtype2.SelectedItem.Value == "1") || wtype2.SelectedItem.Value == "2" || wtype2.SelectedItem.Value == "3")
{
List<FormItem> list = new List<FormItem>();
list.Add(new FormItem() { Name = "access_token", ParamType = ParamType.Text, Value = access_token });
list.Add(new FormItem() { Name = "type", Value = "image", ParamType = ParamType.Text });//媒体文件类型分别有图片image、语音voice、视频video和缩略图thumb
list.Add(new FormItem() { Name = "media", Value = config.webUpPath + hiddenphoto.Value, ParamType = ParamType.File });
string response = Funcs.PostFormData(list, "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + access_token);
string thumb_media_id = "";
if (response.Length > 0)
{
if (response.IndexOf("media_id") != -1)
{
thumb_media_id = response.Replace("\"", "").Replace(":", "").Replace("{", "").Replace("}", "").Replace("(", "").Replace("media_id", "").Replace(";", "").Trim();
}
}
model.thumb_media_id = thumb_media_id;
}
else
{
model.thumb_media_id = "";
}
}
model.media_id = Text_media_id.Text;
model.Content = Hidd_myEditor.Value;
model.Content2 = Text_Content2.Text;
model.Paths = Text_Paths.Text;
model.Paths2 = Text_Paths2.Text;
model.Seef = (myhh.Checked == true ? 1 : 0);
model.wtype = Convert.ToInt32(wtype.SelectedItem.Value);
model.wtype2 = Convert.ToInt32(wtype2.SelectedItem.Value);
model.LinkID = 0;
daoArt.Add(model);
if (wtype.SelectedItem.Value == "3" && IFSend.Checked == true)
{
string strp = Mtxfw.Utility.Common.getPage2("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + access_token + "&next_openid=", "", "utf-8");
string[] strra = strp.Split(']')[0].Split('[');
string data = "";
if (strra.Length > 1)
{
data = strra[1];
}
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.xml", strp + "|data=" + data);
if (wtype2.SelectedItem.Value == "0" && data != "")
{
string strjson = "{\"touser\": [" + data + "],";
strjson += "\"msgtype\":\"text\",";
strjson += "\"text\":{\"content\":\"" + model.Content2 + "\"}";
strjson += "}";
strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + access_token);
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", strp);
}
else if (wtype2.SelectedItem.Value == "1" && data != "")
{
if (model.thumb_media_id != "")
{
string strjson = "{\"articles\": [";
strjson += "{";
strjson += "\"thumb_media_id\":\"" + model.thumb_media_id + "\",";
strjson += "\"author\":\"荞宝商城\",";
strjson += "\"title\":\"" + model.Title + "\",";
strjson += "\"content_source_url\":\"" + model.Paths + "\",";
strjson += "\"content\":\"<p><img src='http://shop.q-b.cc/images/weixinadd.jpg'/></p>" + model.Content.Replace("\"", "'").Replace(":", "") + "\",";
strjson += "\"digest\":\"" + model.Content2 + "\",";
strjson += "\"show_cover_pic\":\"1\"";
strjson += "}]}";
strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=" + access_token);
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", strp);
if (strp.IndexOf("media_id") != -1)
{
string[] Amedia_id = strp.Split(',');
string media_id = Amedia_id[1].Replace("\"", "").Replace(":", "").Replace("{", "").Replace("(", "").Replace(";", "").Trim();
if (media_id != "")
{
strjson = "{\"touser\": [" + data + "],";
strjson += "\"mpnews\":{\"media_id\":\"" + media_id + "\"},";
strjson += "\"msgtype\":\"mpnews\"";
strjson += "}";
strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + access_token);
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", strp);
}
}
}
}
else if (wtype2.SelectedItem.Value == "2" && data != "")
{
if (model.thumb_media_id != "")
{
string strjson = "{\"articles\": [";
strjson += "{";
strjson += "\"thumb_media_id\":\"" + model.thumb_media_id + "\",";
strjson += "\"author\":\"荞宝商城\",";
strjson += "\"title\":\"" + model.Title + "\",";
strjson += "\"content_source_url\":\"" + model.Paths + "\",";
strjson += "\"content\":\"" + model.Content + "\",";
strjson += "\"digest\":\"" + model.Content2 + "\",";
strjson += "\"show_cover_pic\":\"1\"";
strjson += "}]}";
strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=" + access_token);
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", strp);
if (strp.IndexOf("media_id") != -1)
{
string[] Amedia_id = strp.Split(',');
string media_id = Amedia_id[1].Replace("\"", "").Replace(":", "").Replace("{", "").Replace("(", "").Replace(";", "").Replace("media_id", "").Replace(" ", "").Trim();
if (media_id != "")
{
strjson = "{\"touser\": [" + data + "],";
strjson += "\"voice\":{\"media_id\":\"" + media_id + "\"},";
strjson += "\"msgtype\":\"voice\"";
strjson += "}";
strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + access_token);
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", strp);
}
}
}
}
}
if (Session["UploadFileIng"] != null)
{
string[] stra = Session["UploadFileIng"].ToString().Split(',');
foreach (string str in stra)
{
if (str.Trim() != "")
{
if (model.Content.IndexOf(str.Trim()) == -1)
{
if (System.IO.File.Exists(Server.MapPath(str.Trim())))
{
System.IO.File.Delete(Server.MapPath(str.Trim()));
}
}
}
}
}
Session.Remove("UploadFileIng");
System.Collections.IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
Cache.Remove(CacheEnum.Key.ToString());
}
}
catch (Exception err)
{
Mtxfw.Utility.Common.WriteHtml("/weixin/err.txt", err.ToString());
}
Response.Redirect(Hidd_Url.Value);
};
Btn_Update.Click += delegate(object s, EventArgs ex)
{
try
{
Int32 ID = int.Parse(Request.QueryString["ID"]);
Mtxfw.Model.Article model = daoArt.GetModel(ID);
model.senderid = 0;
model.receiverid = 0;
model.Title = Text_Title.Text;
model.KeyWords = Text_KeyWords.Text;
model.ParentID = int.Parse(Request.QueryString["ParentID"].ToString());
model.AddTime = DateTime.Now;
model.Author = hiddenphoto.Value;
model.Content = Hidd_myEditor.Value;
model.Content2 = Text_Content2.Text;
model.Paths = Text_Paths.Text;
model.Paths2 = Text_Paths2.Text;
model.media_id = Text_media_id.Text;
model.Seef = (myhh.Checked == true ? 1 : 0);
model.LinkID = 0;
model.id = ID;
string access_token = Mtxfw.Utility.Common.getaccess_token(HttpContext.Current, config.webappKey, config.webappSecret, config);
if (hiddenphoto.Value!=""&& hiddenphoto.Value!=hiddenphoto2.Value)
{
if ((wtype.SelectedItem.Value == "3" && wtype2.SelectedItem.Value == "1") || wtype2.SelectedItem.Value == "2")
{
List<FormItem> list = new List<FormItem>();
list.Add(new FormItem() { Name = "access_token", ParamType = ParamType.Text, Value = access_token });
list.Add(new FormItem() { Name = "type", Value = "image", ParamType = ParamType.Text });//媒体文件类型分别有图片image、语音voice、视频video和缩略图thumb
list.Add(new FormItem() { Name = "media", Value = config.webUpPath + hiddenphoto.Value, ParamType = ParamType.File });
string response = Funcs.PostFormData(list, "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + access_token);
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", response);
string thumb_media_id = "";
if (response.Length > 0)
{
if (response.IndexOf("media_id") != -1)
{
thumb_media_id = response.Replace("\"", "").Replace(":", "").Replace("{", "").Replace("}", "").Replace("(", "").Replace("media_id", "").Replace(";", "").Trim();
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", response + "|thumb_media_id=" + thumb_media_id);
}
}
if (thumb_media_id != "")
{
model.thumb_media_id = thumb_media_id;
}
}
}
model.media_id = Text_media_id.Text;
model.wtype = Convert.ToInt32(wtype.SelectedItem.Value);
model.wtype2 = Convert.ToInt32(wtype2.SelectedItem.Value);
if (wtype.SelectedItem.Value == "3" && IFSend.Checked == true)
{
string strp = Mtxfw.Utility.Common.getPage2("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + access_token + "&next_openid=", "", "utf-8");
string[] strra = strp.Split(']')[0].Split('[');
string data = "";
if (strra.Length > 1)
{
data = strra[1];
}
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.xml", strp + "|data=" + data);
if (wtype2.SelectedItem.Value == "0" && data != "")
{
string strjson = "{\"touser\": [" + data + "],";
strjson += "\"msgtype\":\"text\",";
strjson += "\"text\":{\"content\":\"" + model.Content2 + "\"}";
strjson += "}";
strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + access_token);
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", strp);
}
else if (wtype2.SelectedItem.Value == "1" && data != "")
{
if (model.thumb_media_id != "")
{
string strContent = model.Content.Replace("\"", "'").Replace(":", "");
strContent = strContent.Replace("src='/Files", "src='http://shop.q-b.cc/Files");
string strjson = "{\"articles\": [";
strjson += "{";
strjson += "\"thumb_media_id\":\"" + model.thumb_media_id + "\",";
strjson += "\"author\":\"admin\",";
strjson += "\"title\":\"" + model.Title + "\",";
strjson += "\"content_source_url\":\"" + model.Paths + "\",";
strjson += "\"content\":\"<p><img src='http://shop.q-b.cc/images/weixinadd.jpg'/></p>" + strContent + "\",";
strjson += "\"digest\":\"" + model.Content2 + "\",";
strjson += "\"show_cover_pic\":\"0\"";
strjson += "}";
DataSet ds = daoArt.GetList("", "ParentID=" + Request.QueryString["ParentID"].ToString() + " And CategoryId='" + model.id + "'");
if (ds.Tables[0].Rows.Count > 0)
{
int i = 0;
foreach (DataRow drv in ds.Tables[0].Rows)
{
strContent = drv["Content"].ToString().Replace("\"", "'").Replace(":", "");
strContent = strContent.Replace("src='/Files", "src='http://shop.q-b.cc/Files");
strjson += ",{";
strjson += "\"thumb_media_id\":\"" + drv["thumb_media_id"].ToString() + "\",";
strjson += "\"author\":\"admin\",";
strjson += "\"title\":\"" + drv["Title"].ToString() + "\",";
strjson += "\"content_source_url\":\"" + drv["Paths"].ToString() + "\",";
strjson += "\"content\":\"<p><img src='http://shop.q-b.cc/images/weixinadd.jpg'/></p>" + strContent + "\",";
strjson += "\"digest\":\"" + drv["Content2"].ToString() + "\",";
strjson += "\"show_cover_pic\":\"0\"";
strjson += "}";
i += 1;
}
}
ds.Clear();
strjson += "]}";
strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=" + access_token);
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin0.txt", strjson);
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin1.txt", strp);
if (strp.IndexOf("media_id") != -1)
{
string[] Amedia_id = strp.Split(',');
string media_id = Amedia_id[1].Replace("\"", "").Replace(":", "").Replace("{", "").Replace("(", "").Replace(";", "").Replace("media_id", "").Replace(" ", "").Trim();
if (media_id != "")
{
strjson = "{\"touser\": [" + data + "],";
strjson += "\"mpnews\":{\"media_id\":\"" + media_id + "\"},";
strjson += "\"msgtype\":\"mpnews\"";
strjson += "}";
strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + access_token);
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin2.txt", strp + "|media_id=" + media_id);
model.media_id = media_id;
}
}
}
}
else if (wtype2.SelectedItem.Value == "2" && data != "")
{
if (model.thumb_media_id != "")
{
string strjson = "{\"articles\": [";
strjson += "{";
strjson += "\"thumb_media_id\":\"" + model.thumb_media_id + "\",";
strjson += "\"author\":\"荞宝商城\",";
strjson += "\"title\":\"" + model.Title + "\",";
strjson += "\"content_source_url\":\"" + model.Paths + "\",";
strjson += "\"content\":\"" + model.Content + "\",";
strjson += "\"digest\":\"" + model.Content2 + "\",";
strjson += "\"show_cover_pic\":\"0\"";
strjson += "}]}";
strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=" + access_token);
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", strp);
if (strp.IndexOf("media_id") != -1)
{
string[] Amedia_id = strp.Split(',');
string media_id = Amedia_id[1].Replace("\"", "").Replace(":", "").Replace("{", "").Replace("(", "").Replace(";", "").Replace("media_id", "").Replace(" ", "").Trim();
if (media_id != "")
{
strjson = "{\"touser\": [" + data + "],";
strjson += "\"voice\":{\"media_id\":\"" + media_id + "\"},";
strjson += "\"msgtype\":\"voice\"";
strjson += "}";
strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + access_token);
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", strp);
}
}
}
}
}
daoArt.Update(model);
if (Session["UploadFileIng"] != null)
{
string[] stra = Session["UploadFileIng"].ToString().Split(',');
foreach (string str in stra)
{
if (str.Trim() != "")
{
if (model.Content.IndexOf(str.Trim()) == -1)
{
if (System.IO.File.Exists(Server.MapPath(str.Trim())))
{
System.IO.File.Delete(Server.MapPath(str.Trim()));
}
}
}
}
}
Session.Remove("UploadFileIng");
System.Collections.IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
Cache.Remove(CacheEnum.Key.ToString());
}
Response.Redirect(Hidd_Url.Value);
}
catch (Exception err)
{
Mtxfw.Utility.Common.WriteHtml("/weixin/err.txt", err.ToString());
}
};
Btn_Upload.Click += delegate(object s, EventArgs ex)
{
try
{
Mtxfw.Utility.UploadFile upload = new Utility.UploadFile(FileUpload1.PostedFile, config.webUpPath, config.webUpType, int.Parse(config.webUpSize));
if (FileUpload1.FileName.Length > 0)
{
upload.StatrUpFile();
if (Text_Paths2.Text != "")
{
if (System.IO.File.Exists(Server.MapPath(Text_Paths2.Text)))
{
System.IO.File.Delete(Server.MapPath(Text_Paths2.Text));
}
}
Text_Paths2.Text = upload.UploadPath;
if (wtype2.SelectedItem.Value == "3" || wtype2.SelectedItem.Value == "4")
{
string access_token = Mtxfw.Utility.Common.getaccess_token(HttpContext.Current, config.webappKey, config.webappSecret, config);
List<FormItem> list = new List<FormItem>();
list.Add(new FormItem() { Name = "access_token", ParamType = ParamType.Text, Value = access_token });
list.Add(new FormItem() { Name = "type", Value = (wtype2.SelectedItem.Value == "3" ? "voice" : "video"), ParamType = ParamType.Text });//媒体文件类型分别有图片image、语音voice、视频video和缩略图thumb
list.Add(new FormItem() { Name = "media", Value = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "Image/" + upload.UploadPath, ParamType = ParamType.File });
string response = Funcs.PostFormData(list, "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" + access_token + "&type=" + (wtype2.SelectedItem.Value == "3" ? "voice" : "video"));
string media_id = "";
if (response.Length > 0)
{
if (response.IndexOf("media_id") != -1)
{
media_id = response.Replace("\"", "").Replace(":", "").Replace("{", "").Replace("}", "").Replace("(", "").Replace("media_id", "").Replace(";", "").Trim();
}
}
Text_media_id.Text = media_id;
}
}
}
catch (Exception err)
{
Mtxfw.Utility.Common.WriteHtml("/weixin/err.txt", err.ToString());
}
};
}
public object FromJson(string json)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Deserialize<getopenids>(json);
}
}
//FormItem类
public class FormItem
{
public string Name { get; set; }
public ParamType ParamType { get; set; }
public string Value { get; set; }
}
//ParamType枚举
public enum ParamType
{
///
/// 文本类型
///
Text,
///
/// 文件路径需要全路径C:\A.JPG)
///
File
}
//Funcs静态类
public static class Funcs
{
public static string PostFormData(List<FormItem> list, string uri)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
//请求
WebRequest req = WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "multipart/form-data; boundary=" + boundary;
//组织表单数据
StringBuilder sb = new StringBuilder();
foreach (FormItem item in list)
{
switch (item.ParamType)
{
case ParamType.Text:
sb.Append("--" + boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"" + item.Name + "\"");
sb.Append("\r\n\r\n");
sb.Append(item.Value);
sb.Append("\r\n");
break;
case ParamType.File:
sb.Append("--" + boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"media\"; filename=\"" + item.Value + "\"");
sb.Append("\r\n");
sb.Append("Content-Type: application/octet-stream");
sb.Append("\r\n\r\n");
break;
}
}
string head = sb.ToString();
//post字节总长度
long length = 0;
byte[] form_data = Encoding.UTF8.GetBytes(head);
//结尾
byte[] foot_data = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
List<FormItem> fileList = list.Where(f => f.ParamType == ParamType.File).ToList();
length = form_data.Length + foot_data.Length;
foreach (FormItem fi in fileList)
{
FileStream fileStream = new FileStream(fi.Value, FileMode.Open, FileAccess.Read);
length += fileStream.Length;
fileStream.Close();
}
req.ContentLength = length;
Stream requestStream = req.GetRequestStream();
//发送表单参数
requestStream.Write(form_data, 0, form_data.Length);
foreach (FormItem fd in fileList)
{
FileStream fileStream = new FileStream(fd.Value, FileMode.Open, FileAccess.Read);
//文件内容
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);
//结尾
requestStream.Write(foot_data, 0, foot_data.Length);
}
requestStream.Close();
//响应
WebResponse pos = req.GetResponse();
StreamReader sr = new StreamReader(pos.GetResponseStream(), Encoding.UTF8);
string html = sr.ReadToEnd().Trim();
sr.Close();
if (pos != null)
{
pos.Close();
pos = null;
}
if (req != null)
{
req = null;
}
return html;
}
///
/// 从URL地址下载文件到本地磁盘
///
/// 本地磁盘地址
/// URL网址
///
public static string SaveFileFromUrl(string FileName, string Url)
{
WebResponse response = null;
Stream stream = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
response = request.GetResponse();
stream = response.GetResponseStream();
if (!response.ContentType.ToLower().StartsWith("text/"))
{
SaveBinaryFile(response, FileName);
}
else
{
StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
return sr.ReadToEnd();
}
}
catch (Exception err)
{
return err.ToString();
}
return "complete";
}
///
/// 将二进制文件保存到磁盘
///
/// 将二进制文件保存到磁盘
// 将二进制文件保存到磁盘
private static bool SaveBinaryFile(WebResponse response, string FileName)
{
bool Value = true;
byte[] buffer = new byte[1024];
try
{
if (File.Exists(FileName))
File.Delete(FileName);
Stream outStream = System.IO.File.Create(FileName);
Stream inStream = response.GetResponseStream();
int l;
do
{
l = inStream.Read(buffer, 0, buffer.Length);
if (l > 0)
outStream.Write(buffer, 0, l);
}
while (l > 0);
outStream.Close();
inStream.Close();
}
catch
{
Value = false;
}
return Value;
}
}
public class getopenids
{
public String total { get; set; }
public String count { get; set; }
public String data { get; set; }
public String next_openid { get; set; }
}
}