3

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...

  1. POST Parameters with HttpURLConnection
  2. Authenticate on server
  3. 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!

njsmecho
  • 45
  • 6
  • what I've used so far... http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests http://stackoverflow.com/questions/5859667/how-to-send-requestparameter-in-post-request-using-httpurlconnection – njsmecho Jun 26 '12 at 14:50
  • also those links... http://stackoverflow.com/questions/995514/https-connection-android http://home-1.worldonline.nl/~bmc88/java/sbook/045.html http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139 http://digitallibraryworld.com/?p=189 – njsmecho Jun 26 '12 at 15:01

1 Answers1

0

If you can manually perform HTTPS connection using browser e.g. Firefox, then you just look into request sent by browser (e.g. using Firebug Firefox extension) and copy from there headers you need and put them into your code.

The idea is to login first using standard tools (browser in this case) and see, what is sends and receives on success.

ivan_onys
  • 2,282
  • 17
  • 21