作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在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/
我是一名优秀的程序员,十分优秀!