做过Symbian 推送通知服务程序的兄弟帮个忙啊!谢谢了!!
本帖最后由 zhujian_1986 于 2011-10-17 16:30:43 编辑 Nokia Symbian Notifications Service API
大体的需求,写一个访问Nokia Notification Service API这个HTTP REST接口的程序,然后向手机客户端应用发通知消息。目前官网上有Java,Python,Perl三种语言的示例,但是我这里需要用.Net来实现。目前针对Java版本,写了一个.Net版本,但是目前存在些问题,因此想请教下各位大虾。
先说下Call这个API需要的参数信息:
service_id:服务ID
service_secret:服务密码
payload:通知的内容
nid:通知ID
jid:用户ID
application_id:目标应用的ID
目前我们是通过nid来Push Notification,因此不需要用到jid与application_id。
所请求的连接是一个https: https://alpha.one.ovi.com/nnapi/1.0/nid/{Notification ID}
例如: https://alpha.one.ovi.com/nnapi/1.0/nid/5JnYUYiBqs3PH2AeeIqqBBAoYaT22vwtjw4OCAQqm0D%2btXCbl989ke0PKUp5C1NnJsYJiSHJzvE51o%2btxwJXhOwD4stUtTe%2ff2kT%2bh%2f%2f4FW9Htu%2fJFMsN2fGvWZ41Ffr
现在的关键是sendHttpPost这个方法:
以下是官方Java示例:
/*** Executes the HTTP POST request to the Notifications API.
* @param headerParameters header parameter for the POST request.
* @param requestUri uri for the request.
*/
private void sendHttpPost(List<NameValuePair> headerParameters, String requestUri) {
//Create the HTTP POST Request
HttpPost httpPost = new HttpPost(requestUri.toString());
try {
httpPost.setEntity(new UrlEncodedFormEntity(headerParameters, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Encoding error while converting HTTP parameters to UTF-8.", e);
}
//Push the notification with HTTPS client
try {
HttpResponse response = client.execute(httpPost);
System.out.println("HTTP Response from Notifications API: " + response.getStatusLine().getStatusCode());
if(response.getEntity() != null) {
response.getEntity().consumeContent();
}
} catch (IOException e) {
throw new RuntimeException("HTTP POST Request to " + requestUri.toString() + " failed with error: " + e.getMessage());
}
}
目前.Net版本是这么写的:
public string sendHttpPost(List<KeyValuePair<string, string>> headerParameters, String requestUri)
{
string results = string.Empty;
StringBuilder paraString = new StringBuilder();
if (!(headerParameters == null || headerParameters.Count == 0))
{
int i = 0;
foreach (var item in headerParameters)
{
if (i > 0)
paraString.AppendFormat("&{0}={1}", item.Key, item.Value);
else
paraString.AppendFormat("{0}={1}", item.Key, item.Value);
i += 1;
}
}
Encoding UTF8encoding = Encoding.GetEncoding("utf-8");
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
request.Method = "POST";
request.CookieContainer = new CookieContainer();
request.Timeout = 30000;
request.Credentials = new NetworkCredential(this.ServiceId, this.ServiceSecret);
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/x-www-form-urlencoded";
request.ProtocolVersion = HttpVersion.Version10;
byte[] buffer = UTF8encoding.GetBytes(paraString.ToString());
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), UTF8encoding))
{
results= reader.ReadToEnd();
}
}
catch (Exception e)
{
throw new UriFormatException("HTTP POST Request to " + requestUri + " failed with error: " + e.Message);
}
return results;
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors errors)
{
return true;
}
目前的问题是,执行该方法时.net会报400的服务器响应错误信息,如果哪位同学有过这方面的经验希望能帮帮俺,解决这个问题,非常感谢!
[最优解释]
no answer