【问题标题】:My website stops working when I request data from my Android application当我从我的 Android 应用程序请求数据时,我的网站停止工作
【发布时间】:2014-07-11 16:31:06
【问题描述】:

我创建了一个 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“请求包含内容,但缺少 Content-Type 标头”] [严重性“通知”] [版本“OWASP_CRS/2.2.8”] [成熟度“9”] [准确性“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 服务器请求数据时遇到过这个问题?

【问题讨论】:

  • 您在 Android 应用程序的请求中发送什么 Content-Type 标头? (注意:不是PHP 从服务器端发回的Content-Type,您已经向我们展示了,而是来自客户端的请求。)

标签: java mysql apache android-webservice


【解决方案1】:

这并不完全清楚,因为您还没有向我们展示您如何在 Android 上从客户端发送请求,但我认为问题在于您没有在您发出的请求上设置任何 Content-Type。假设您使用的是 HttpPost 类,您可能希望使用:

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

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

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

【讨论】:

    【解决方案2】:

    实际上,我根本没有在我的请求中使用 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);
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 2018-08-28
      • 2019-06-16
      • 1970-01-01
      相关资源
      最近更新 更多