来自Google云消息服务器的响应正常,但响应体显示“未识别的IP地址”
本文关键字:响应 显示 识别 IP 未识别的 地址 消息 Google 服务器 来自 | 更新日期: 2023-09-27 18:07:09
来自Google云消息服务器的响应是"OK",但响应正文显示"您从一个无法识别的IP地址浏览。因此代理拒绝访问此站点或页面:"
private static string SendGCMNotification(string apiKey, string postData, string postDataContentType = "application/json")
{
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);
//
// MESSAGE CONTENT
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
//
// CREATE REQUEST
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
Request.Method = "POST";
Request.KeepAlive = false;
Request.ContentType = postDataContentType;
Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
Request.ContentLength = byteArray.Length;
Request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
Stream dataStream = Request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
//
// SEND MESSAGE
try
{
WebResponse Response = Request.GetResponse();
HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
{
var text = "Unauthorized - need new token";
}
else if (!ResponseCode.Equals(HttpStatusCode.OK))
{
var text = "Response from web service isn't OK";
}
StreamReader Reader = new StreamReader(Response.GetResponseStream());
string responseLine = Reader.ReadToEnd();
Reader.Close();
return responseLine;
}
catch (Exception e)
{
}
return "error";
}
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
任何帮助都是非常感激的。提前感谢
请注意实现HTTP连接服务器中的这句话:
如果您的组织有防火墙,限制流量到或您需要将其配置为允许与Internet连接以便您的GCM客户端应用程序接收消息。港口要打开的是:5228、5229和5230。GCM通常只使用5228,但是它有时使用5229和5230。GCM没有提供具体的ip,所以您应该允许防火墙接受所有的传出连接包含在谷歌ASN的IP块中的IP地址15169 .