错误 411 在 java 中消耗 API(POST 方法)

ERROR 411 consuming API in java (POST METHOD)

我正在从事个人项目以学习如何使用 Rest Web 服务。

我有一个 Visual 中的 API Web 应用程序,它是我的控制器,也是与 Oracle 建立连接的地方,还有一个 JAVA 中的 Web 应用程序和一个 JSON 库,除了根据自己在研究所学到的东西,尽量分层做。

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

我阅读了通过放置 "Content-Length" 寻找一些为他们工作的解决方案我不知道我是否正确,但我仍然有问题。

    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();

记录现在显示 header:

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

推荐使用该解决方法。