title: Execution stops automatically at getOutputStream() function Push notification using servlets
body: <p>I have a problem while getting a push notification on the server side. I am using C2DM sever connection to achieve the push notification. Its working fine on android but not in the Java Servecr. I'm able to get the registration ID and authentication code but the handling stops when it comes to the line:</p>
  
  <pre><code>OutputStream out = conn.getOutputStream();
  </code></pre>
  
  <p>It does not throw anything, just stops.</p>
  
  <p>If anyone has a solution for this then please guide me. I am adding the whole code so you can go through and tell me where I'm wrong in getting the result.</p>
  
  <pre><code>import org.apache.http.client.methods.HttpPost;
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.net.URLConnection;
  import java.net.URLEncoder;
  import java.util.ArrayList;
  import java.util.List;
  import javax.net.ssl.HttpsURLConnection;
  import javax.print.DocFlavor.INPUT_STREAM;
  import org.apache.http.HttpClientConnection;
  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.DefaultHttpClientConnection;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.message.BasicNameValuePair;
  import org.apache.http.util.EntityUtils;
  public class Server {
      private static String key=null;
      //    private final static String AUTH = "authentication";
      private static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth";
      public static final String PARAM_REGISTRATION_ID = "registration_id";
      public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle";
      public static final String PARAM_COLLAPSE_KEY = "collapse_key"; 
      private static final String UTF8 = "UTF-8";
      private static String Authcode = null;
  
      // Registration is currently hardcoded
      private final static String YOUR_REGISTRATION_STRING = "reg id";
  
      public void getAuthentification() {
          System.out.println("check");
          //HttpPost post = new HttpPost("http://www.google.com/accounts/ClientLogin");
          try {
              System.getProperties().put("proxySet", true);
              System.getProperties().put("proxyHost","proxy" );
              System.getProperties().put("proxyPort","8080");
              System.out.println("getAuthentication method called****************************");
              URL url=new URL("https://www.google.com/accounts/ClientLogin");
  
              URLConnection connection=url.openConnection();
              connection.setDoOutput(true);
  
              HttpURLConnection conn=(HttpURLConnection)connection;
              conn.setRequestMethod("POST");
              conn.setDoInput(true);
              conn.setDoOutput(true);
              conn.setUseCaches(true);
  
              StringBuilder content = new StringBuilder();
              content.append("Email=").append("xyz@gmail.com");
              content.append("&amp;Passwd=").append("asdfgt");
              content.append("&amp;service=").append("ac2dm");
              content.append("&amp;source=").append("MY_APP-V0.1");
              content.append("&amp;accountType=").append("HOSTED_OR_GOOGLE");
  
              OutputStream out = conn.getOutputStream();
              out.write(content.toString().getBytes("UTF-8"));
              out.close();
  
              int res = conn.getResponseCode();
              System.out.println(res + "Success");
              StringBuffer resp = new StringBuffer();
  
              if(res == HttpsURLConnection.HTTP_OK){
                  InputStream in = conn.getInputStream();
                  BufferedReader rd = new BufferedReader(new InputStreamReader(in));
                  String line = "";
                  while ((line = rd.readLine()) != null) {
                      System.out.println(line);
                      System.out.println("inside");
                      if (line.startsWith("Auth=")) {
                          Authcode = line.substring(5);
                          resp.append(line.substring(5));
                          System.out.println(line.substring(5));
                          System.out.println("something to be done here..");
  
                      }
                  }
                  rd.close();
              }
          }
      }
      public void sendMessage() {
          try {
              System.out.println(YOUR_REGISTRATION_STRING);
              System.out.println("Authcode = " + Authcode);
              System.getProperties().put("proxySet", true);
              System.getProperties().put("proxyHost","proxy" );
              System.getProperties().put("proxyPort","8080");
              URL url1 = new URL("https://android.apis.google.com/c2dm/send");
              System.out.println("here2.5");
              HttpURLConnection conn1 = (HttpURLConnection) url1.openConnection();
              System.out.println("here2.6");
              conn1.setDoInput(true);
              conn1.setDoOutput(true);
              conn1.setUseCaches(false);            
              conn1.setRequestMethod("POST");
  
              conn1.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
              conn1.setRequestProperty("Authorization", "GoogleLoginauth="+ Authcode);
              System.out.println("here2.7");
              OutputStream out = conn1.getOutputStream();
              System.out.println("send Message method.");
              String auth_key="n/a";
              if(key!=null) {
                  auth_key=Authcode;
              }
              System.out.println("here");
              // Send a sync message to this Android device.
              StringBuilder postDataBuilder = new StringBuilder();
              postDataBuilder.append(PARAM_REGISTRATION_ID)
                  .append("=").append(YOUR_REGISTRATION_STRING);
              System.out.println("here1");
              postDataBuilder.append("&amp;").append(PARAM_COLLAPSE_KEY)
                  .append("=").append("0");
              System.out.println("here2");
              postDataBuilder.append("&amp;").append("data.payload")
                  .append("=").append(URLEncoder.encode("Lars war hier", UTF8));
  
              byte[] postData = postDataBuilder.toString().getBytes(UTF8);
              conn1.setRequestProperty("Content-Length",
                              Integer.toString(postData.length));
              // Hit the dm URL.
              System.out.println("out created");
              out.write(postData);
              System.out.println("data written");
  
              out.close();
              System.out.println("here3");
              int responseCode = conn1.getResponseCode();
  
              System.out.println(String.valueOf(responseCode));
              // Validate the response code
  
              if (responseCode == 401 || responseCode == 403) {
                  // The token is too old - return false to retry later, will
                  // fetch the token
                  // from DB. This happens if the password is changed or token
                  // expires. Either admin
                  // is updating the token, or Update-Client-Auth was received by
                  // another server,
                  // and next retry will get the good one from database.
                  System.out.println("C2DM, Unauthorized - need token");
              }
          } catch (Exception ignore) {
              // the editor that corrected the indentation of the code could not find any code here so he closed all methods to have a syntactically correct class.
          }
      }
  }
  </code></pre>