嗯,我在 Postman 那里有个请求:
127.0.0.1:8000/empleado/615050/retrieve-update/
我加上这个身体:
{"nombre":Marc}
在django的api开发中,这个请求对我有效
但是,当我尝试在java桌面应用程序中复制这个请求时,失败了,什么也没有发生。这是我的java代码:
public void sendJson() {
JsonObjectBuilder o = Json.createObjectBuilder();
o.add("contrasenia", txtPass.getText());
JsonObject contra = o.build();
String contraS = contra.toString();
System.out.println(contraS);
try {
URL url = new URL("http://localhost:8000/empleado/" + txtUsuario.getText() + "/retrieve-update/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("PATCH");
conn.setRequestProperty("Content-Type","application/json;charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream os = conn.getOutputStream();
os.write(contraS.getBytes(StandardCharsets.UTF_8));
os.close();
} catch (MalformedURLException malformedURLException) {
malformedURLException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
思想?谢谢,对不起我的英语
1条答案
按热度按时间llmtgqce1#
这是因为,不幸的是,
PATCH
不是的法定动词HttpURLConnection
有关详细信息,请参阅javadocHttpURLConnection#setRequestMethod
```Set the method for the URL request, one of:
GET
POST
HEAD
OPTIONS
PUT
DELETE
TRACE
are legal, subject to protocol restrictions. The default method is GET.