13

I am trying to download the MMS picture content through the MMS url, but it returns with a 403 (Forbidden) server response with an invalid MSISDN number. I have pasted my code below for reference. Thanks in advance!

private static boolean downloadThroughGateway(Context context, String host,
            String port, String urlMms) throws Exception {
        URL url = new URL(urlMms);

        // Set-up proxy
        if (host != null && port != null && host.equals("") && !port.equals("")) {
            Log.d(TAG, "[MMS Receiver] Setting up proxy (" + host + ":" + port
                    + ")");
            Properties systemProperties = System.getProperties();
            systemProperties.setProperty("http.proxyHost", host);
            systemProperties.setProperty("http.proxyPort", port);
            systemProperties.setProperty("http.keepAlive", "false");
        }

        // Open connection
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // Disable cache
        connection.setUseCaches(false);

        // Set the timeouts
        connection.setConnectTimeout(TIMEOUT);
        connection.setReadTimeout(TIMEOUT);

        // Connect to the MMSC
        Log.d(TAG, "[MMS Receiver] Connecting to MMS Url " + urlMms);
        connection.connect();

        try {
            Log.d(TAG,
                    "[MMS Receiver] Response code is "
                            + connection.getResponseCode());

            if (connection.getContentLength() >= 0) {
                Log.d(TAG, "[MMS Receiver] Download MMS data (Size: "
                        + connection.getContentLength() + ")");
                byte[] responseArray = new byte[connection.getContentLength()];
                DataInputStream i = new DataInputStream(
                        connection.getInputStream());
                int b = 0;
                int index = 0;
                while ((b = i.read()) != -1) {
                    responseArray[index] = (byte) b;
                    index++;
                }
                i.close();

                // Parse the response
                MmsDecoder parser = new MmsDecoder(responseArray);
                parser.parse();

                byte[][] imageBytes = new byte[parser.getParts().size()][];
                for (int j = 0; j < parser.getParts().size(); j++) {
                    imageBytes[j] = parser.getParts().get(j).getContent();
                }

                // Insert into db
                // Uri msgUri = MmsHelper.insert(context, parser.getFrom(),
                // parser.getSubject(), imageBytes);
                // ContentValues updateValues = new ContentValues();
                // updateValues.put("read", 0);
                // context.getContentResolver().update(msgUri, updateValues,
                // null,
                // null);

                // Close the connection
                Log.d(TAG, "[MMS Receiver] Disconnecting ...");
                connection.disconnect();

                System.gc();

                // Callback
                // if (bi != null)
                // bi.onReceiveMms(context, msgUri);

                return true;
            }

            // Close the connection
            Log.d(TAG, "[MMS Receiver] Disconnecting ...");
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
kamal_tech_view
  • 4,235
  • 4
  • 28
  • 49
  • What about user access credentials? Did you check whether the right credentials are set for data service access. Did you try using (Network Protocol Analyzer) tools such as WireShark etc.. to decipher the MMS submit request (HTTP PDU) and the corresponding response from the remote MMSC. Without providing any details regarding the connection such as the remote MMSC URL, log in credentials its difficult to reproduce and provide a fix for the issue. – Sdr Apr 23 '13 at 17:00
  • how to pass the credentials and I also didn't have any information about the credential that need to be provide? – kamal_tech_view Apr 25 '13 at 11:16
  • 1
    First please check if you're able to access and launch google.com on your browser successfully. Then you can navigate to the MMS specific settings by navigating to (On android 2.0) Settings-> Mobile networks -> Access Point Names -> Your Network Providor's Name . Under here you will find Tabs such as 1.)MMSC 2.) MMS proxy 3.) MMS port 4.) Username 5.) Password. – Sdr Apr 25 '13 at 16:49
  • 2
    Once you got the right credentials set on the phone under the above mentioned tabs, please refer to the post (http://android.riteshsahu.com/tips/programmatically-accessing-apns-stored-on-the-phone) to set the connection parameters by programatically reading these credentials. – Sdr Apr 25 '13 at 17:05
  • thanks for your response... it may be the exact way as you have describe but on the airtel and idea operator you can see there is no any password and username assign.. it's blank.. if you have done the mms downloading then let me show the code snippets.. – kamal_tech_view Apr 26 '13 at 09:27
  • 1
    Actually you can observe additional info here http://stackoverflow.com/questions/14452808/sending-and-receiving-sms-and-mms-in-android – Artemis Apr 26 '13 at 10:25
  • Hi Artemis 1'st thanks for your response by the way I didn't find any way to download the mms content although I have extracted all the required mms information. – kamal_tech_view Apr 26 '13 at 12:34
  • Kamal, Maybe its too late. Anyways, basically you'll have to construct a WSP/HTTP GET request to the URL which you extracted from the MMS notification message. This is the only way by which you'll be able to download the MMS message to your phone. This post should be useful for that : (http://stackoverflow.com/questions/1485708/how-do-i-do-a-http-get-in-java). – Sdr Apr 26 '13 at 15:09
  • @Sriram the issue is not what the solution you have mention, as you can see we do it by GET, the issue is how to authenticate the particular number with the operator server.. – kamal_tech_view Apr 29 '13 at 09:41
  • @kamal_tech_view: I did inquire about the user access credentials from the very first comment that I posted here, didn't I? The reason I hinted at WSP/HTTP GET in my second comment was because of you saying "I didn't find any way to download the mms content although I have extracted all the required mms information". What was that all about? Did you check with the service provider (Idea/Airtel) so as to why the (MMS specific settings) required fields are blank? – Sdr May 01 '13 at 17:35
  • @Sriram: My application need those required field and the default messaging app in phone didn't ... does that make any sense ? – kamal_tech_view May 02 '13 at 04:50
  • Anything @kamal_tech_view ? I get from time to time a timed out exception... – zwebie May 05 '13 at 12:28
  • @zwebie : I guess you have ensureRouteToHost() method, you should ensure that it works perfectly and you can post your code as a answer. – kamal_tech_view May 06 '13 at 05:12
  • @kamal_tech_view I do have ensure route to host but it keeps on throwing an IOException when trying to connect to the proxy – zwebie May 06 '13 at 06:16
  • can you post your code snippets so, that I can go in detail through it.. – kamal_tech_view May 06 '13 at 06:33
  • hi all I have found the solution for this issue please find the answer as mention. – kamal_tech_view May 28 '13 at 05:02

1 Answers1

3

Now I am able to find out the solution but I find that sometime the download code doesn't work but when you try again it works although what I was missing first establishing the connectivity to the server. I have mention below the connectivity method and after this call the method name downloadThroughGateway(parameters) which is mention on this question code.

private void startConnectivity() throws Exception {
        ConnectivityManager mConnMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (!mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS)
                .isAvailable()) {
            throw new Exception("Not available yet");
        }
        int count = 0;
        int result = beginMmsConnectivity(mConnMgr);
        if (result != PhoneEx.APN_ALREADY_ACTIVE) {
            NetworkInfo info = mConnMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
            while (!info.isConnected()) {
                Thread.sleep(1500);
                info = mConnMgr
                        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
                Log.d(">>>", "Waiting for CONNECTED: state=" + info.getState());
                if (count++ > 5)
                    throw new Exception("Failed to connect");
            }
        }
        Thread.sleep(1500);
    }
kamal_tech_view
  • 4,235
  • 4
  • 28
  • 49