0

In html it looks like that:

$.ajax({
   url : 'http://www.mclista.pl/json/daj_diax/',
   type : 'POST',
   data : {
       'id_serwera' : '39914',
       'csrf_mclista_token' : 'b7b96922dba267733168629d9f5ba6d7'
   },
   dataType : 'JSON',
   success : function(result) {
        if (result.status == 'ok') {
            $('#glosow_'+id).html(parseInt($('#glosow_'+id).html()) + 1);
         }else if(result.status == 'juz_glosowal')
                alert('Możesz dać tylko jednego diax-a na jeden serwer.');
        }
    });

but how to send it from Java HttpURLConnection? My Code:

    String url="http://www.mclista.pl/json/daj_diax/";
    URL object=new URL(url);

    HttpURLConnection con = (HttpURLConnection) object.openConnection();
    con.setDoOutput(true);
    con.setDoInput(true);
    con.setRequestProperty("dataType", "JSON");
    con.setRequestMethod("POST");
    JSONObject cred = new JSONObject();
    cred.put("id_serwera", "41673").put("csrf_mclista_token", "587fb64c54defa881d293ee1aadb93fa");
    System.out.println(new String(cred.toString().getBytes(StandardCharsets.UTF_8)));
    OutputStream os = con.getOutputStream();
    os.write(cred.toString().getBytes("UTF-8"));
    os.close();
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
     String inputLine;
     StringBuffer response = new StringBuffer();
     while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
     }
     in.close();
     System.out.println(response.toString());

It returns Disallowed Key Characters. I don't know what to do beacuse I tried almost everything (Remember that from html it is working) Cookies in success http request: COOKIES

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Dadudze
  • 1
  • 2
  • you have a { missing in ajax, try after fixing that – Sinto Aug 15 '18 at 06:04
  • @Sinto http post is working, but java post is not working – Dadudze Aug 15 '18 at 06:05
  • Check this out https://stackoverflow.com/questions/37825618/disallowed-key-characters-error-in-json-output – E141 Aug 15 '18 at 06:11
  • 2
    The AJAX query doesn't send JSON. It sends application/x-www-form-urlencoded form data, i.e. key/value pairs: `id_serwera=39914&csrf_mclista_token=b7b96922dba267733168629d9f5ba6d7`. Also, dataType is not a header sent to the server: it just tells JQuery what to expect in the response. Use your browser dev tools, and see what your browser sends, in the network panel. Read the documentation of JQuery.ajax. – JB Nizet Aug 15 '18 at 06:11
  • your picture have "session_id". Maybe you should have it in Java too? – talex Aug 15 '18 at 06:32
  • @talex I have changed csrf_mclista_token to session_id but it still doesn't work – Dadudze Aug 15 '18 at 06:34

0 Answers0