JAVA - 发送post json数据并接收状态代码500

时间:2018-02-15 20:08:47

标签: java post

我发送POST json数据但收到状态码500。 有网站ajax,我不知道我做错了什么.. 我想发送帖子数据为变量添加1个金额并收到回复消息

AJAX:

function daj_diaxa(id){
    $.ajax({
        url : 'http://www.mclista.pl/json/daj_diax/',
        type : 'POST',
        data : {
            id_serwera : id,
            'csrf_mclista_token' : 'f64851282427665bbdb47915bf16956e'
        },
        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.');
        }
    });
};

我的代码是发送JSON数据:

    try{
        StringEntity entity = new StringEntity("data={\"id_serwera\":\"37923\",\"csrf_mclista_token\":\"" + getToken() + "\"} ");
        HttpClientBuilder httpClient = HttpClientBuilder.create();
        httpClient.useSystemProperties();
        HttpClient client = httpClient.build();
        HttpPost request = new HttpPost("http://www.mclista.pl/json/daj_diax/");
        request.addHeader("content-type", "application/json");
        request.addHeader("Accept","application/json");
        request.setEntity(entity);
        HttpResponse response = client.execute(request);
        System.out.println(response.getStatusLine().getStatusCode());
        System.out.println(toString(response.getEntity().getContent()));
    }catch (IOException e){
        e.printStackTrace();
    }

public static String getToken(){
    try{
        String src = getURLSource("http://mclista.pl/");
        String[] strings = src.split(" ");
        int idToken = 0;
        for(String s : strings){
            if(s.contains("'")){
                idToken++;
                if(idToken == 4){
                    System.out.println("csrf_mclista_token = " + s);
                    return s.replaceAll("'", "");
                }
            }
        }
        //System.out.println(getURLSource("http://mclista.pl/"));
    }catch (Exception e){
        e.printStackTrace();
    }
    return null;
}

public static String getURLSource(String url) throws IOException
{
    URL urlObject = new URL(url);
    URLConnection urlConnection = urlObject.openConnection();
    urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

    return toString(urlConnection.getInputStream());
}

private static String toString(InputStream inputStream) throws IOException
{
    try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")))
    {
        String inputLine;
        StringBuilder stringBuilder = new StringBuilder();
        while ((inputLine = bufferedReader.readLine()) != null)
        {
            stringBuilder.append(inputLine);
        }

        return stringBuilder.toString();
    }
}

0 个答案:

没有答案