【移動(dòng)應(yīng)用開發(fā)技術(shù)】asp.net微信開發(fā)之如何實(shí)現(xiàn)群發(fā)圖文_第1頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】asp.net微信開發(fā)之如何實(shí)現(xiàn)群發(fā)圖文_第2頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】asp.net微信開發(fā)之如何實(shí)現(xiàn)群發(fā)圖文_第3頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】asp.net微信開發(fā)之如何實(shí)現(xiàn)群發(fā)圖文_第4頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】asp.net微信開發(fā)之如何實(shí)現(xiàn)群發(fā)圖文_第5頁
已閱讀5頁,還剩33頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

【移動(dòng)應(yīng)用開發(fā)技術(shù)】微信開發(fā)之如何實(shí)現(xiàn)群發(fā)圖文

/upload/information/20201208/260/14037.jpg/upload/information/20201208/260/14039.jpg

<!--彈出選擇素材窗口-->

<p

id="shownewgroup">

<p

class="closeLogin"

style="height:40px;

background-color:#ddd9ff;

line-height:40px;"><span

style="float:left;

color:#000;

font-size:14px;

text-indent:5px;">選擇素材</span>

<span

style="float:left;margin-left:20px;"><a

href="WxNewTuWen.aspx"

style="color:red;"

onclick="hrefurl();"

class="hrefurl">新建圖文素材</a></span>

<a

class="closeloginpage"><img

src="images/close1.png"

alt=""

/></a>

</p>

<p

style="height:455px;

width:100%;">

<asp:UpdatePanel

ID="UpdatePanel2"

runat="server">

<ContentTemplate>

<p

style="width:100%;

height:35px;

margin:10px;"><asp:LinkButton

ID="LinkBtnSelect"

runat="server"

OnClick="LinkBtnSelect_Click"

><p

style="background-image:url('images/buttonbg.png');

width:111px;

height:35px;

float:left;

line-height:35px;

font-weight:bold;

text-align:center;color:#fff;">確認(rèn)選擇</p></asp:LinkButton>

<span

style="float:left;margin-left:20px;"><asp:LinkButton

ID="LinkbtnRefresh"

CssClass="LinkbtnRefresh"

runat="server"

OnClick="LinkbtnRefresh_Click"><p

style="background-image:url('images/buttonbg.png');

width:111px;

height:35px;

line-height:35px;

font-weight:bold;

text-align:center;color:#fff;">刷新</p></asp:LinkButton></span>

<span

style="float:left;margin-left:20px;"><asp:LinkButton

ID="LinkBtnDelete"

CssClass="LinkbtnRefresh"

runat="server"

OnClick="LinkBtnDelete_Click"><p

style="background-image:url('images/buttonbg.png');

width:111px;

height:35px;

line-height:35px;

font-weight:bold;

text-align:center;color:#fff;">刪除素材</p></asp:LinkButton></span>

</p>

<p

style="word-wrap:break-word;"

id="lbnewssucai"

runat="server">

<asp:Repeater

ID="Repeatersucailist"

runat="server"

OnItemDataBound="Repeatersucailist_ItemDataBound">

<ItemTemplate>

<table

style="width:100%;

border-top:1px

solid

#edc9df;

border-collapse:collapse;

font-size:12px;"

>

<tr>

<td

style="width:100px;"><asp:Image

ID="ImageUrl"

CssClass="fenmianstyle2"

runat="server"

/></td>

<td

style="text-align:left;

width:470px;

">

<asp:Repeater

ID="Repeatersucailist2"

runat="server">

<ItemTemplate>

<ul

style="margin:0px;padding:0px;">

<li><%#

Eval("title")

%></li>

</ul>

</ItemTemplate>

</asp:Repeater>

</td>

<td

style="width:130px;">

<asp:Label

ID="lbUpate_time"

runat="server"

Text="Label"></asp:Label>

</td>

<td

style="width:50px;

text-align:center;">

<asp:CheckBox

ID="CheckIn"

runat="server"

/>

<asp:Label

ID="lbmedia_id"

runat="server"

Visible="false"

Text=""></asp:Label>

</td>

</tr>

</table>

</ItemTemplate>

</asp:Repeater>

<p

style="font-size:14px;

height:30px;

line-height:30px;

text-indent:10px;

border-top:1px

solid

#ced9df;">

<span

style="float:left;">本類型素材總數(shù)量為:</span><span

style="float:left;

color:red;"><asp:Label

ID="lbtotal_count"

runat="server"

Text="0"></asp:Label></span>

<span

style="float:left;

margin-left:20px;">本次獲取的素材數(shù)量為:</span><span

style="float:left;

color:red;"><asp:Label

ID="lbitem_count"

runat="server"

Text="0"></asp:Label></span>

</p>

</p>

</ContentTemplate>

</asp:UpdatePanel>

</p>

</p>

<p

id="shownewgroupzhezhaoceng"></p>

///

<summary>

///

綁定圖文素材列表

///

</summary>

private

void

BindNewsSucaiList()

{

WeiXinServer

wxs

=

new

WeiXinServer();

string

res

=

"";

///從緩存讀取accesstoken

string

Access_token

=

Cache["Access_token"]

as

string;

if

(Access_token

==

null)

{

//如果為空,重新獲取

Access_token

=

wxs.GetAccessToken();

//設(shè)置緩存的數(shù)據(jù)7000秒后過期

Cache.Insert("Access_token",

Access_token,

null,

DateTime.Now.AddSeconds(7000),

System.Web.Caching.Cache.NoSlidingExpiration);

}

string

Access_tokento

=

Access_token.Substring(17,

Access_token.Length

-

37);

string

posturl

=

"/cgi-bin/material/batchget_material?access_token="

+

Access_tokento;

//POST數(shù)據(jù)例子:

POST數(shù)據(jù)例子:{"type":TYPE,"offset":OFFSET,"count":COUNT}

string

postData

=

"{\"type\":\"news\",\"offset\":\"0\",\"count\":\"20\"}";

res

=

wxs.GetPage(posturl,

postData);

//使用前需要引用Newtonsoft.json.dll文件

JObject

jsonObj

=

JObject.Parse(res);

int

groupsnum

=

jsonObj["item"].Count();

List<WxNewsSucaiIteminfo>

newssucaiitemlist

=

new

List<WxNewsSucaiIteminfo>();

List<WxNewsSuCaiItemlistinfo>

WxNewsSuCaiItemlist

=

new

List<WxNewsSuCaiItemlistinfo>();

for

(int

i

=

0;

i

<

groupsnum;

i++)

{

WxNewsSucaiIteminfo

newssucaiitem

=

new

WxNewsSucaiIteminfo();

newssucaiitem.media_id

=

jsonObj["item"][i]["media_id"].ToString();

newssucaiitem.update_time

=

jsonObj["item"][i]["update_time"].ToString();

newssucaiitem.total_count

=

jsonObj["total_count"].ToString();

newssucaiitem.item_count

=

jsonObj["item_count"].ToString();

newssucaiitemlist.Add(newssucaiitem);

int

news_itemcount

=

jsonObj["item"][i]["content"]["news_item"].Count();

if

(news_itemcount

>

0)

{

for

(int

j

=

0;

j

<

news_itemcount;

j++)

{

WxNewsSuCaiItemlistinfo

wnscilinfo

=

new

WxNewsSuCaiItemlistinfo();

wnscilinfo.title

=

jsonObj["item"][i]["content"]["news_item"][j]["title"].ToString();

wnscilinfo.thumb_media_id

=

jsonObj["item"][i]["content"]["news_item"][j]["thumb_media_id"].ToString();

wnscilinfo.show_cover_pic

=

int.Parse(jsonObj["item"][i]["content"]["news_item"][j]["show_cover_pic"].ToString());

wnscilinfo.author

=

jsonObj["item"][i]["content"]["news_item"][j]["author"].ToString();

wnscilinfo.digest

=

jsonObj["item"][i]["content"]["news_item"][j]["digest"].ToString();

wnscilinfo.content

=

jsonObj["item"][i]["content"]["news_item"][j]["content"].ToString();

wnscilinfo.url

=

jsonObj["item"][i]["content"]["news_item"][j]["url"].ToString();

wnscilinfo.content_source_url

=

jsonObj["item"][i]["content"]["news_item"][j]["content_source_url"].ToString();

wnscilinfo.media_id

=

newssucaiitem.media_id.ToString();

WxNewsSuCaiItemlist.Add(wnscilinfo);

}

}

}

Session["WxNewsSuCaiItemlist"]

=

WxNewsSuCaiItemlist;

this.Repeatersucailist.DataSource

=

newssucaiitemlist;

this.Repeatersucailist.DataBind();

}/upload/information/20201208/260/14041.jpg/upload/information/20201208/260/14044.jpg

///

<summary>

///

///

</summary>上傳圖片文件

///

<param

name="sender"></param>

///

<param

name="e"></param>

protected

void

LinkBtnFileUploadImg_Click(object

sender,

EventArgs

e)

{

if

(this.FileUploadImg.HasFile)

{

string

fileContentType

=

FileUploadImg.PostedFile.ContentType;

if

(fileContentType

==

"image/bmp"

||

fileContentType

==

"image/gif"

||

fileContentType

==

"image/png"

||

fileContentType

==

"image/x-png"

||

fileContentType

==

"image/jpeg"

||

fileContentType

==

"image/pjpeg")

{

int

fileSize

=

this.FileUploadImg.PostedFile.ContentLength;

if

(fileSize

<=2097152)

{

string

fileName

=

this.FileUploadImg.PostedFile.FileName;

//

客戶端文件路徑

string

filepath

=

FileUploadImg.PostedFile.FileName;

//得到的是文件的完整路徑,包括文件名,如:C:\Documents

and

Settings\Administrator\My

Documents\My

Pictures\20022775_m.jpg

//string

filepath

=

FileUpload1.FileName;

//得到上傳的文件名20022775_m.jpg

string

filename

=

filepath.Substring(filepath.LastIndexOf("\\")

+

1);//20022775_m.jpg

string

serverpath

=

Server.MapPath("~/WeiXinImg/")

+

filename;//取得文件在服務(wù)器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg

this.ImgTuWen.ImageUrl

=

"~/WeiXinImg/"

+

FileUploadImg.FileName;

this.ImgTuWen2.Visible

=

true;

this.ImgTuWen2.ImageUrl

=

"~/WeiXinImg/"

+

FileUploadImg.FileName;

this.FileUploadImg.PostedFile.SaveAs(serverpath);//將上傳的文件另存為

this.LinkBtnDeleteImg.Visible

=

true;

Session["fileNameimg"]

=

this.FileUploadImg.PostedFile.FileName;

//上傳臨時(shí)圖片素材至微信服務(wù)器,3天后微信服務(wù)器會(huì)自動(dòng)刪除

WeiXinServer

wxs

=

new

WeiXinServer();

///從緩存讀取accesstoken

string

Access_token

=

Cache["Access_token"]

as

string;

if

(Access_token

==

null)

{

//如果為空,重新獲取

Access_token

=

wxs.GetAccessToken();

//設(shè)置緩存的數(shù)據(jù)7000秒后過期

Cache.Insert("Access_token",

Access_token,

null,

DateTime.Now.AddSeconds(7000),

System.Web.Caching.Cache.NoSlidingExpiration);

}

string

Access_tokento

=

Access_token.Substring(17,

Access_token.Length

-

37);

//WebClient

wx_upload

=

new

WebClient();

//wx_upload.Credentials

=

CredentialCache.DefaultCredentials;

string

url

=

string.Format("/cgi-bin/media/upload?access_token={0}&type={1}",

Access_tokento,

"image");

string

result

=

HttpUploadFile(url,

serverpath);

if

(result.Contains("media_id"))

{

//使用前需要引用Newtonsoft.json.dll文件

JObject

jsonObj

=

JObject.Parse(result);

Session["imgmedia_id"]

=

jsonObj["media_id"].ToString();

}

Response.Write("<script>alert('上傳圖片成功!')</script>");

}

else

{

Response.Write("<script>alert('上傳文件不能大于2M!')</script>");

}

}

else

{

Response.Write("<script>alert('只支持BMP,GIF,PNG,JPG格式的圖片!')</script>");

}

}

else

{

Response.Write("<script>alert('請選擇圖片!')</script>");

}

}

///

<summary>

///

Http上傳文件

///

</summary>

public

static

string

HttpUploadFile(string

url,

string

path)

{

//

設(shè)置參數(shù)

HttpWebRequest

request

=

WebRequest.Create(url)

as

HttpWebRequest;

CookieContainer

cookieContainer

=

new

CookieContainer();

request.CookieContainer

=

cookieContainer;

request.AllowAutoRedirect

=

true;

request.Method

=

"POST";

string

boundary

=

DateTime.Now.Ticks.ToString("X");

//

隨機(jī)分隔線

request.ContentType

=

"multipart/form-data;charset=utf-8;boundary="

+

boundary;

byte[]

itemBoundaryBytes

=

Encoding.UTF8.GetBytes("\r\n--"

+

boundary

+

"\r\n");

byte[]

endBoundaryBytes

=

Encoding.UTF8.GetBytes("\r\n--"

+

boundary

+

"--\r\n");

int

pos

=

path.LastIndexOf("\\");

string

fileName

=

path.Substring(pos

+

1);

//請求頭部信息

StringBuilder

sbHeader

=

new

StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n",

fileName));

byte[]

postHeaderBytes

=

Encoding.UTF8.GetBytes(sbHeader.ToString());

FileStream

fs

=

new

FileStream(path,

FileMode.Open,

FileAccess.Read);

byte[]

bArr

=

new

byte[fs.Length];

fs.Read(bArr,

0,

bArr.Length);

fs.Close();

Stream

postStream

=

request.GetRequestStream();

postStream.Write(itemBoundaryBytes,

0,

itemBoundaryBytes.Length);

postStream.Write(postHeaderBytes,

0,

postHeaderBytes.Length);

postStream.Write(bArr,

0,

bArr.Length);

postStream.Write(endBoundaryBytes,

0,

endBoundaryBytes.Length);

postStream.Close();

//發(fā)送請求并獲取相應(yīng)回應(yīng)數(shù)據(jù)

HttpWebResponse

response

=

request.GetResponse()

as

HttpWebResponse;

//直到request.GetResponse()程序才開始向目標(biāo)網(wǎng)頁發(fā)送Post請求

Stream

instream

=

response.GetResponseStream();

StreamReader

sr

=

new

StreamReader(instream,

Encoding.UTF8);

//返回結(jié)果網(wǎng)頁(html)代碼

string

content

=

sr.ReadToEnd();

return

content;

}

///

<summary>

///

刪除圖片

///

</summary>

///

<param

name="sender"></param>

///

<param

name="e"></param>

protected

void

LinkBtnDeleteImg_Click(object

sender,

EventArgs

e)

{

string

filename

=

Session["fileNameimg"].ToString();

if

(!string.IsNullOrEmpty(filename))//確保picPath有值并且不為空。

{

string

serverpath

=

Server.MapPath("~/WeiXinImg/")

+

filename;//取得文件在服務(wù)器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg

if

(File.Exists(serverpath))

{

try

{

File.Delete(serverpath);

this.ImgTuWen.ImageUrl

=

"weixinimg/fengmiandefault.jpg";

this.ImgTuWen2.Visible

=

false;

this.ImgTuWen2.ImageUrl

=

"";

Session["fileNameimg"]

=

null;

this.LinkBtnDeleteImg.Visible

=

false;

}

catch(Exception

ex)

{

//錯(cuò)誤處理:

Response.Write(ex.Message.ToString());

}

}

}

}

///

<summary>

///

預(yù)覽圖文消息

///

</summary>

///

<param

name="sender"></param>

///

<param

name="e"></param>

protected

void

LinkBtnSendPreview_Click(object

sender,

EventArgs

e)

{

Session["media_id"]

=

null;

//非空驗(yàn)證

if

(String.IsNullOrWhiteSpace(this.txttuwen_title.Value.ToString()))

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('請輸入圖文標(biāo)題!');",

true);

this.txttuwen_title.Focus();

return;

}

if

(this.ImgTuWen2.ImageUrl.ToString().Equals(""))

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('必須上傳一張圖片!');",

true);

this.ImgTuWen2.Focus();

return;

}

if

(String.IsNullOrWhiteSpace(this.tbContent.InnerText.ToString()))

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('請輸入正文內(nèi)容!');",

true);

this.tbContent.Focus();

return;

}

//對各項(xiàng)進(jìn)行賦值

WeiXinServer

wxs

=

new

WeiXinServer();

///從緩存讀取accesstoken

string

Access_token

=

Cache["Access_token"]

as

string;

if

(Access_token

==

null)

{

//如果為空,重新獲取

Access_token

=

wxs.GetAccessToken();

//設(shè)置緩存的數(shù)據(jù)7000秒后過期

Cache.Insert("Access_token",

Access_token,

null,

DateTime.Now.AddSeconds(7000),

System.Web.Caching.Cache.NoSlidingExpiration);

}

string

Access_tokento

=

Access_token.Substring(17,

Access_token.Length

-

37);

//POST數(shù)據(jù)例子:

POST數(shù)據(jù)例子:

//{

//

"articles":

[{

//

"title":

TITLE,

//

"thumb_media_id":

THUMB_MEDIA_ID,

//

"author":

AUTHOR,

//

"digest":

DIGEST,

//

"show_cover_pic":

SHOW_COVER_PIC(0

/

1),

//

"content":

CONTENT,

//

"content_source_url":

CONTENT_SOURCE_URL

//

},

//

//若新增的是多圖文素材,則此處應(yīng)還有幾段articles結(jié)構(gòu)

//

]

//}

string

isshow_cover_pic

=

"";

if

(this.CheckFengMianShow.Checked)

{

isshow_cover_pic

=

"1";

}

else

{

isshow_cover_pic

=

"0";

}

string

description

=

NoHTML(this.tbContent.InnerText.ToString());

string

postData

=

"{\"articles\":[{\"title\":\""

+

this.txttuwen_title.Value.ToString()

+

"\",\"thumb_media_id\":\""

+

Session["imgmedia_id"].ToString()

+

"\",\"author\":\""

+

this.txttuwen_author.Value.ToString()

+

"\",\"digest\":\""

+

this.txtzhaiyao.InnerText.ToString()

+

"\",\"show_cover_pic\":\""

+

isshow_cover_pic

+

"\",\"content\":\""

+

description

+

"\",\"content_source_url\":\""

+

this.txtYuanWenUrl.Text.ToString()

+

"\"}]}";

string

posturl

=

string.Format("/cgi-bin/media/uploadnews?access_token={0}",

Access_tokento);

string

jsonres

=

PostUrl(posturl,

postData);

if

(jsonres.Contains("media_id"))

{

//使用前需要引用Newtonsoft.json.dll文件

JObject

jsonObj

=

JObject.Parse(jsonres);

if

(this.txttoUserName.Value.ToString().Trim().Equals("請輸入用戶微信號"))

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('請輸入接收消息的用戶微信號!');",

true);

return;

}

string

posturls

=

"/cgi-bin/message/mass/preview?access_token="

+

Access_tokento;

//預(yù)覽圖文消息的json數(shù)據(jù){

//

"touser":"OPENID",

可改為對微信號預(yù)覽,例如towxname:zhangsan

//

"mpnews":{

//

"media_id":"123dsdajkasd231jhksad"

//

},

//

"msgtype":"mpnews"

//}

string

postDatas

=

"{\"towxname\":\""

+

this.txttoUserName.Value.ToString()

+

"\",\"mpnews\":{\"media_id\":\""

+

jsonObj["media_id"].ToString()

+

"\"},\"msgtype\":\"mpnews\"}";

string

tuwenres

=

wxs.GetPage(posturls,

postDatas);

//使用前需藥引用Newtonsoft.json.dll文件

JObject

jsonObjss

=

JObject.Parse(tuwenres);

if

(jsonObjss["errcode"].ToString().Equals("0"))

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('發(fā)送預(yù)覽成功??!');",

true);

return;

}

else

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('發(fā)送預(yù)覽失敗??!');",

true);

return;

}

}

}

public

static

string

NoHTML(string

Htmlstring)

{

//刪除腳本

Htmlstring

=

Regex.Replace(Htmlstring,

@"<script[^>]*?>.*?</script>",

"",

RegexOptions.IgnoreCase);

//替換標(biāo)簽

Htmlstring

=

Htmlstring.Replace("\r\n",

"

");

Htmlstring

=

Htmlstring.Replace("\"",

"'");

Htmlstring

=

Htmlstring.Replace("

",

"

");

return

Htmlstring;

}

///

<summary>

///

確認(rèn)選擇

///

</summary>

///

<param

name="sender"></param>

///

<param

name="e"></param>

protected

void

LinkBtnSubSave_Click(object

sender,

EventArgs

e)

{

Session["media_id"]

=

null;

//非空驗(yàn)證

if

(String.IsNullOrWhiteSpace(this.txttuwen_title.Value.ToString()))

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('請輸入圖文標(biāo)題!');",

true);

return;

}

if

(this.ImgTuWen2.ImageUrl.ToString().Equals(""))

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('必須上傳一張圖片!');",

true);

return;

}

if

(String.IsNullOrWhiteSpace(this.tbContent.InnerText.ToString()))

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('請輸入正文內(nèi)容!');",

true);

return;

}

//對各項(xiàng)進(jìn)行賦值

WeiXinServer

wxs

=

new

WeiXinServer();

///從緩存讀取accesstoken

string

Access_token

=

Cache["Access_token"]

as

string;

if

(Access_token

==

null)

{

//如果為空,重新獲取

Access_token

=

wxs.GetAccessToken();

//設(shè)置緩存的數(shù)據(jù)7000秒后過期

Cache.Insert("Access_token",

Access_token,

null,

DateTime.Now.AddSeconds(7000),

System.Web.Caching.Cache.NoSlidingExpiration);

}

string

Access_tokento

=

Access_token.Substring(17,

Access_token.Length

-

37);

//POST數(shù)據(jù)例子:

POST數(shù)據(jù)例子:

//{

//

"articles":

[{

//

"title":

TITLE,

//

"thumb_media_id":

THUMB_MEDIA_ID,

//

"author":

AUTHOR,

//

"digest":

DIGEST,

//

"show_cover_pic":

SHOW_COVER_PIC(0

/

1),

//

"content":

CONTENT,

//

"content_source_url":

CONTENT_SOURCE_URL

//

},

//

//若新增的是多圖文素材,則此處應(yīng)還有幾段articles結(jié)構(gòu)

//

]

//}

string

isshow_cover_pic

=

"";

if

(this.CheckFengMianShow.Checked)

{

isshow_cover_pic

=

"1";

}

else

{

isshow_cover_pic

=

"0";

}

string

description

=

NoHTML(this.tbContent.InnerText.ToString());

string

postData

=

"{\"articles\":[{\"title\":\""

+

this.txttuwen_title.Value.ToString()

+

"\",\"thumb_media_id\":\""

+

Session["imgmedia_id"].ToString()

+

"\",\"author\":\""

+

this.txttuwen_author.Value.ToString()

+

"\",\"digest\":\""

+

this.txtzhaiyao.InnerText.ToString()

+

"\",\"show_cover_pic\":\""

+

isshow_cover_pic

+

"\",\"content\":\""

+

description

+

"\",\"content_source_url\":\""

+

this.txtYuanWenUrl.Text.ToString()

+

"\"}]}";

string

posturl

=

string.Format("/cgi-bin/media/uploadnews?access_token={0}",

Access_tokento);

string

jsonres

=

PostUrl(posturl,

postData);

if

(jsonres.Contains("media_id"))

{

//使用前需要引用Newtonsoft.json.dll文件

JObject

jsonObj

=

JObject.Parse(jsonres);

WxMpNewsInfo

wmninfo

=

new

WxMpNewsInfo();

wmninfo.title

=

this.txttuwen_title.Value.ToString();

wmninfo.contents

=

description.ToString();

wmninfo.ImageUrl

=

this.ImgTuWen.ImageUrl.ToString();

Session["wmninfo"]

=

wmninfo;

Response.Redirect("WxMassManage.aspx?media_id="

+

jsonObj["media_id"].ToString());

}

}

///

<summary>

///

請求Url,發(fā)送數(shù)據(jù)

///

</summary>

public

static

string

PostUrl(string

url,

string

postData)

{

byte[]

data

=

Encoding.UTF8.GetBytes(postData);

//

設(shè)置參數(shù)

HttpWebRequest

request

=

WebRequest.Create(url)

as

HttpWebRequest;

CookieContainer

cookieContainer

=

new

CookieContainer();

request.CookieContainer

=

cookieContainer;

request.AllowAutoRedirect

=

true;

request.Method

=

"POST";

request.ContentType

=

"application/x-www-form-urlencoded";

request.ContentLength

=

data.Length;

Stream

outstream

=

request.GetRequestStream();

outstream.Write(data,

0,

data.Length);

outstream.Close();

//發(fā)送請求并獲取相應(yīng)回應(yīng)數(shù)據(jù)

HttpWebResponse

response

=

request.GetResponse()

as

HttpWebResponse;

//直到request.GetResponse()程序才開始向目標(biāo)網(wǎng)頁發(fā)送Post請求

Stream

instream

=

response.GetResponseStream();

StreamReader

sr

=

new

StreamReader(instream,

Encoding.UTF8);

//返回結(jié)果網(wǎng)頁(html)代碼

string

content

=

sr.ReadToEnd();

return

content;

}

protected

void

Page_Load(object

sender,

EventArgs

e)

{

if(!Page.IsPostBack)

{

BindNewsSucaiList();//綁定素材列表

BindGroupList();//綁定分組列表

BindMassCount();//綁定本月已群發(fā)條數(shù)

this.DataBind();

if

(Request.QueryString["media_id"]

!=

null)

{

this.RadioBtnList.SelectedValue

=

"1";

this.showExpress.Visible

=

false;

this.txtwenben.Visible

=

false;

this.tuwen.Visible

=

true;

this.tuwenxuan.Visible

=

false;

this.tuwenjian.Visible

=

false;

this.lbtuwenmedai_id.Visible

=

true;

this.lbtuwenmedai_id.Text

=

Request.QueryString["media_id"].ToString();

this.LinkBtndeletetuwen.Visible

=

true;

this.Imageyixuan.Visible

=

true;

}

}

}/upload/information/20201208/260/14047.jpg/upload/information/20201208/260/14049.jpg

///

<summary>

///

群發(fā)

///

</summary>

///

<param

name="sender"></param>

///

<param

name="e"></param>

protected

void

LinkBtnSubSend_Click(object

sender,

EventArgs

e)

{

//根據(jù)單選按鈕判斷類型,//如果選擇的是圖文消息

if

(this.RadioBtnList.SelectedValue.ToString().Equals("1"))

{

if

(String.IsNullOrWhiteSpace(this.lbtuwenmedai_id.Text.ToString().Trim()))

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('請選擇或新建圖文素材再進(jìn)行群發(fā)!');",

true);

return;

}

WxMassService

wms

=

new

WxMassService();

List<WxMassInfo>

wxmaslist

=

wms.GetMonthMassCount();

if

(wxmaslist.Count

>=

4)

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('本月可群發(fā)消息數(shù)量已達(dá)上限!');",

true);

return;

}

else

{

//如何群發(fā)類型為全部用戶,根據(jù)openID列表群發(fā)給全部用戶,訂閱號不可用,服務(wù)號認(rèn)證后可用

if

(this.DDLMassType.SelectedValue.ToString().Equals("0"))

{

StringBuilder

sbs

=

new

StringBuilder();

sbs.Append(GetAllUserOpenIDList());

WeiXinServer

wxs

=

new

WeiXinServer();

///從緩存讀取accesstoken

string

Access_token

=

Cache["Access_token"]

as

string;

if

(Access_token

==

null)

{

//如果為空,重新獲取

Access_token

=

wxs.GetAccessToken();

//設(shè)置緩存的數(shù)據(jù)7000秒后過期

Cache.Insert("Access_token",

Access_token,

null,

DateTime.Now.AddSeconds(7000),

System.Web.Caching.Cache.NoSlidingExpiration);

}

string

Access_tokento

=

Access_token.Substring(17,

Access_token.Length

-

37);

string

posturl

=

"/cgi-bin/message/mass/send?access_token="

+

Access_tokento;

///群發(fā)POST數(shù)據(jù)示例如下:

//

{

//

"touser":[

//

"OPENID1",

//

"OPENID2"

//

],

//

"mpnews":{

//

"media_id":"123dsdajkasd231jhksad"

//

},

//

"msgtype":"mpnews"

//}

string

postData

=

"{\"touser\":["

+

sbs.ToString()

+

"],\"mpnews\":{\"media_id\":\""

+

this.lbtuwenmedai_id.Text.ToString()

+

"\"},\"msgtype\":\"mpnews\"}";

string

tuwenres

=

wxs.GetPage(posturl,

postData);

//使用前需藥引用Newtonsoft.json.dll文件

JObject

jsonObj

=

JObject.Parse(tuwenres);

if

(jsonObj["errcode"].ToString().Equals("0"))

{

Session["media_id"]

=

null;

WxMassInfo

wmi

=

new

WxMassInfo();

if

(Session["wmninfo"]

!=

null)

{

WxMpNewsInfo

wmninfo

=

Session["wmninfo"]

as

WxMpNewsInfo;

wmi.title

=

wmninfo.title.ToString();

wmi.contents

=

wmninfo.contents.ToString();

wmi.ImageUrl

=

wmninfo.ImageUrl.ToString();

wmi.type

=

"圖文";

if

(this.DDLMassType.SelectedValue.ToString().Equals("0"))

{

wmi.massObject

=

this.DDLMassType.SelectedItem.Text.ToString();

}

else

{

wmi.massObject

=

this.DDLGroupList.SelectedItem.Text.ToString();

}

wmi.massStatus

=

"成功";//群發(fā)成功之后返回的狀態(tài)碼

wmi.massMessageID

=

jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID

wmi.massDate

=

System.DateTime.Now.ToString();

int

num

=

wms.AddWxMassInfo(wmi);

if

(num

>

0)

{

Session["wmninfo"]

=

null;

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('群發(fā)任務(wù)已提交成功?。。?shù)據(jù)已保存!');location='WxMassManage.aspx';",

true);

return;

}

else

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('群發(fā)任務(wù)已提交成功!??!數(shù)據(jù)保存失敗!');",

true);

return;

}

}

else

{

wmi.title

=

"";

wmi.contents

=

"";

wmi.ImageUrl

=

"";

wmi.type

=

"圖文";

if

(this.DDLMassType.SelectedValue.ToString().Equals("0"))

{

wmi.massObject

=

this.DDLMassType.SelectedItem.Text.ToString();

}

else

{

wmi.massObject

=

this.DDLGroupList.SelectedItem.Text.ToString();

}

wmi.massStatus

=

"成功";//群發(fā)成功之后返回的狀態(tài)碼

wmi.massMessageID

=

jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID

wmi.massDate

=

System.DateTime.Now.ToString();

int

num

=

wms.AddWxMassInfo(wmi);

if

(num

>

0)

{

Session["wmninfo"]

=

null;

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('群發(fā)任務(wù)已提交成功?。?!圖文部分?jǐn)?shù)據(jù)已保存!');location='WxMassManage.aspx';",

true);

return;

}

else

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)保存失??!');",

true);

return;

}

}

}

else

{

ScriptManager.RegisterClientScriptBlock(this.Page,

this.GetType(),

"",

"alert('群發(fā)任務(wù)提交失?。。?);",

true);

return;

}

}

else

{

//根據(jù)分組進(jìn)行群發(fā),訂閱號和服務(wù)號認(rèn)證后均可用

string

group_id

=

this.DDLGroupList.SelectedValue.ToString();

WeiXinServer

wxs

=

new

WeiXinServer();

///從緩存讀取accesstoken

string

Access_token

=

Cache["Access_token"]

as

string;

if

(Access_token

==

null)

{

//如果為空,重新獲取

Access_token

=

wxs.GetAccessToken();

//設(shè)置緩存的數(shù)據(jù)7000秒后過期

Cache.Insert("Access_token",

Access_token,

null,

DateTime.Now.AddSeconds(7000),

System.Web.Caching.Cache.NoSlidingExpiration);

}

string

Access_tokento

=

Access_token.Substring(17,

Access_token.Length

-

37);

string

posturl

=

"/cgi-bin/message/mass/send?access_token="

+

Access_tokento;

///群發(fā)POST數(shù)據(jù)示例如下:

//

{

//

"filter":{

//

"is_to_all":false

//

"group_id":"2"

//

},

//

"mpnews":{

//

"media_id":"123dsdajkasd231jhksad"

//

},

//

"msgtype":"mpnews"

//}

string

postData

=

"{\"filter\":{\"is_to_all\":\"false\"\"group_id\":\""+group_id+

"\"},\"mpnews\":{\"media_id\":\""

+

this.lbtuwenmedai_id.Text.ToString()

+

"\"},\"msgtype\":\"mpnews\"}";

string

tuwenres

=

wxs.GetPage(posturl,

postData);

//使用前需藥引用Newtonsoft.json.dll文件

JObject

jsonObj

=

JObject.Parse(tuwenres);

if

(jsonObj["errcode"].ToString().Equals("0"))

{

Session["media_id"]

=

null;

WxMassInfo

wmi

=

new

WxMassInfo();

if

(Session["wmninfo"]

!=

null)

{

WxMpNewsInfo

wmninfo

=

Session["wmninfo"]

as

WxMpNewsInfo;

wmi.title

=

wmninfo.title.ToString();

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論