Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8864081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:08:54+00:00 2026-06-14T16:08:54+00:00

i did a simple Web Service in Java and i deployed it in JBOSS

  • 0

i did a simple Web Service in Java and i deployed it in JBOSS 5.1.
This WS handles C2DM service for sending a notify message to an Android phone. I set all like i red in google c2dm api, and, first of all, i sign up for accessing to c2dm service. In this case, all works well.

Now i have to do the same in .NET on IIS7. Some clarification about the .Net code:

  • setRegId() and pushMessage() method are available by WebService.

  • handShakeRegId() is simply called by setRegId() after String “reg_id” and “device_id” are setted

  • all code commented are my try for solving problem, but all was useless

Thats the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace WebService1
{

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

public class Service1 : System.Web.Services.WebService
{

    private String accountType = "HOSTED_OR_GOOGLE";
    private String email = "example@gmail.com";
    private String password = "password";
    private String service = "ac2dm";
    private String source = "com.cloudTest.app";
    private String HTTPHeaderCT = "application/x-www-form-urlencoded";

    private String auth;
    private String reg_Id;
    private String deviceId;
    private String collapseKey = "CollapseKey";

    public void handShakeRegId()
    {

        HttpWebRequest req;
        Stream reqst;
        try
        {
            req = (HttpWebRequest)WebRequest.Create(@"https://www.google.com/accounts/ClientLogin");

         //   string proxy = null;
         //   req.MaximumAutomaticRedirections = 4;
         //   req.MaximumResponseHeadersLength = 4;
         //   req.Credentials = CredentialCache.DefaultCredentials;
            string data = String.Format("accountType={0}&Email={1}&Passwd={2}&service={3}&source={4}", accountType, email, password, service, source);

            byte[] buffer = Encoding.UTF8.GetBytes(data);
         //   ASCIIEncoding encoding = new ASCIIEncoding();
         //   byte[] buffer = encoding.GetBytes(data);

            req.Method = "POST";
            req.ContentType = HTTPHeaderCT;
            req.ContentLength = buffer.Length;
          //  req.Proxy = new WebProxy(proxy, true);
          //  req.CookieContainer = new CookieContainer();

            reqst = req.GetRequestStream(); // add form data to request stream
            reqst.Write(buffer, 0, buffer.Length);

        }
        catch (Exception e)
        {
            Debug.WriteLine("--------------------");
            Debug.Write("(handShakeRegId) Request Error:" + e);
            Debug.WriteLine("--------------------");
            throw;
        }

        HttpWebResponse res;
        Stream resst;
        try
        {
            res = (HttpWebResponse)req.GetResponse();

            resst = res.GetResponseStream();
            StreamReader sr = new StreamReader(resst, Encoding.UTF8);
            string response = sr.ReadToEnd();

            string SID = response.Substring((response.IndexOf("SID=") + 4),
            (response.IndexOf("\n") - 4));//extracting SID

            string Auth = response.Substring((response.IndexOf("Auth=") + 5),
            (response.Length - (response.IndexOf("Auth=") + 5)) - 1);//extracting Auth

            auth = Auth;
        }
        catch (Exception e)
        {
            Debug.Write("(handShakeRegId) Response Error:" + e);
            throw;
        }

        resst.Flush();
        resst.Close();
        reqst.Flush();
        reqst.Close();
    }

    [WebMethod]
    public void setRegId(String reg_id, String device_id)
    {

        reg_Id = reg_id;
        deviceId = device_id;

        Debug.WriteLine("RegID=" + reg_Id);
        Debug.WriteLine("--------------------");
        Debug.WriteLine("DeviceID=" + deviceId);

        handShakeRegId();
    }

    [WebMethod]
    public void pushMessage(String msg)
    {
        // Needed! Without an SSL Exception comes out
        System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate(object sender,
        System.Security.Cryptography.X509Certificates.X509Certificate certificate,
        System.Security.Cryptography.X509Certificates.X509Chain chain,
        System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; };

        HttpWebRequest req;
        Stream reqst;

        try
        {

            req = (HttpWebRequest)WebRequest.Create("http://android.apis.google.com/c2dm/send");
            //req.MaximumAutomaticRedirections = 4;
            //req.MaximumResponseHeadersLength = 4;
            //req.Credentials = CredentialCache.DefaultCredentials;
            //req.Credentials = new NetworkCredential("example@gmail.com","password");
            //req.KeepAlive = true;

            //string proxy = null;


            string data = String.Format("registration_id={0}&collapse_key={1}&data.message={2}", reg_Id, collapseKey, msg);

           // ASCIIEncoding encoding = new ASCIIEncoding();
           // byte[] buffer = encoding.GetBytes(data);

            byte[] buffer = Encoding.UTF8.GetBytes(data);

            req.Method = "POST";
            req.ContentType = HTTPHeaderCT;
            req.ContentLength = buffer.Length;
            req.Headers.Add("Authorization", "GoogleLogin auth=" + auth);

           // req.Proxy = new WebProxy(proxy, true);
           // req.CookieContainer = new CookieContainer();

            reqst = req.GetRequestStream(); // add form data to request stream
            reqst.Write(buffer, 0, buffer.Length);
        }
        catch (Exception e)
        {
            Debug.Write("(PushMessageMsgOUT)Error: " + e);
            throw;
        }

        HttpWebResponse res;
        Stream resst;

        try
        {
            res = (HttpWebResponse)req.GetResponse();

            HttpStatusCode responseCode = ((HttpWebResponse)res).StatusCode;
            if (responseCode.Equals(HttpStatusCode.Unauthorized) || responseCode.Equals(HttpStatusCode.Forbidden))
            {
                Debug.WriteLine("Unauthorized - need new token");
            }
            else if (!responseCode.Equals(HttpStatusCode.OK))
            {
                Debug.WriteLine("Response from web service not OK :");
                Debug.WriteLine(((HttpWebResponse)res).StatusDescription);
            }

            resst = res.GetResponseStream();
            StreamReader sr = new StreamReader(resst);

            string response = sr.ReadToEnd();

        }
        catch (Exception e)
        {
            Debug.WriteLine("(pushMessageMsgIN) Error: "+e);
            throw;
        }

        resst.Flush();
        resst.Close();
        reqst.Flush();
        reqst.Close();
    }
  }
}
  • Handshake method works well! I get auth token without problem.
  • setRegId method is called by Android device (in my case is the Android+GoogleApi 2.2 emulator)

Error which comes out is always the same in pushMessage getResponse() ( and its strange because i implement connection exactly like its in handshake method :-/ ):

A first chance exception of type ‘System.Net.WebException’ occurred in System.dll
(pushMessageMsgIN) Error: System.Net.WebException: remote server error (401) Unauthorized in System.Net.HttpWebRequest.GetResponse()

2 days for searching something useful but…. NOTHING!!
Its very stressful…

I hope someone can help me.

I red something about Authentication in IIS, so i enabled Anonymous User and other unknown things just for trying. Nothing!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:08:55+00:00Added an answer on June 14, 2026 at 4:08 pm

    Solved: MY STUPIDITY !!! i made a mistake in private String source !! I specified a wrong package name! -.-

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was playing around with recursion and did this simple function. I was assuming
Ok I know this is simple, in fact damn simple, I did this in
I am using restEasy(Restful implementation for jboss) ejb3.0, Jboss5.1.1 AS I did restful service
I've written a simple Java Console Application in Eclipse that references a WCF web
I have created a simple web service which return a response, but I am
I made the simple hello world NODEJS Server. I have a enyo web service
I am currently writing a java REST Web Service using Netbeans and Glassfish. The
I am tasked to create a simple tool, invoking a specific, SOAP-based web service.
I found a simple web service online at http://chennaiemergency.co.in/sree/s2.php?wsdl which i am able to
I created a simple WCF web service that has one method: SubmitTicket(flightticket ft, string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.