Java 我有一个在没有应用服务器的情况下访问GCM服务器的示例源代码,我尝试转换winapi源代码
我开发了一个android应用程序,带有一个与winapi一起工作的桌面程序,我尝试在没有应用程序服务器的情况下访问google GCM服务器。我发现了一个很好的例子,效果很好。但我需要通过winapi应用程序访问GCM服务器。我还开发了android应用程序,这有助于我理解如何使用这个示例,但我无法转换为winapi java,winapi是如此不同,我从未使用过类似winapi http连接的东西,如Get、Post …谁来帮帮我Java 我有一个在没有应用服务器的情况下访问GCM服务器的示例源代码,我尝试转换winapi源代码,java,android,winapi,google-cloud-messaging,Java,Android,Winapi,Google Cloud Messaging,我开发了一个android应用程序,带有一个与winapi一起工作的桌面程序,我尝试在没有应用程序服务器的情况下访问google GCM服务器。我发现了一个很好的例子,效果很好。但我需要通过winapi应用程序访问GCM服务器。我还开发了android应用程序,这有助于我理解如何使用这个示例,但我无法转换为winapi java,winapi是如此不同,我从未使用过类似winapi http连接的东西,如Get、Post …谁来帮帮我 import java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
public class TestSender {
static String gcmURL = "https://android.googleapis.com/gcm/send";
static String gcmRegID = "device register ID";
static String gcmAuthToken = "GCM sever Access Key";
public static void main(String[] args) {
try {
sender(gcmRegID, gcmAuthToken, "Test MSG");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void sender(String regId, String authToken, String msg)
throws Exception {
StringBuffer postDataBuilder = new StringBuffer();
postDataBuilder.append("registration_id=" + regId); // register ID
postDataBuilder.append("&collapse_key=1");
postDataBuilder.append("&delay_while_idle=1");
postDataBuilder.append("&data.msg=" + URLEncoder.encode(msg, "UTF-8"));
byte[] postData = postDataBuilder.toString().getBytes("UTF8");
URL url = new URL(gcmURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
HttpsURLConnection
.setDefaultHostnameVerifier(new FakeHostnameVerifier());
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-formurlencoded;charset=UTF-8");
conn.setRequestProperty("Content-Length",
Integer.toString(postData.length));
conn.setRequestProperty("Authorization", "key=" + gcmAuthToken);
OutputStream out = conn.getOutputStream();
out.write(postData);
out.close();
conn.getInputStream();
System.out.println("postData : " + postDataBuilder.toString());
String reponseLine = new BufferedReader(new InputStreamReader(
conn.getInputStream())).readLine();
System.out.println("responseLine : " + reponseLine);
}
private static class FakeHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
}
你的问题我不清楚。在windows中,如果要访问Internet(http),可以使用WinInet或WinHttp api集