无法发送X-CSRF-令牌以发布方法httpurlconnection

时间:2018-07-27 07:38:27

标签: java post httpurlconnection csrf-protection httpsurlconnection

希望您能帮我解决X-CSRF TOKEN问题。

这是我的代码段:

URL url =新URL(MyURL / MAIN / Userinfo)

字符串编码= Base64.getEncoder()。encodeToString(username:password).getBytes(“ UTF-8”));

    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization", "Basic " + encoding);
    connection.setRequestProperty("x-csrf-token", "fetch");
    connection.setRequestMethod("GET");
    connection.setDoOutput(false);
    System.out.println("Response code " + connection.getResponseCode());
    System.out.println(connection.getHeaderFields());
    String crsfToken= connection.getHeaderField("x-csrf-token");

URL url2 = new URL(MyURL/MAIN");
    HttpsURLConnection connection2 = (HttpsURLConnection) url2.openConnection();
    connection2.setDoOutput(true);
    connection2.setRequestProperty("Authorization", "Basic " + encoding);
    connection2.setRequestMethod("POST");
    connection2.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection2.setRequestProperty("x-csrf-Token", server);

我收到错误403,这是禁止的,当我检查connection2的标头字段时,我发现我的csrf令牌丢失了。

0 个答案:

没有答案