【移動(dòng)應(yīng)用開發(fā)技術(shù)】C#微信公眾平臺(tái)開發(fā)之高級(jí)群發(fā)接口的示例分析_第1頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】C#微信公眾平臺(tái)開發(fā)之高級(jí)群發(fā)接口的示例分析_第2頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】C#微信公眾平臺(tái)開發(fā)之高級(jí)群發(fā)接口的示例分析_第3頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】C#微信公眾平臺(tái)開發(fā)之高級(jí)群發(fā)接口的示例分析_第4頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】C#微信公眾平臺(tái)開發(fā)之高級(jí)群發(fā)接口的示例分析_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

【移動(dòng)應(yīng)用開發(fā)技術(shù)】C#微信公眾平臺(tái)開發(fā)之高級(jí)群發(fā)接口的示例分析

一、為了實(shí)現(xiàn)高級(jí)群發(fā)功能,需要解決的問題二、實(shí)現(xiàn)步驟,以根據(jù)OpenID列表群發(fā)圖文消息為例/upload/information/20201208/260/14855.jpgDataTable

dt

=

ImgItemDal.GetImgItemTable(loginUser.OrgID,

data);///

<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);

//請(qǐng)求頭部信息

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ā)送請(qǐng)求并獲取相應(yīng)回應(yīng)數(shù)據(jù)

HttpWebResponse

response

=

request.GetResponse()

as

HttpWebResponse;

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

Stream

instream

=

response.GetResponseStream();

StreamReader

sr

=

new

StreamReader(instream,

Encoding.UTF8);

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

string

content

=

sr.ReadToEnd();

return

content;

}///

<summary>

///

上傳媒體返回媒體ID

///

</summary>

public

static

string

UploadMedia(string

access_token,

string

type,

string

path)

{

//

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

string

url

=

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

access_token,

type);

return

HttpRequestUtil.HttpUploadFile(url,

path);

}string

msg

=

WXApi.UploadMedia(access_token,

"image",

path);

//

上圖片返回媒體IDstring

media_id

=

Tools.GetJsonValue(msg,

"media_id");string

path

=

MapPath(data);///

<summary>

///

拼接圖文消息素材Json字符串

///

</summary>

public

static

string

GetArticlesJsonStr(PageBase

page,

string

access_token,

DataTable

dt)

{

StringBuilder

sbArticlesJson

=

new

StringBuilder();

sbArticlesJson.Append("{\"articles\":[");

int

i

=

0;

foreach

(DataRow

dr

in

dt.Rows)

{

string

path

=

page.MapPath(dr["ImgUrl"].ToString());

if

(!File.Exists(path))

{

return

"{\"code\":0,\"msg\":\"要發(fā)送的圖片不存在\"}";

}

string

msg

=

WXApi.UploadMedia(access_token,

"image",

path);

//

上圖片返回媒體ID

string

media_id

=

Tools.GetJsonValue(msg,

"media_id");

sbArticlesJson.Append("{");

sbArticlesJson.Append("\"thumb_media_id\":\""

+

media_id

+

"\",");

sbArticlesJson.Append("\"author\":\""

+

dr["Author"].ToString()

+

"\",");

sbArticlesJson.Append("\"title\":\""

+

dr["Title"].ToString()

+

"\",");

sbArticlesJson.Append("\"content_source_url\":\""

+

dr["TextUrl"].ToString()

+

"\",");

sbArticlesJson.Append("\"content\":\""

+

dr["Content"].ToString()

+

"\",");

sbArticlesJson.Append("\"digest\":\""

+

dr["Content"].ToString()

+

"\",");

if

(i

==

dt.Rows.Count

-

1)

{

sbArticlesJson.Append("\"show_cover_pic\":\"1\"}");

}

else

{

sbArticlesJson.Append("\"show_cover_pic\":\"1\"},");

}

i++;

}

sbArticlesJson.Append("]}");

return

sbArticlesJson.ToString();

}///

<summary>

///

請(qǐng)求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ā)送請(qǐng)求并獲取相應(yīng)回應(yīng)數(shù)據(jù)

HttpWebResponse

response

=

request.GetResponse()

as

HttpWebResponse;

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

Stream

instream

=

response.GetResponseStream();

StreamReader

sr

=

new

StreamReader(instream,

Encoding.UTF8);

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

string

content

=

sr.ReadToEnd();

return

content;

}///

<summary>

///

上傳圖文消息素材返回media_id

///

</summary>

public

static

string

UploadNews(string

access_token,

string

postData)

{

return

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

access_token),

postData);

}string

articlesJson

=

ImgItemDal.GetArticlesJsonStr(this,

access_token,

dt);

string

newsMsg

=

WXApi.UploadNews(access_token,

articlesJson);

string

newsid

=

Tools.GetJsonValue(newsMsg,

"media_id");///

<summary>

///

獲取關(guān)注者OpenID集合

///

</summary>

public

static

List<string>

GetOpenIDs(string

access_token)

{

List<string>

result

=

new

List<string>();

List<string>

openidList

=

GetOpenIDs(access_token,

null);

result.AddRange(openidList);

while

(openidList.Count

>

0)

{

openidList

=

GetOpenIDs(access_token,

openidList[openidList.Count

-

1]);

result.AddRange(openidList);

}

return

result;

}

///

<summary>

///

獲取關(guān)注者OpenID集合

///

</summary>

public

static

List<string>

GetOpenIDs(string

access_token,

string

next_openid)

{

//

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

string

url

=

string.Format("/cgi-bin/user/get?access_token={0}&next_openid={1}",

access_token,

string.IsNullOrWhiteSpace(next_openid)

?

""

:

next_openid);

string

returnStr

=

HttpRequestUtil.RequestUrl(url);

int

count

=

int.Parse(Tools.GetJsonValue(returnStr,

"count"));

if

(count

>

0)

{

string

startFlg

=

"\"openid\":[";

int

start

=

returnStr.IndexOf(startFlg)

+

startFlg.Length;

int

end

=

returnStr.IndexOf("]",

start);

string

openids

=

returnStr.Substring(start,

end

-

start).Replace("\"",

"");

return

openids.Split(',').ToList<string>();

}

else

{

return

new

List<string>();

}

}///

<summary>

///

圖文消息json

///

</summary>

public

static

string

CreateNewsJson(string

media_id,

List<string>

openidList)

{

StringBuilder

sb

=

new

StringBuilder();

sb.Append("{\"touser\":[");

sb.Append(string.Join(",",

openidList.ConvertAll<string>(a

=>

"\""

+

a

+

"\"").ToArray()));

sb.Append("],");

sb.Append("\"msgtype\":\"mpnews\",");

sb.Append("\"mpnews\":{\"media_id\":\""

+

media_id

+

"\"}");

sb.Append("}");

return

sb.ToString();

}resultMsg

=

WXApi.Send(access_token,

WXMsgUtil.CreateNewsJson(newsid,

openidList));///

<summary>

///

根據(jù)OpenID列表群發(fā)

///

</summary>

public

static

string

Send(string

access_token,

string

postData)

{

return

HttpRequestUtil.PostUrl(string.Format("/cgi-bin/message/mass/send?access_token={0}",

access_token),

postData);

}///

<summary>

///

群發(fā)

///

</summary>

public

string

Send()

{

string

type

=

Request["type"];

string

data

=

Request["data"];

string

access_token

=

AdminUtil.GetAccessToken(this);

//獲取access_token

List<string>

openidList

=

WXApi.GetOpenIDs(access_token);

//獲取關(guān)注者OpenID列表

UserInfo

loginUser

=

AdminUtil.GetLoginUser(this);

//當(dāng)前登錄用戶

string

resultMsg

=

null;

//發(fā)送文本

if

(type

==

"1")

{

resultMsg

=

WXApi.Send(access_token,

WXMsgUtil.CreateTextJson(data,

openidList));

}

//發(fā)送圖片

if

(type

==

"2")

{

string

path

=

MapPath(data);

if

(!File.Exists(path))

{

return

"{\"code\":0,\"msg\":\"要發(fā)送的圖片不存在\"}";

}

string

msg

=

WXApi.UploadMedia(access_token,

"image",

path);

string

media_id

=

Tools.GetJsonValue(msg,

"media_id");

resultMsg

=

WXApi.Send(access_token,

WXMsgUtil.CreateImageJson(media_id,

openidList));

}

//發(fā)送圖文消息

if

(type

==

"3")

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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ǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論