gpt4 book ai didi

java - ERROR 411 在 java 中使用 API(POST 方法)

转载 作者:太空宇宙 更新时间:2023-11-04 09:15:13 25 4
gpt4 key购买 nike

我正在开展一个个人项目,以学习如何使用 Rest Web 服务。

我有一个 Visual 中的 API Web 应用程序,它是我的 Controller ,也是与 Oracle 的连接的地方,还有一个 JAVA 中的 Web 应用程序,带有 JSON 库,此外还尝试根据我在研究所学到的知识分层进行操作。

当我发出 GET 请求时,我没有问题,他们给我带来了数据,但是当我作为客户的注册商发出 POST 请求时,问题就开始出现,我在 java 中收到错误 411。

我通过放置“内容长度”来寻找一些为他们工作的解决方案,我不知道我是否正确,但我仍然有问题。

    public int insertarCliente(Cliente c){
globalURL += "?rut=" + c.getRut() + "&nom="+ c.getNombre() +"&app=" + c.getApellidoP() + "&apm=" + c.getApellidoM();
try {
HttpURLConnection conn = Conectar(globalURL);
conn.setRequestMethod("POST");
conn.setRequestProperty("ACCEPT", "application/json");
conn.setRequestProperty("Content-Length", "0");

if (conn.getResponseCode() == 200) {
//InputStreamReader in = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String resp = br.readLine();
JSONObject obj = new JSONObject(resp);
return obj.getInt("resp");
}
} catch (Exception e) {
Logger.getLogger(ClienteDAO.class.getName()).log(Level.SEVERE, null, e);

}
return 0;
}

问题从 IF 开始。

向我显示的错误如下:

     Glook2 was successfully deployed in 227 milliseconds.
**Grave: java.lang.RuntimeException: Failed : HTTP Error code : 411**
at Controllers.ClienteDAO.insertarCliente(ClienteDAO.java:50)
at Services.cliente.registrar(cliente.java:104)
at Services.cliente.processRequest(cliente.java:46)
at Services.cliente.doPost(cliente.java:77)

我必须强调,我已经在 POSTMAN 中证明了 Web 服务方法可以正常工作并将数据正确添加到数据库中。


String globalURL = "http://localhost:60367/api/Cliente";
HttpURLConnection conn;

public ClienteDAO() {
conn = Conectar(this.globalURL);
}

private HttpURLConnection Conectar(String urlRest) {
try {
URL url;
url = new URL(urlRest);
return (HttpURLConnection) url.openConnection();
} catch (Exception e) {
Logger.getLogger(ClienteDAO.class.getName()).log(Level.SEVERE, null, e);
}
return null;
}

最佳答案

启用日志记录,如下所示:How to enable wire logging for a java HttpURLConnection traffic?

然后您将看到1 Content-Length header 未发送:

FINE: sun.net.www.MessageHeader@4bf558aa5 pairs:
{POST / HTTP/1.1: null}
{ACCEPT: application/json}
{User-Agent: Java/13}
{Host: localhost:8080}
{Connection: keep-alive}

1:示例日志条目,经过包装以便于阅读

这是因为 HttpURLConnection 管理该 header 。

要发送 Content-Length: 0 header ,请不发送输出,即替换:

conn.setRequestProperty("Content-length", "0");

与:

conn.setDoOutput(true);
conn.getOutputStream().close();

日志记录现在显示标题:

FINE: sun.net.www.MessageHeader@5fa7e7ff7 pairs:
{POST / HTTP/1.1: null}
{ACCEPT: application/json}
{User-Agent: Java/13}
{Host: localhost:8080}
{Connection: keep-alive}
{Content-type: application/x-www-form-urlencoded}
{Content-Length: 0}
<小时/>

另请参阅:JDK-6997628: HttpURLConnection strips Content-Length header on Post :

Affects Version/s: 6u22
Status: Open
Resolution: Unresolved

BT2:EVALUATION

The fix for CR 6961084 restricts the setting of some potentially security sensitive headers. Since these headers were allowed to be set in previous releases then of course compatibility is effected. A decision was made that compatibility was secondary to the security risk these headers posed. We understand that there may be valid apps out there that will be effected by this, so the sun.net.http.allowRestrictedHeaders property was added to revert to previous behavior.

BT2:WORK AROUND

Run with -Dsun.net.http.allowRestrictedHeaders=true

建议使用该解决方法。

关于java - ERROR 411 在 java 中使用 API(POST 方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59112393/

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