Запросы в Volley, похожие на Node.js (нет ответа) - PullRequest
0 голосов
/ 09 мая 2020

Я пытаюсь выполнить те же запросы, чтобы получить такой же ответ в Java, но не получаю ответа. Это код Node.js, на котором я моделирую:

'use strict';
let request = require('request');
let exec = require('child_process').exec;

const USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36';

class WizzApi {
  constructor() {
    this.apiUrl = null;
    this.apicall = 0;
    this.cookie = null;
    this.cookieStartedRequest = false;
    this.updateCookie();
    this.updateApiVersionUrl();
    // this.apiUrl = 'https://be.wizzair.com/6.0.0/Api';
  }

  getCookie(cache = true) {
    if (this.cookieStartedRequest) return this.cookieStartedRequest;
    if (this.cookie && cache) return Promise.resolve(this.cookie);

    this.cookieStartedRequest = new Promise((resolve, reject) => {
      return this.getApiVersionUrl()
        .then(function(apiUrl) {
          let
          //url = apiUrl + '/information/browserSupport',
          url = apiUrl + '/information/buildNumber',
          options = {
            url: url,
            headers: {
      'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
      'accept-language': 'en-US,en;q=0.9,ro;q=0.8',
      'accept-encoding': 'gzip, deflate, br',
      'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
      'connection': 'open'
            } 
          };
      console.log(options);
      //Temporary
      //resolve('ak_bmsc=D0856A430327C6B4DBDD15FF733410310210B5165B1D0000E8C50B5C4501732D~plhWH04eb5GTS17uiXAp9CtuI56o/fH9rWRUzEnZpJh7dfuP1rE41d9kzWkuYk4yVdeQbuURunGufbyD4B76huSkckPgFn2aneJc/zrh8xH/K+YnIwWk2YGfZU3r4EpBGwhSwRgyHj7plDpXAdrlwLocob1qdNt7qIlJ3iO7WHNITiBfi8+M1dsfnzsrzO/gjyvRP/VYIew/Ip89YhmQ5cm89UNgQFXmOYH8Iwt2OaZIM=;');

          exec('sh getWizzCache.sh ' + apiUrl,
            (error, stdout, stderr) => {
              console.log(stdout);
              if (error !== null) {
        reject(Error('Failed \'sh getWizzCache.sh\' with error: ' + error));
              } else {
            resolve(stdout.slice(0, -1)); // remove last character, probably new line
              };
            }
          );

    })
      .catch((error) => {throw ('Failed to get cookie. ' + error);}); 
    })
    .catch((error) => {throw (error);}); 
    return this.cookieStartedRequest;
  }

  updateCookie() {
    this.cookie = null;
    this.cookieStartedRequest = false;
    return this.getCookie(false).then((r) => {this.cookie = r;}).catch((error) => {logger.error('Failed to get cookie. ' + error);});
  }

  getApiVersionUrl(cache = true, url = 'https://wizzair.com/static_fe/metadata.json') {
    if (this.apiUrl && cache) return Promise.resolve(this.apiUrl);
    return new Promise(function(resolve, reject) {
      request(url, function(error, response, body) {
        if (error) {
          reject(Error('Request error: ' + error));
        } else if (response.statusCode !== 200) {
          reject(Error('Bad statusCode error: ' + response.statusCode));
        } else {
          let resBody;
          try {
        // TODO: validate body is a valid JSON
        try {
              resBody = JSON.parse(body);
            } catch (error) {
              logger.error('Could not parse body. Trying fallback.');
              resBody = JSON.parse(body.substring(1, body.length));
        }
          } catch (error) {
            reject(Error('Could not parse body. ' + error));
          }

          try {
            resolve(resBody.apiUrl);
          } catch(error) {
            reject(Error('apiUrl field not found!' + error));
          }
        }
      });
    }); 
  }

  updateApiVersionUrl() {
    let that = this;
    this.apiUrl = null;
    return this.getApiVersionUrl(false).then((r) => {that.apiUrl = r}).catch((e) => logger.error('Failed to update API version' + e));
  }

  getPricesPeriod(departure, arrival, depDateFrom, depDateTo) {
    // Map {
    //   '2017-10-27T00:00:00' => Map { '2017-10-27T19:40:00' => 539 },
    //   '2017-10-28T00:00:00' => Map { '2017-10-28T06:05:00' => 359 },
    //   '2017-10-29T00:00:00' => Map { '2017-10-29T06:00:00' => 319, '2017-10-29T20:15:00' => 319 } }
    let that = this;
    return new Promise(function(resolve, reject) {
      return that.getApiVersionUrl()
        .then(function(apiUrl) {
          return that.getCookie()
            .then(function(cookie) {
              let payload = {
                'adultCount': 1,
                'childCount': 0,
                'infantCount': 0,
                'flightList':[
                  {
                    'departureStation': departure,
                    'arrivalStation': arrival,
                    'from': depDateFrom,
                    'to': depDateTo
                  }
                ],
                'priceType': 'regular'
              },
              url = apiUrl + '/search/timetable',
              options = {
                url: url,
                body: JSON.stringify(payload),
                headers: {
                  'content-type': 'application/json; charset=utf-8',
                  'cookie': cookie,
                  'user-agent': USER_AGENT
                }
              };

              return request.post(options, function(error, response, body) {
                //console.log('body', body);
                if (error) {
                  reject(Error('Request error: ' + error));
                } else if (response.statusCode !== 200) {
                  //that.updateCookie();
                  reject(Error('Bad statusCode error: ' + response.statusCode));
                } else {
                  try {
                    // outboundFlights: {
                    //   departureDate: '2017-10-01T00:00:00',
                    //   departureDates: ['2017-10-01T06:05:00', '2017-10-01T19:40:00'],
                    //   price: {amount: 500}
                    // }
                    let flights = JSON.parse(body).outboundFlights,
                        periodPriceMap = new Map();

                    flights.forEach(function(fly, i) {
                      let date = fly.departureDate.toString(),
                          dateTimeArray = fly.departureDates,
                          dateTimeMap = new Map(),
                          price = -1;

                          if (fly.priceType !== 'soldOut') {
                            price = fly.price.amount;
                          }

                          // Implement when:
                          // price: null
                          // priceType: "soldOut"

                        dateTimeArray.forEach(function(dateTime, i) {
                          dateTimeMap.set(dateTime, price)
                        });

                      if (date && price) {
                        dateTimeMap
                        periodPriceMap.set(date, dateTimeMap);
                      } else {
                        return
                      }
                    });

                    //console.log('periodMap', periodPriceMap);
                    resolve(periodPriceMap);
                  } catch (error) {
                    reject(Error('Could not parse body. ' + error));
                  }
                }
              });
            })
            .catch((error) => {throw ('Failed to get cookie. ' + error);});
        });
    });
  }

  toString() {
    return 'Wizz API url: ' + this.apiUrl;
  }
}

module.exports = WizzApi;

Это getWizzCache. sh:

#!/bin/sh
curl -I -XGET --output - -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'accept-language: en-US,en;q=0.9,ro;q=0.8' -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36' -H 'accept-encoding: gzip, deflate, br' -H 'connection: keep-alive' "$1/information/buildNumber" | grep -Fi 'set-cookie: ak_bmsc' | cut -d " " -f 2

В моем коде я делаю :

Я пытаюсь getPricesPeriod. Сначала я получаю apiVersionUrl из: https://wizzair.com/buildnumber, затем создаю url: (https://be.wizzair.com/10.19.1/Api/information/buildNumber) и получаю заголовок с «Set-Cook ie» ak_bmsc. Затем я отправляю запрос с телом json и заголовком с ak_bmsc как повар ie с обрезанным последним символом, чтобы получить период цен: https://be.wizzair.com/10.19.1/Api/search/timetable, но я не получаю никакого ответа. Я что-то упустил?

Это мой код:

String ak_bmscKey = "ak_bmsc";
String ak_bmscValue = null;

    public void getApiVersionUrlWizzair() {

        String urlGetApiVersionUrl = "https://wizzair.com/buildnumber";

        RequestQueue queue = Volley.newRequestQueue(this);

        StringRequest stringRequestForApiUrl = new StringRequest
                (Request.Method.GET, urlGetApiVersionUrl, new Response.Listener<String>() {

                    @SneakyThrows
                    @Override
                    public void onResponse(String response) {
                            String apiUrl = (response.substring(response.indexOf("http"))).split(" ")[0];
                                getCookieWizzair(apiUrl);
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
                params.put("Accept-Language", "pl,en-US;q=0.7,en;q=0.3");
                params.put("Connection", "keep-alive");
                params.put("Host", "wizzair.com");
                params.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36");
                return params;
            }
        };
        queue.add(stringRequestForApiUrl);
    }

    private void getCookieWizzair(final String apiUrlWizzair) {

        String urlGetCookie = apiUrlWizzair + "/Api/information/buildNumber";

        RequestQueue queue = Volley.newRequestQueue(this);

        StringRequest stringRequestForCookie = new StringRequest
                (Request.Method.GET, urlGetCookie, new Response.Listener<String>() {

                    @SneakyThrows
                    @Override
                    public void onResponse(String response) {
                    }
                }, new Response.ErrorListener() {

                    @SneakyThrows
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
                params.put("Accept-Language", "en-US,en;q=0.9,ro;q=0.8");
                params.put("Connection", "open");
                params.put("Accept-Encoding", "gzip, deflate, br");
                params.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36");
                return params;
            }
            @SneakyThrows
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                List<Header> responseHeaders = response.allHeaders;
                HashMap<String,String> cookiesHashMap = new HashMap<>();

                for (int i = 0; i < responseHeaders.size(); i++) {
                    if (responseHeaders.get(i).getName().equals("Set-Cookie")) {
                        if (responseHeaders.get(i).getValue().contains(ak_bmscKey)) {
                            ak_bmscValue = responseHeaders.get(i).getValue();
                            ak_bmscValue = ak_bmscValue.substring(0 , ak_bmscValue.length() -1);
                            ak_bmscValue = ak_bmscValue.split(";")[0];
                            Log.println(Log.INFO, null, "Obtained cookie " + ak_bmscKey + " is: " + ak_bmscValue);
                            cookiesHashMap.put(ak_bmscKey, ak_bmscValue);
                        }
                    }
                }
                getPricesPeriodWizzair(apiUrlWizzair, cookiesHashMap);
                return super.parseNetworkResponse(response);
            }
        };
        queue.add(stringRequestForCookie);
    }

    private void getPricesPeriodWizzair(String apiUrlWizzair, final HashMap cookiesHashMap) throws JSONException {

        String urlGetCheapestForVolley = apiUrlWizzair + "/Api/search/timetable";

        String bodyString2 = "{\n" +
                "\t\"adultCount\": 1,\n" +
                "\t\"childCount\": 0,\n" +
                "\t\"flightList\": [{\n" +
                "\t\t\"arrivalStation\": \"SPU\",\n" +
                "\t\t\"departureStation\": \"WAW\",\n" +
                "\t\t\"from\": \"2020-06-01\",\n" +
                "\t\t\"to\": \"2020-09-01\"\n" +
                "\t}],\n" +
                "\t\"infantCount\": 0,\n" +
                "\t\"priceType\": \"regular\"\n" +
                "}";

        JSONObject bodyJson = new JSONObject(bodyString2);

        RequestQueue queue = Volley.newRequestQueue(this);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                (Request.Method.POST, urlGetCheapestForVolley, bodyJson, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject responseJSONObject) {
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/json; charset=utf-8");
                params.put("Cookie", ak_bmscKey + "=" + cookiesHashMap.get(ak_bmscKey));
                params.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36");
                return params;
            }
        };
        queue.add(jsonObjectRequest);
    }
...