Tuesday, May 10, 2011

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

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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate