Z
Z
zebox2016-07-26 14:17:32
Java
zebox, 2016-07-26 14:17:32

How to send SMS via HttpClient in Java from Yandex Maps page?

Good afternoon!
Please advise in solving the problem of sending SMS with a link to Yandex.Maps (from the service page) using Java.
As I understand it, every time the page is refreshed, a csfrToken is generated, which is subsequently used in a POST request to send SMS.
The problem is that I get csfrToken, but it seems that with every POST or GET it changes (probably I do not fully understand the ApacheHttpClient mechanism).
I am trying to implement using the following code:

public void WebRequest() throws Exception{
        CookieHandler.setDefault(new CookieManager());
        RequestConfig globalConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.DEFAULT)
                .build();
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultRequestConfig(globalConfig)
                .build();
        RequestConfig localConfig = RequestConfig.copy(globalConfig)
                .setCookieSpec(CookieSpecs.STANDARD_STRICT)
                .build();


        HttpGet request = new HttpGet("https://yandex.ru/maps");
        HttpPost send = new HttpPost("https://yandex.ru/maps/api/sms/sendObject");

        request.setConfig(localConfig);
        send.setConfig(localConfig);

        request.addHeader("Host", "yandex.ru");
        request.addHeader("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0");
        request.addHeader("Accept", "*/*");
        request.addHeader("Accept-Language", "en-US,en;q=0.8,ru;q=0.6");
        request.addHeader("Accept-Encoding", "gzip, deflate, br");
        request.addHeader("Origin", "https://yandex.ru");
        request.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        request.addHeader("X-Requested-With", "XMLHttpRequest");
        request.addHeader("Referer", "https://yandex.ru/maps/35/https://yandex.ru/maps/35/myCity/?text=someURL-WithSomeParams");
        request.addHeader("Connection", "keep-alive");

        HttpResponse result = httpclient.execute(request);

        setCookies(result.getFirstHeader("Set-Cookie") == null ? "" :
        result.getFirstHeader("Set-Cookie").toString());

        BufferedReader rd = new BufferedReader(new InputStreamReader(result.getEntity().getContent()));
        String line = "";

        while ((line = rd.readLine()) != null) {
            System.out.println(line);
            int pos=line.indexOf("csrfT");
            tokenStr=line.substring(pos,pos+67).split("\"");
            System.out.println(tokenStr[2]);
            pos=line.indexOf("sessionId");
            sessionId=line.substring(pos,pos+33).split("\"");
            System.out.println(sessionId[2]);
        }

//------------ОТПРАВКА--------------------

        send.addHeader("Host", "yandex.ru");
        send.addHeader("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0");
        send.addHeader("Accept", "*/*");
        send.addHeader("Accept-Language", "en-US,en;q=0.8,ru;q=0.6");
        send.addHeader("Accept-Encoding", "gzip, deflate, br");
        send.addHeader("Origin", "https://yandex.ru");
        send.addHeader("Cookie", getCookies());
        send.addHeader("Content-Type", "application/json; charset=UTF-8");
        send.addHeader("X-Requested-With", "XMLHttpRequest");
        send.addHeader("Referer", "https://yandex.ru/maps/35/myCity/?text=someURL-WithSomeParams");
        send.addHeader("Connection", "keep-alive");
      
       String json="{\"lang\":\"ru_RU\",\"gid\":35,\"uri\":\"ymapsbm1://org?oid=XXXXXXXXXX\",\"phone\":\"+7XXXXXXXXXX\",\"csrfToken\":\""+tokenStr[2].trim()+"\",\"sessionId\":\""+sessionId[2].trim()+"\",\"host_config\":{\"hostname\":\"yandex.ru\"}}";
        StringEntity params = new StringEntity(json);
        params.setContentType("application/json");
        send.setEntity(params);
      
        result = httpclient.execute(send);
        rd = new BufferedReader(new InputStreamReader(result.getEntity().getContent()));
        line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
}

public void setCookies(String cookies) {
        this.cookies = cookies;
    }
    public String getCookies() {
        return cookies;
}

As a result, when I try to send, I get an error stating that the current Token does not match the one specified in the POST request.
Please advise what am I doing wrong?
PS I must say right away that I am not going to engage in spam, I need to implement automatic notification of service users when they receive a certain kind of requests (for equipment maintenance). Applications are sent to me by the operator, which are formed by the users themselves.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TOB BOT, 2016-07-26
@TOBBOT

use the sniffer to study the requests that your browser exchanges with the server when sending several SMS.
I recommend the Fiddler2 sniffer.
Check if csrfToken changes with each new request. Most likely it is changing, because this is its main purpose.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question