0

I created an android application that makes some requests to a website and gets back data in JSON format, but when I make a request to a specific PHP file, something weird happens: the whole website stops working. The webpage does not load at all. It's like the site does not exist and obviously that happens automatically from my Internet Provider.

In the error log I get a record that says something about the header:

[Fri Jul 11 19:17:26 2014] [error] [client 79.103.143.40] ModSecurity: [file "/etc/httpd/crs/activated_rules/modsecurity_crs_21_protocol_anomalies.conf"] [line "84"] [id "960904"] [rev "2"] [msg "Request Containing Content, but Missing Content-Type header"] [severity "NOTICE"] [ver "OWASP_CRS/2.2.8"] [maturity "9"] [accuracy "9"] Warning. Match of "rx ^0$" against "REQUEST_HEADERS:Content-Length" required. [hostname "www.MYWEBSITW.eu"] [uri "/webservice/MYPHPFILE.php"] [unique_id "U8AOFn8AAAEAABq-qs8AAAEt"]

Is that so serious an error that my provider stops my site on every request?

In my php file, what I do is:

  1. connect to my db
  2. execute a select statement
  3. echo the SQL results (in order to send them to Android device) like this:

       header('Content-type: application/json');
       echo (json_encode(array('notifications'=>$result)));
    

Has anyone faced that problem when trying to request data from a web server?

David Conrad
  • 15,432
  • 2
  • 42
  • 54
  • 1
    What `Content-Type` header are you sending on the request from the Android app? (Note: not the `Content-Type` that `PHP` is sending back from the server side, which you've shown us, but the one on the request from the client side.) – David Conrad Jul 11 '14 at 16:46

2 Answers2

0

It is not entirely clear, because you haven't shown us how you send the request from the client side on Android, but I think the problem is that you are not setting any Content-Type on the request that you make. Assuming you are using the HttpPost class, you would want to use:

httpPost.addHeader("Content-Type", "text/plain");

or

httpPost.addHeader("Content-Type", "application/json");

or whatever is appropriate for whatever data you are sending.

David Conrad
  • 15,432
  • 2
  • 42
  • 54
0

actually i don't use addHeader at all on my request, i just use request.setHeader("json", json.toString());. do you think that this is the problem? Into my doInBackground function i have that code:

            JSONObject json = new JSONObject();

            json.put("action", "get");
            json.put("serial", mSerial);
            json.put("appId", AppConstants.APP_ID);
            json.put("dateFrom", MainApplication.dbHelper.getNotificationLastTime());
            Calendar now = Calendar.getInstance();
            json.put("currentDate",sdfyyyyMMdd.format(now.getTime()));

            Log.i(TAG, "GetNotificationTask request = " + json.toString());

            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
            HttpClient client = new DefaultHttpClient(httpParams);

            HttpPost request = new HttpPost(AppConstants.URL_NOTICATION);

            request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF-8")));
            request.setHeader("json", json.toString());

            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();


            if (entity != null) {
                InputStream instream = entity.getContent();

                String result = RestClient.convertStreamToString(instream);
                Log.i(TAG, "GetNotificationTask response = " + result);
                JSONObject jsonRes = new JSONObject(result);
                JSONObject notifications = jsonRes.getJSONObject("notifications");

                String success = notifications.getString("success");
                if (success.equals("1")) {
                    JSONArray messages = notifications.getJSONArray("0");

                    Log.i(TAG, "Notification messages count = " + messages.length());
                    String message = "";
                    ArrayList<NotificationData> msgArray = new ArrayList<NotificationData>();
                    if (messages.length() == 0)
                        return null;

                    for (int i = 0; i < messages.length(); i++) {
                        NotificationData info = new NotificationData();

                        JSONObject object = messages.getJSONObject(i);
                        info.setId(object.getString("msgId"));
                        info.setSerial(object.getString("fkmsgSerial"));
                        info.setTitle(object.getString("msgTitle"));
                        info.setMessage(object.getString("msgMessage"));
                        info.setDate(object.getString("msgDate"));
                        info.setImage(object.getString("msgImage"));
                        msgArray.add(info);

                        if (i == 0)
                            message = info.getMessage();
                        else
                            message = "\n" + info.getMessage();
                    }

                    MainApplication.dbHelper.insertMessages(msgArray);

                    String title = "";
                    if (messages.length() == 1)
                        title = "1 new notificiation";
                    else
                        title = messages.length() + " new notificiations";

                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.putExtra("SHOW_MESSAGES", true);

                    PendingIntent pIntent = PendingIntent.getActivity(
                            getApplicationContext(), 0, intent,
                            PendingIntent.FLAG_UPDATE_CURRENT);
                    Notification noti = new NotificationCompat.Builder(getApplicationContext())
                        .setContentTitle(title)
                        .setContentText(message)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setSound(soundUri)
                        .setContentIntent(pIntent).getNotification();

                    noti.flags |= Notification.FLAG_AUTO_CANCEL;

                    NotificationManager notificationManager = (NotificationManager) getApplicationContext()
                            .getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(R.string.alarm_service_started, noti);
                }
Messa
  • 24,321
  • 6
  • 68
  • 92