C#으로 c2dm serverside를 만들 수있는 사람이 있는지 알고 있습니까? 필수 매개 변수 (전자 메일, 암호 등)를 어떻게 추가합니까?C#을 가진 c2dm 서버
2
A
답변
1
내 자신의 질문에 대답하려면 : 가능합니다. 다른 문제가 있지만 다른 이야기가 있습니다 (No auth in c2dm response)
7
내가 만든 기본 프로토 타입 C# 서버의 핵심은 다음과 같습니다.
class C2DMPrototype
{
// Hardcoded for now
private const string RegistrationId = "XXXXXXXXXXX";
private const string GoogleAuthUrl = "https://www.google.com/accounts/ClientLogin";
// TODO : Production code should use https (secure) push and have the correct certificate
private const string GoogleMessageUrl = "http://android.clients.google.com/c2dm/send";
private const string PostWebRequest = "POST";
private const string AuthTokenHeader = "Auth=";
private const string UpdateClientAuth = "Update-Client-Auth";
// Post data parameters
private const string RegistrationIdParam = "registration_id";
private const string CollapseKeyParam = "collapse_key";
private const string DataPayloadParam = "data.payload";
private const string DelayWhileIdleParam = "delay_while_idle";
private string _authTokenString = String.Empty;
private string _updatedAuthTokenString = String.Empty;
private string _message = String.Empty;
public void StartServer()
{
if ((_authTokenString = GetAuthentificationToken()).Equals(String.Empty))
{
Console.ReadLine();
return;
}
while (true)
{
try
{
Console.Write("Message> ");
_message = Console.ReadLine().ToLower().Trim();
SendMessage(_authTokenString, RegistrationId, _message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
}
private static string GetAuthentificationToken()
{
string authTokenString = String.Empty;
try
{
WebRequest request = WebRequest.Create(GoogleAuthUrl);
request.Method = PostWebRequest;
NameValueCollection postFieldNameValue = new NameValueCollection();
postFieldNameValue.Add("Email", "XXXXXXXXXXX");
postFieldNameValue.Add("Passwd", "XXXXXXXXXXX");
postFieldNameValue.Add("accountType", "GOOGLE");
postFieldNameValue.Add("source", "Google-cURL-Example");
postFieldNameValue.Add("service", "ac2dm");
string postData = GetPostStringFrom(postFieldNameValue);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
if (((HttpWebResponse)response).StatusCode.Equals(HttpStatusCode.OK))
{
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
authTokenString = ParseForAuthTokenKey(responseFromServer);
reader.Close();
dataStream.Close();
}
else
{
Console.WriteLine("Response from web service not OK :");
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
}
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("Getting Authentication Failure");
Console.WriteLine(ex.Message);
}
return authTokenString;
}
private static void SendMessage(string authTokenString, string registrationId, string message)
{
//Certeficate was not being accepted for the sercure call
//ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(GoogleMessageUrl);
request.Method = PostWebRequest;
request.KeepAlive = false;
NameValueCollection postFieldNameValue = new NameValueCollection();
postFieldNameValue.Add(RegistrationIdParam, registrationId);
postFieldNameValue.Add(CollapseKeyParam, "0");
postFieldNameValue.Add(DelayWhileIdleParam, "0");
postFieldNameValue.Add(DataPayloadParam, message);
string postData = GetPostStringFrom(postFieldNameValue);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
request.ContentLength = byteArray.Length;
request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + authTokenString);
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
HttpStatusCode responseCode = ((HttpWebResponse)response).StatusCode;
if (responseCode.Equals(HttpStatusCode.Unauthorized) || responseCode.Equals(HttpStatusCode.Forbidden))
{
Console.WriteLine("Unauthorized - need new token");
}
else if (!responseCode.Equals(HttpStatusCode.OK))
{
Console.WriteLine("Response from web service not OK :");
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
}
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseLine = reader.ReadLine();
reader.Close();
}
private static string GetPostStringFrom(NameValueCollection nameValuePair)
{
StringBuilder postString = new StringBuilder();
for (int i = 0; i < nameValuePair.Count; i++)
{
postString.Append(nameValuePair.GetKey(i));
postString.Append("=");
postString.Append(Uri.EscapeDataString(nameValuePair[i]));
if (i + 1 != nameValuePair.Count)
{
postString.Append("&");
}
}
return postString.ToString();
}
private static string ParseForAuthTokenKey(string webResponse)
{
string tokenKey = String.Empty;
if (webResponse.Contains(AuthTokenHeader))
{
tokenKey = webResponse.Substring(webResponse.IndexOf(AuthTokenHeader) + AuthTokenHeader.Length);
if (tokenKey.Contains(Environment.NewLine))
{
tokenKey.Substring(0, tokenKey.IndexOf(Environment.NewLine));
}
}
return tokenKey.Trim();
}
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
// Return "true" to force the certificate to be accepted.
return true;
}
}
0
네, 여기에는 기성품 서버가 있습니다. C2DM-Sharp의 소스 코드는 https://github.com/Redth/C2DM-Sharp
관련 문제
- 1. 안드로이드 C2DM 서버 사이드
- 2. Google C2DM 서버
- 3. Android C2DM 서버 메시지 보내기
- 4. PHP 401이 포함 된 C2DM 서버 오류
- 5. C2DM 서버 쪽 ClientLogin with Java 문제
- 6. C/C을 찾고 ++ 언어와 표준 라이브러리 사양
- 7. 목적 적합성에 대한 C2DM 질문
- 8. Android c2dm 이메일 주소 문제
- 9. Android-x86에서 C2DM 사용
- 10. android c2dm 프로그래밍 예
- 11. android에서 c2dm 구현
- 12. C++의 C2DM NDK
- 13. C2DM 등록 재시도
- 14. C2DM : C2DMReceiver는 어디서 인스턴스화됩니까?
- 15. C2DM 메시지 보내기
- 16. 안드로이드에서 C2DM 구현
- 17. C2DM 관련 프로덕션 과정
- 18. Android C2DM 슬리핑 전화기
- 19. Android C2DM 등록 실패
- 20. c2dm 용 Android 401
- 21. Google의 C2DM 게임
- 22. C2DM 용 방화벽 설정
- 23. Android C2DM 전송 문제
- 24. 오징어 서버 - 다른 도메인을 가진 다중 originservers
- 25. Android : C2DM 잘못된 등록 오류
- 26. C2DM : C2D_MESSAGE 권한을 사용하는 방법?
- 27. C2DM - onMessage, 결과와 상호 작용
- 28. 안드로이드 C2DM 메시지 수신 확인
- 29. Android C2DM 알림 상단 구석?
- 30. C2DM 메시지 수신 안 함
asp.net 페이지에서 구현하는 방법을 참조하십시오. – eyalb