我如何使用 HttpURLConnection 原始 post

How can I raw post using HttpURLConnection

我需要原始 post 到授权系统。

POST /v1 HTTP/1.1
Host: api.auth.gg
Content-Type: application/x-www-form-urlencoded
Content-Length: 124

type=login&aid=76471&apikey=156444483727231153&secret=aIGeWaR4YHR3LBCvtr4yOtDlb0HI4MA0gBL&username=demo&password=demo&hwid=demo

我试过这段代码(我用 gson JSON)

public int LoginWithUserPass(String user, String pass) throws Exception {
        URL url = new URL("https://api.auth.gg/v1/");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", "LoginSystem");
        con.addRequestProperty("Content-Type", "Content-Type: application/x-www-form-urlencoded");
        JsonObject auth = new JsonObject();
        auth.addProperty("type", "login");
        auth.addProperty("hwid", getHWID());
        auth.addProperty("password", pass);
        auth.addProperty("username", user);
        auth.addProperty("secret", "test");
        auth.addProperty("apikey", apikey);
        auth.addProperty("aid", "test");

        OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
        wr.write(auth.toString());
        wr.flush();

        if (con.getResponseCode() == 200) {
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            JsonParser parser = new JsonParser();
            JsonElement element = parser.parse(sb.toString());
            if (!element.getAsJsonObject().get("result").getAsString().equalsIgnoreCase("failed")) {
                System.out.println("Successfully Logged in!");
            } else {
                System.out.println(element.getAsJsonObject());
                return -1;
            }

        }

        return con.getResponseCode();
    }

它returns {"result":"failed","message":"Invalid type"}

您的示例显示了查询字符串,而不是 JSON 字符串。因此,无需创建 JSON 对象,只需将您的参数写入流即可。

    OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
    wr.append("type").append("=").append("login");
    wr.append("&").append("hwid").append("=").append(getHWID());
    ...

已修复

public String loginWithUserPass(String user, String pass) throws Exception {
        String url = "https://api.auth.gg/v1/", charset = StandardCharsets.UTF_8.name();
        String hwid = getHWID(), secret = "*secret*", aid = "*aid*";

        String query = String.format("type=login&hwid=%s&password=%s&username=%s&secret=%s&apikey=%s&aid=%s",
                URLEncoder.encode(hwid, charset),
                URLEncoder.encode(pass, charset),
                URLEncoder.encode(user, charset),
                URLEncoder.encode(secret, charset),
                URLEncoder.encode(apikey, charset),
                URLEncoder.encode(aid, charset));

        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Accept-Charset", charset);
        connection.setRequestProperty("User-Agent", "LoginSystem");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
        try (OutputStream output = connection.getOutputStream()) {
            output.write(query.getBytes(charset));
        }

        InputStream response = connection.getInputStream();
        if (connection.getResponseCode() == 200) {
            BufferedReader br = new BufferedReader(new InputStreamReader(response));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            JsonParser parser = new JsonParser();
            JsonElement element = parser.parse(sb.toString());
            return element.getAsJsonObject().get("result").getAsString();

        }
        return null;
    }