diff --git a/B3WeChat/Entities/ErrorObject.cs b/B3WeChat/Entities/ErrorObject.cs
index cdf0a97..a8eb66a 100644
--- a/B3WeChat/Entities/ErrorObject.cs
+++ b/B3WeChat/Entities/ErrorObject.cs
@@ -5,7 +5,7 @@ using System.Web;
namespace BWP.B3WeChat.Entities
{
- public class ErrorObject
+ public class WeChatResponseBase
{
public int errcode { get; set; }
public string errmsg { get; set; }
diff --git a/B3WeChat/Entities/SendTemplateMessageResult.cs b/B3WeChat/Entities/SendTemplateMessageResult.cs
index 509609a..82dedcc 100644
--- a/B3WeChat/Entities/SendTemplateMessageResult.cs
+++ b/B3WeChat/Entities/SendTemplateMessageResult.cs
@@ -8,19 +8,9 @@ namespace BWP.B3WeChat.Entities
///
/// 发送模板信息返回结果
///
- public class SendTemplateMessageResult
+ public class SendTemplateMessageResult:WeChatResponseBase
{
- public int errcode { get; set; }
- public string errmsg { get; set; }
public int msgid { get; set; }
- public bool IsError
- {
- get
- {
- return errcode > 0;
- }
- }
-
}
}
\ No newline at end of file
diff --git a/B3WeChat/Entities/TokenObject.cs b/B3WeChat/Entities/TokenObject.cs
index 5e3052b..fe36787 100644
--- a/B3WeChat/Entities/TokenObject.cs
+++ b/B3WeChat/Entities/TokenObject.cs
@@ -8,7 +8,7 @@ namespace BWP.B3WeChat.Entities
///
/// Token对象
///
- public class TokenObject:ErrorObject
+ public class TokenObject:WeChatResponseBase
{
public string access_token { get; set; }
public string expires_in { get; set; }
diff --git a/B3WeChat/Entities/WeiChartServerList.cs b/B3WeChat/Entities/WeiChartServerList.cs
index 837be9e..b10295d 100644
--- a/B3WeChat/Entities/WeiChartServerList.cs
+++ b/B3WeChat/Entities/WeiChartServerList.cs
@@ -5,7 +5,7 @@ using System.Web;
namespace BWP.B3WeChat.Entities
{
- public class WeiChartServerList
+ public class WeiChartServerList:WeChatResponseBase
{
public List ip_list { get; set; }
}
diff --git a/B3WeChat/Utils/InOutMessageUtil.cs b/B3WeChat/Utils/InOutMessageUtil.cs
index c69d4f0..557a32e 100644
--- a/B3WeChat/Utils/InOutMessageUtil.cs
+++ b/B3WeChat/Utils/InOutMessageUtil.cs
@@ -44,58 +44,68 @@ namespace BWP.B3WeChat.Utils
public static B3WeChatConfig config = new B3WeChatConfig();
- public static string GetToken()
+
+
+ static JavaScriptSerializer jsonHelper = new JavaScriptSerializer();
+
+ static T GetRequest(string uriStr) where T : WeChatResponseBase
{
- string token = string.Empty;
- string uriStr = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", config.AppID.Value, config.AppSecret.Value);
+ var responseBody = GetRequestString(uriStr);
+ T obj = jsonHelper.Deserialize(responseBody);
+ if (obj.IsError)
+ {
+ throw new Exception(string.Format("{0}:{1}", obj.errcode, obj.errmsg));
+ }
+ return obj;
+ }
+ private static string GetRequestString(string url)
+ {
WebClient client = new WebClient();
- string responseBody = string.Empty;
- try
+ var data = client.DownloadData(url);
+ return Encoding.UTF8.GetString(data);
+ }
+
+ static T PostRequest(string url, string json) where T : WeChatResponseBase
+ {
+ byte[] requestBuffer = Encoding.UTF8.GetBytes(json);
+ HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
+ webRequest.Method = "POST";
+ webRequest.ContentType = "application/x-www-form-urlencoded";
+ webRequest.ContentLength = requestBuffer.Length;
+ using (var requestStream = webRequest.GetRequestStream())
{
- byte[] bytes = client.DownloadData(uriStr);
- responseBody = Encoding.UTF8.GetString(bytes);
- JavaScriptSerializer jsonHelper = new JavaScriptSerializer();
- TokenObject obj = jsonHelper.Deserialize(responseBody);
- if (obj.IsError)
- {
- throw new Exception(string.Format("{0}:{1}", obj.errcode, obj.errmsg));
- }
- if (obj != null)
- {
- token = obj.access_token;
- }
+ requestStream.Write(requestBuffer, 0, requestBuffer.Length);
}
- catch (Exception e)
+ HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
+ var data = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
+ var obj = jsonHelper.Deserialize(data);
+ if (obj.IsError)
{
- throw e;
+ throw new Exception(string.Format("{0}:{1}", obj.errcode, obj.errmsg));
}
- return token;
+ return obj;
+ }
+
+
+ public static string GetToken()
+ {
+ string token = string.Empty;
+ string url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", config.AppID.Value, config.AppSecret.Value);
+ var res = GetRequest(url);
+ return res.access_token;
}
public static WeiChartServerList GetIPList()
{
WeiChartServerList result = new WeiChartServerList();
string uriStr = string.Format("https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token={0}", TOKEN);
- WebClient client = new WebClient();
- try
- {
- byte[] bytes = client.DownloadData(uriStr);
- string responseBody = Encoding.UTF8.GetString(bytes);
- JavaScriptSerializer jsonHelper = new JavaScriptSerializer();
- result = jsonHelper.Deserialize(responseBody);
- }
- catch (Exception e)
- {
- throw e;
- }
- return result;
+ return GetRequest(uriStr);
}
public static SendTemplateMessageResult SendTemplateMessage(string openID, string templateID, Dictionary dic, string url = "")
{
string uriStr = string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", TOKEN);
-
JavaScriptSerializer jsonHelper = new JavaScriptSerializer();
SendTemplateMessageResult result = new SendTemplateMessageResult();
MeassageBody body = new MeassageBody();
@@ -104,34 +114,30 @@ namespace BWP.B3WeChat.Utils
body.url = url;
body.data = dic;
string postData = jsonHelper.Serialize(body);
- string data = string.Empty;
- try
- {
- byte[] byteArray = Encoding.UTF8.GetBytes(postData);
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(uriStr));
- webRequest.Method = "post";
- webRequest.ContentType = "application/x-www-form-urlencoded";
- webRequest.ContentLength = byteArray.Length;
- System.IO.Stream newStream = webRequest.GetRequestStream();
- newStream.Write(byteArray, 0, byteArray.Length);
- newStream.Close();
- HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
- data = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
- result = jsonHelper.Deserialize(data);
- if (result.IsError)
- {
- throw new Exception(string.Format("{0}:{1}", result.errcode, result.errmsg));
- }
- }
- catch (Exception e)
- {
- throw e;
- }
- return result;
+ return PostRequest(uriStr, postData);
+ }
+
+ public static void SetCustomMenu(string menuJson)
+ {
+ var url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", TOKEN);
+ PostRequest(url, menuJson);
+ }
+
+ public static string GetCustomMenu()
+ {
+ var url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", TOKEN);
+ return GetRequestString(url);
+ }
+
+ public static void DelteCustomMenu()
+ {
+ var url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}",TOKEN);
+ GetRequest(url);
}
public static List GetOpenIDList()
{
+
string openid = string.Empty;
string uriStr = string.Empty;
List lstOpenID = new List();