Hopefully some of you can help me with my "brain-deadlock".
(just for the understanding) I'm trying to receive a database by using a php-script. Therefore I have to call a webadress/server which gives me access to the php-script only if I'm using a valid username/pwd-combo AND sending the right Parameters.
My code WORKS fine when using HttpClient and HttpPost. (below the GOOD code)
InputStream myInputDB = null;
OutputStream myOutputDB = null;
HttpResponse response = null;
// Path to the just created empty db
String dbFilePath = DB_PATH + KeyConstants.DB_NAME;
try {
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("version", "20110516140000"));
HttpPost httppost = new HttpPost("http://USERNAME:PASSWORD@SomeAdress/u/db2.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpClient.execute(httppost);
//Open your local db as the input stream
myInputDB = response.getEntity().getContent();
//Open the empty db as the output stream
myOutputDB = new FileOutputStream(dbFilePath);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInputDB.read(buffer)) > 0) {
myOutputDB.write(buffer, 0, length);
}
//Close the streams
myOutputDB.flush();
myOutputDB.close();
myInputDB.close();
Now I'm trying to use HttpURLConnection/HttpsURLConnection instead of the above solution. Why should be clear, the live-system uses HTTPS (server certification).
By now I wasted days of trying to figure out how to...
- POST Parameters with HttpURLConnection
- Authenticate on server
- Avoid certification-check in general (for testing!)
and I just don't know where I'm making the mistake. Below my code so far...
String urlString = "http://USERNAME:PASSWORD@SomeAdress/u/db2.php";
String params = "version=20110516140000";
InputStream stream = null;
url = new URL(urlString);
if (url.getProtocol().toLowerCase().equals("https")) {
trustAllHosts();
HttpsURLConnection urlconn = (HttpsURLConnection) url.openConnection();
urlconn.setHostnameVerifier(DO_NOT_VERIFY);
urlconn.setDoInput(true);
urlconn.setDoOutput(true);
urlconn.setRequestMethod("POST");
urlconn.setRequestProperty("Content-Type", "text/xml");
urlconn.setRequestProperty("Content-Length", String.valueOf(params.getBytes().length));
OutputStreamWriter wr = new OutputStreamWriter(urlconn.getOutputStream());
wr.write(params);
wr.flush();
stream = urlconn.getInputStream();
if ("gzip".equals(urlconn.getContentEncoding())) {
stream = new GZIPInputStream(stream);
}
wr.close();
} else {
// Almost the same as if-path, but without trustAllHosts() and with HttpURLConnection instead of HttpsURLConnection
}
return stream;
What I need is an InputStream which I then can save/copy on the android phone (as in the working code). By now the result I get is an IOException. The Activity aborts as soon as urlconn.getInputStream() is being called. But the getOutputStream() too has no valid value.
Now to the question. Could anyone please review my code and tell me how to POST Parameters the right way. In addition to that do you know if I can send the authentication via URL? Particularly the HTTP'S' part interests me. For the non-secure mode I already have a running solution.
Thanks for your help!