c# webapi调用友盟u-push接口推送消息
来源:互联网 发布:gta5没有网络能玩吗 编辑:程序博客网 时间:2024/06/05 04:53
try
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
string timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
string appkey = string.Empty;
string app_master_secret = string.Empty;
var param = new object();
if (model.platform == "android")
{
appkey = System.Configuration.ConfigurationManager.AppSettings["android_appkey"];
app_master_secret = System.Configuration.ConfigurationManager.AppSettings["android_App_Master_Secret"];
param = new
{
appkey = appkey,
timestamp = timestamp,
type = model.type,
device_tokens = model.device_tokens == null ? "" : model.device_tokens,
payload = new
{
body = new
{
ticker = model.ticker,
title = model.title,
text = model.text,
after_open = model.after_open,
url = model.url == null ? "" : model.url,
activity = model.activity == null ? "" : model.activity,
},
display_type = "notification" //消息类型:通知
},
policy = new
{
start_time = model.start_time == null ? "" : model.start_time
}
};
}
else if (model.platform == "ios")
{
appkey = System.Configuration.ConfigurationManager.AppSettings["ios_appkey"];
app_master_secret = System.Configuration.ConfigurationManager.AppSettings["ios_App_Master_Secret"];
param = new
{
appkey = appkey,
timestamp = timestamp,
type = model.type,
device_tokens = model.device_tokens == null ? "" : model.device_tokens,
payload = new
{
aps = new // 必填 严格按照APNs定义来填写
{
alert = model.title // 必填
}
},
policy = new
{
start_time = model.start_time == null ? "" : model.start_time
}
};
}
//string app_master_secret = "nxus2ozijipcgwjr0htcepyno6y4piaz";
string url = System.Configuration.ConfigurationManager.AppSettings["umeng_url"];
var requestJson = JsonConvert.SerializeObject(param);
string mysign = getMD5Hash("POST" + url + requestJson + app_master_secret);
url = url + "?sign=" + mysign;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
byte[] bs = Encoding.UTF8.GetBytes(requestJson);
request.ContentLength = bs.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpStatusCode statusCode = response.StatusCode;
if (statusCode == System.Net.HttpStatusCode.OK)
{
t_pushinfo.pushtime = DateTime.Now;
t_pushinfo.status = "成功";
entity.T_PushInfo.Add(t_pushinfo);
entity.SaveChanges();
ModelState.AddModelError("push", "发送成功!");
return View();
}
else
{
t_pushinfo.pushtime = DateTime.Now;
t_pushinfo.status = "失败";
entity.T_PushInfo.Add(t_pushinfo);
entity.SaveChanges();
ModelState.AddModelError("push", "发送失败!");
return View();
}
}
catch (WebException e)
{
string msg = string.Empty;
if (e.Status == WebExceptionStatus.ProtocolError)
{
Stream myResponseStream = ((HttpWebResponse)e.Response).GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
msg = "异常:" + retString + e.Response + ",联系工作人员";
}
t_pushinfo.pushtime = DateTime.Now;
t_pushinfo.status = "失败";
entity.T_PushInfo.Add(t_pushinfo);
entity.SaveChanges();
ModelState.AddModelError("push", msg);
return View();
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
string timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
string appkey = string.Empty;
string app_master_secret = string.Empty;
var param = new object();
if (model.platform == "android")
{
appkey = System.Configuration.ConfigurationManager.AppSettings["android_appkey"];
app_master_secret = System.Configuration.ConfigurationManager.AppSettings["android_App_Master_Secret"];
param = new
{
appkey = appkey,
timestamp = timestamp,
type = model.type,
device_tokens = model.device_tokens == null ? "" : model.device_tokens,
payload = new
{
body = new
{
ticker = model.ticker,
title = model.title,
text = model.text,
after_open = model.after_open,
url = model.url == null ? "" : model.url,
activity = model.activity == null ? "" : model.activity,
},
display_type = "notification" //消息类型:通知
},
policy = new
{
start_time = model.start_time == null ? "" : model.start_time
}
};
}
else if (model.platform == "ios")
{
appkey = System.Configuration.ConfigurationManager.AppSettings["ios_appkey"];
app_master_secret = System.Configuration.ConfigurationManager.AppSettings["ios_App_Master_Secret"];
param = new
{
appkey = appkey,
timestamp = timestamp,
type = model.type,
device_tokens = model.device_tokens == null ? "" : model.device_tokens,
payload = new
{
aps = new // 必填 严格按照APNs定义来填写
{
alert = model.title // 必填
}
},
policy = new
{
start_time = model.start_time == null ? "" : model.start_time
}
};
}
//string app_master_secret = "nxus2ozijipcgwjr0htcepyno6y4piaz";
string url = System.Configuration.ConfigurationManager.AppSettings["umeng_url"];
var requestJson = JsonConvert.SerializeObject(param);
string mysign = getMD5Hash("POST" + url + requestJson + app_master_secret);
url = url + "?sign=" + mysign;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
byte[] bs = Encoding.UTF8.GetBytes(requestJson);
request.ContentLength = bs.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpStatusCode statusCode = response.StatusCode;
if (statusCode == System.Net.HttpStatusCode.OK)
{
t_pushinfo.pushtime = DateTime.Now;
t_pushinfo.status = "成功";
entity.T_PushInfo.Add(t_pushinfo);
entity.SaveChanges();
ModelState.AddModelError("push", "发送成功!");
return View();
}
else
{
t_pushinfo.pushtime = DateTime.Now;
t_pushinfo.status = "失败";
entity.T_PushInfo.Add(t_pushinfo);
entity.SaveChanges();
ModelState.AddModelError("push", "发送失败!");
return View();
}
}
catch (WebException e)
{
string msg = string.Empty;
if (e.Status == WebExceptionStatus.ProtocolError)
{
Stream myResponseStream = ((HttpWebResponse)e.Response).GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
msg = "异常:" + retString + e.Response + ",联系工作人员";
}
t_pushinfo.pushtime = DateTime.Now;
t_pushinfo.status = "失败";
entity.T_PushInfo.Add(t_pushinfo);
entity.SaveChanges();
ModelState.AddModelError("push", msg);
return View();
}
public static String getMD5Hash(String str)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
StringBuilder strReturn = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
strReturn.Append(Convert.ToString(result[i], 16).PadLeft(2, '0'));
}
return strReturn.ToString().PadLeft(32, '0');
}
0 0
- c# webapi调用友盟u-push接口推送消息
- 友盟消息推送u-push
- Android Push消息推送
- 消息推送技术Push
- Android Push消息推送
- c# 【MVC】WebApi通过HttpClient来调用Web Api接口
- Java服务端调用百度推送接口推送消息
- c# 调用webApi
- java 调用 C# webapi
- C# 调用WebApi
- C#调用webApi方法
- 调用极光推送接口C#代码
- iphone push notification 消息推送
- iphone push notification 消息推送
- 消息推送(Server Push)
- phonegap push-消息推送实现
- 消息推送(push原理)
- Android开发 消息推送 Push
- Delphi类型转换
- 关于为什么jar包放入classpath为什么启动tomcat还报找不到类的错误
- WinForm窗体的StartPosition属性
- C#中对元组理解Tuple<T,T>
- ElasticSearch学习二:构建集群与简单搜索实例应用
- c# webapi调用友盟u-push接口推送消息
- TOEFL speaking——phrasal verbs
- 关于使用mybatis几个功能的实现
- SDUTACM ASCII码排序
- nbd修改img文件内容
- 《Java编程思想》--面向对象和JVM基础
- 微信不能分享/登录的问题之一
- 创建Kafka0.8.2生产者与消费者
- Mpeg-2的同步及时间恢复