[android-developers] Re: HttpsURLConnection's getResponseCode() returns -1

2011-05-11 Thread Mika
Hi,

I sometimes had similar kind of problems, where one request received
the correct responseCode but the second request to the same URL failed
with request code -1. After setting http.keepAlive system property to
false, everything started to function properly. So maybe you could try
adding this line  System.setProperty(http.keepAlive, false); to
your code. Maybe it'll help.

-Mika

On May 11, 3:36 am, John Gaby jg...@gabysoft.com wrote:
 I am trying to use HttpsURLConnection to connect to a secure site, and
 it is failing and getResponseCode() is returning -1.  The following is
 the code that I am using.  Note that this very same code works in
 other cases.  Can anyone give me a clue as to why I might get the -1,
 and how I can get more information about what is going wrong.  Also
 note, that if I take the URL that I am trying to connect to in this
 call and paste it into a browser, I get the correct response, so I am
 pretty sure that the URL itself is correct.  In this particular call,
 m_data is null, so it is performing a GET and no data is written.

 Thanks

     HttpsURLConnection connection = null;

     try
     {
         URL url = new URL(arg0[0]);
         connection = (HttpsURLConnection) url.openConnection();

         // Allow Inputs  Outputs if there is data to send
         connection.setDoInput(true);
         connection.setDoOutput(m_data != null);
         connection.setUseCaches(false);

         // Enable GET or POST depending on whether there is data to
 send
         connection.setRequestMethod((m_data == null) ? GET :
 POST);

         connection.setRequestProperty(Connection, Keep-Alive);
         connection.setRequestProperty(Content-Type, application/
 octet-stream);

         if (m_data != null)
         {
             DataOutputStream outputStream = null;

             connection.setFixedLengthStreamingMode(m_data.length);

             outputStream = new
 DataOutputStream( connection.getOutputStream() );
             outputStream.write(m_data);

             outputStream.flush();
             outputStream.close();
         }

         // Responses from the server (code and message)
         int serverResponseCode = connection.getResponseCode();
         String serverResponseMessage =
 connection.getResponseMessage();

         InputStream inputStream = connection.getInputStream();

         int nBytes;

         m_curBytes    = 0;
         m_totBytes    = connection.getContentLength();

         byte[] bytes = new byte[65536];

         while ((nBytes = inputStream.read(bytes))  0)
         {
             if (m_file != null)
             {
                 m_file.write(bytes, 0, nBytes);
             }
             else if (m_result != null)
             {
                 m_result.append(bytes, 0, nBytes);
             }

             m_curBytes    += nBytes;

             m_handler.post(new Runnable()
             {
                 public void run()
                 {
                     if (m_pInet != 0)
                     {
                         GDownloadProgress(m_pInet, m_curBytes,
 m_totBytes);
                     }
                     else
                     {
                         int i = 0;
                     }
                 }
             });

         }

         if (m_file != null)
         {
             m_error = false;
         }
         else if (m_result != null)
         {
             m_error    = (m_result.length() = 0);
         }
         else
         {
             m_error    = true;
         }
     }
     catch (Exception ex)
     {
         GSystem.GLogWarning(GINet: error =  + ex.getMessage());
     }

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: HttpsURLConnection's getResponseCode() returns -1

2011-05-11 Thread John Gaby
Thanks for the tip, that does indeed fix the problem.  Can you tell me
what the ramifications of setting the keepAlive to false are?

Thanks.

On May 11, 3:18 am, Mika mika.ristim...@gmail.com wrote:
 Hi,

 I sometimes had similar kind of problems, where one request received
 the correct responseCode but the second request to the same URL failed
 with request code -1. After setting http.keepAlive system property to
 false, everything started to function properly. So maybe you could try
 adding this line  System.setProperty(http.keepAlive, false); to
 your code. Maybe it'll help.

 -Mika

 On May 11, 3:36 am, John Gaby jg...@gabysoft.com wrote:

  I am trying to use HttpsURLConnection to connect to a secure site, and
  it is failing and getResponseCode() is returning -1.  The following is
  the code that I am using.  Note that this very same code works in
  other cases.  Can anyone give me a clue as to why I might get the -1,
  and how I can get more information about what is going wrong.  Also
  note, that if I take the URL that I am trying to connect to in this
  call and paste it into a browser, I get the correct response, so I am
  pretty sure that the URL itself is correct.  In this particular call,
  m_data is null, so it is performing a GET and no data is written.

  Thanks

      HttpsURLConnection connection = null;

      try
      {
          URL url = new URL(arg0[0]);
          connection = (HttpsURLConnection) url.openConnection();

          // Allow Inputs  Outputs if there is data to send
          connection.setDoInput(true);
          connection.setDoOutput(m_data != null);
          connection.setUseCaches(false);

          // Enable GET or POST depending on whether there is data to
  send
          connection.setRequestMethod((m_data == null) ? GET :
  POST);

          connection.setRequestProperty(Connection, Keep-Alive);
          connection.setRequestProperty(Content-Type, application/
  octet-stream);

          if (m_data != null)
          {
              DataOutputStream outputStream = null;

              connection.setFixedLengthStreamingMode(m_data.length);

              outputStream = new
  DataOutputStream( connection.getOutputStream() );
              outputStream.write(m_data);

              outputStream.flush();
              outputStream.close();
          }

          // Responses from the server (code and message)
          int serverResponseCode = connection.getResponseCode();
          String serverResponseMessage =
  connection.getResponseMessage();

          InputStream inputStream = connection.getInputStream();

          int nBytes;

          m_curBytes    = 0;
          m_totBytes    = connection.getContentLength();

          byte[] bytes = new byte[65536];

          while ((nBytes = inputStream.read(bytes))  0)
          {
              if (m_file != null)
              {
                  m_file.write(bytes, 0, nBytes);
              }
              else if (m_result != null)
              {
                  m_result.append(bytes, 0, nBytes);
              }

              m_curBytes    += nBytes;

              m_handler.post(new Runnable()
              {
                  public void run()
                  {
                      if (m_pInet != 0)
                      {
                          GDownloadProgress(m_pInet, m_curBytes,
  m_totBytes);
                      }
                      else
                      {
                          int i = 0;
                      }
                  }
              });

          }

          if (m_file != null)
          {
              m_error = false;
          }
          else if (m_result != null)
          {
              m_error    = (m_result.length() = 0);
          }
          else
          {
              m_error    = true;
          }
      }
      catch (Exception ex)
      {
          GSystem.GLogWarning(GINet: error =  + ex.getMessage());
      }



-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en