gpt4 book ai didi

java - 如何用java创建hastebin

转载 作者:行者123 更新时间:2023-12-01 18:09:48 26 4
gpt4 key购买 nike

我需要在java中创建一个hastebin粘贴,但我不知道如何做。我试过这个

public static String paste(String content) throws MalformedURLException, IOException {
URL url = new URL("https://hasteb.in/documents");
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.addRequestProperty("data", content);
http.setRequestMethod("POST");
http.setDoOutput(true);

InputStream in = new BufferedInputStream(http.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

StringBuilder entirePage = new StringBuilder();
String inputLine;
while ((inputLine = reader.readLine()) != null) {
entirePage.append(inputLine);
}
reader.close();
if (!(entirePage.toString().contains("\"key\":\""))) {
return "UNKNOWN";
}
return "https://hasteb.in/"+entirePage.toString().split("\"key\":\"")[1].split("\",")[0];
}

但是它不起作用。错误是Caused by: java.lang.IllegalArgumentException: Illegal character(s) in message header value有什么帮助吗?

最佳答案

要使用 HttpURLConnection 执行 POST 请求,您需要使用提供的 OutputStream 功能编写请求正文,而不是设置名为“data”的 HTTP header :

try (OutputStream out = http.getOutputStream()) {
out.write(content.getBytes());
out.flush();
}

这需要在您开始从 InputStream 读取之前发生(我建议也使用 try-with-resources)。

关于java - 如何用java创建hastebin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60487482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com