package com.windowphone.text;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
public class HttpPost {
private String xml;
private String url;
public HttpPost(String url, String xml) {
this.xml = xml;
this.url = url;
}
private void Send() {
HttpURLConnection con = null;
URL url = null;
try {
url = new URL(this.url);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
//全球唯一的ID,类型:eb84a429-1ac6-46e2-b3f3-51929fd17648
String guid = UUID.randomUUID().toString();
con.setRequestProperty("X-MessageID",guid);
con.setRequestProperty("Content-Type","text/xml;charset=utf-8");
//①Raw Notification模式
//3:立刻发送 13:等待450秒发送 23:等待900秒发送
con.setRequestProperty("X-NotificationClass", "3");
//②Toast Notification模式
//2:立刻发送 12:等待450秒发送 22:等待900秒发送
// con.setRequestProperty("X-WindowsPhone-Target", "toast");
// con.setRequestProperty("X-NotificationClass", "2");
//③Tile Notification模式
//1:立刻发送 11:等待450秒发送 21:等待900秒发送
// con.setRequestProperty("X-WindowsPhone-Target", "token");
// con.setRequestProperty("X-NotificationClass", "1");
OutputStream out = con.getOutputStream();
//在此要特别的小心,发送比特流,要把获取字节码改为utf-8,不然中文会乱码
out.write(this.xml.getBytes("utf-8"));
out.flush();
//输出微软服务器response的情况,正常输出OK
System.out.println("response: "+con.getResponseMessage());
out.close();
con.disconnect();
} catch (ConnectException ce) {
} catch (IOException ie) {
} catch (Exception e) {
}
}
public static void main(String[] args) {
//这里直接复制window phone 应用注册微软的Uri
String uri = "http://db3.notify.live.net/throttledthirdparty/01.00/AAGKzo1xh_AfR4Ia6ePTklzoAgAAAAADAQAAAAQUZm52OjIzOEQ2NDJDRkI5MEVFMEQ";
///①Raw Notification模式
String rawMessage = "hitler 林楚金!";
//②Toast Notification模式,固定模式,Text1和Text2两个参数
String toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"wpNotification\">" +
"<wp:Toast>" +
"<wp:Text1>123</wp:Text1>" +
"<wp:Text2>林楚金</wp:Text2>" +
"</wp:Toast>" +
"</wp:Notification>";
//③Tile Notification模式,固定模式,BackgroundImage背景图片,count数量,Title小标题
String tileMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"wpNotification\">" +
"<wp:Tile>" +
"<wp:BackgroundImage>/Images/天晴.jpg</wp:BackgroundImage>" +
"<wp:Count>2</wp:Count>" +
"<wp:Title>fuck 林楚金</wp:Title>" +
"</wp:Tile>" +
"</wp:Notification>";
HttpPost post = new HttpPost(uri,rawMessage);
post.Send();
}
}