有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

rest如何在Java中发布此http请求?

在html中,它如下所示:

$.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.');
        }
    });

但是如何从JavaHttpUrlConnection发送它呢? 我的代码:

    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());

它返回不允许的关键字符。我不知道该怎么办,因为我几乎什么都试过了(记住,从html开始它就可以工作了) 成功http请求中的Cookie: COOKIES


共 (0) 个答案