0

Есть два сайта - один использует протокол https, другой http. На них загружен один и тот же файл api.php. (да и вообще оба сайта являются копией друг друга). Сайты выглядят так:

  1. https://www.site.000webhost.com/
  2. http://site.online/

Сам запрос соответственно:

  1. https://www.site.000webhost.com/api.php?connect=check
  2. http://site.online/api.php?connect=check

Должен придти ответ: server_connect.

Если делать запрос через браузерную строку, то все нормально приходит в обоих случаях. Но вот если делать запрос через приложение, то в первом случае (https...) все проходит нормально, а вот во втором приложение вылетает, когда делает этот же самый запрос.

Исходники:

Класс для GET запроса:

class ProgressTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... path) {
        String content;
        try {
            content = getContent(path[0]);
        } catch (IOException ex) {
            content = ex.getMessage();
        }
        return content;
    }

    @Override
    protected void onPostExecute(String content) { //метод для получения ответа
        if (content.equals("server_connect")){
            Intent intent = new Intent(MainActivity.this, MainCl.class);
            startActivity(intent);
        } else {
            Toast.makeText(getApplicationContext(),R.string.server_error, Toast.LENGTH_LONG).show();
        }
        pb.setVisibility(View.INVISIBLE);
        btn.setText(R.string.next);
        btn.setEnabled(true);
    }

    private String getContent(String path) throws IOException {
        BufferedReader reader = null;
        try {
            URL url = new URL(path);
            HttpsURLConnection c = (HttpsURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setReadTimeout(PublicVar.time);
            c.connect();
            reader = new BufferedReader(new InputStreamReader(c.getInputStream()));
            StringBuilder buf = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                buf.append(line);
            }
            return (buf.toString());
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }
}

Сам GET-запрос:

new ProgressTask().execute((PublicVar.URL + "server_connect=" + "VersionApp: "
            + BuildConfig.VERSION_NAME + ";_SDK:_"+ Integer.toString(Build.VERSION.SDK_INT) + ";_ModelPhone:_" + android.os.Build.MODEL.toString()).replaceAll(" ", "_"));

Переменная в классе PublicVar:

static final String URL = "https://www.site.000webhost.com/api.php?"; //(работает)

или

static final String URL = "http://www.site.online/api.php?"; //(не работает)
2
  • 2
    HttpsURLConnection c = (HttpsURLConnection) url.openConnection(); может быть поэтому? Для http нужно использовать HttpURLConnection
    – iksuy
    24 ноя 2017 в 11:21
  • да, именно поэтому... Спасибо. 24 ноя 2017 в 11:25

1 ответ 1

-2

Вся проблема в том, что я использовал:

HttpsURLConnection c = (HttpsURLConnection) url.openConnection();

А для Http нужно использовать:

HttpURLConnection c = (HttpURLConnection) url.openConnection();

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.