0

我创建了一个 android 应用程序,它向网站发出一些请求并以JSON格式返回数据,但是当我向特定PHP文件发出请求时,发生了一些奇怪的事情:整个网站停止工作。网页根本不加载。就好像该网站不存在,显然这会从我的 Internet 提供商处自动发生。

在错误日志中,我得到一条记录,其中说明了标题:

[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"]警告。需要匹配“rx ^0$”与“REQUEST_HEADERS:Content-Length”。[主机名“www.MYWEBSITW.eu”][uri“/webservice/MYPHPFILE.php”][unique_id“U8AOFn8AAAEAABq-qs8AAAEt”]

这是一个严重的错误,以至于我的提供商在每次请求时都会停止我的网站?

在我的 php 文件中,我所做的是:

  1. 连接到我的数据库
  2. 执行一条选择语句
  3. 像这样回显SQL结果(以便将它们发送到 Android 设备):

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

有人在尝试从 Web 服务器请求数据时遇到过这个问题吗?

4

2 回答 2

0

目前尚不完全清楚,因为您尚未向我们展示如何在 Android 上从客户端发送请求,但我认为问题在于您没有Content-Type对您提出的请求进行任何设置。假设您正在使用HttpPost该类,您将希望使用:

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

或者

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

或适合您发送的任何数据的任何内容。

于 2014-07-11T17:47:04.333 回答
0

实际上我根本没有在我的请求中使用 addHeader,我只是使用 request.setHeader("json", json.toString()); . 你认为这是问题所在吗?在我的 doInBackground 函数中,我有该代码:

            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);
                }
于 2014-07-12T07:52:26.973 回答