body 中 POST 和 xml 的思科 ISE REST HttpUrlConnection 400 错误请求

Cisco ISE REST HttpUrlConnection 400 Bad Request with POST and xml in body

我目前正在开发一个程序,该程序从 csv 文件中获取 4 个值(MAC、策略、IdentityGroup 和描述)并将它们作为端点写入 Cisco ISE。

用户凭据和 URL 是通过一个小 GUI 获取的。

首先,我通过工作正常的 GET 请求和 return 映射到组名的组 ID 映射来检查文件中给出的 IdentityGroups 是否有效。

然后我尝试通过发送 POST 请求来创建端点,请求中包含 Body 中的 XML 所需信息:

URL readURL = new URL(url.getProtocol() + "://" + url.getHost() + ":" + 

url.getPort() + "/ers/config/Endpoint");
HttpURLconnection con;
con = (HttpURLConnection) readURL.openConnection();
con.setRequestProperty("Authorization","Basic "+ Base64.getEncoder().encodeToString((username + ":" + password.getBytes("utf-8")));
con.setRequestProperty("Accept", "application/vnd.com.cisco.ise.identity.Endpoint.1.0.xml");
con.setRequestProperty("Accept-Charset","utf-8");
con.setRequestProperty("Content-Type","application/vnd.com.cisco.ise.identity.Endpoint.1.0+xml");
con.setDoOutput(true)
con.setDoInput(true);
con.connect();

System.out.println("All Properties set.");

OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream(), "UTF-8");

System.out.println("Got Outputstream.");

System.out.println("The entry to write: "+e.toString());

writer.write("<ns3:endpoint name=\"" + e.getMAC()
        + "\" id=\"12341234-12341234-asdasdf-12341\" description=\"" + e.getDescription()
        + "\" xmlns:ns2=\"ers.ise.cisco.com\" xmlns:ns3=\"identity.ers.ise.cisco.com\"><groupId>"
        + groupIDs.get(e.getEndpointIdentifyGroup())    
        + "</groupId><identityStore></identityStore><identityStoreId></identityStoreId><mac>" + e.getMAC()      
        + "</mac><portalUser></portalUser><profileId></profileId><staticGroupAssignment>true</staticGroupAssignment><staticProfileAssignment>false</staticProfileAssignment>");

writer.flush();

System.out.println("MAC " + e.getMAC() + " was sent.");
writer.close();

System.out.println("All entries were written.");

System.out.println(con.getResponseMessage());

控制台输出:

All Properties set.
Got Outputstream.
The entry to write: CSVEntry [endpointPolicy=StringProperty [value: test], endpointIdentifyGroup=StringProperty [value: test], MAC=StringProperty [value: 11:11:11:11:11:11], description=StringProperty [value: tes>_phe]]
MAC 11:11:11:11:11:11 was sent.
All entries were written.
Bad Request
java.io.IOException: Server returned HTTP response code: 400 for URL: https://193.186.11.135:9060/ers/config/Endpoint
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
.....
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://193.186.11.135:9060/ers/config/Endpoint
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1839)
....

我已经尝试了多种方法,例如不同的 headers,写成多行,检查值 500 次,但我不知道为什么我会收到 400 错误请求响应。

我还尝试过的另一件事是检查请求是否正确,方法是使用 Postman 并发送具有完全相同 headers 和完全相同 body 的 POST 请求到服务器并且它工作(创建 201 并显示在数据库中)。

我希望有人能帮助我,因为我不知道该怎么做,我在谷歌上搜索解决方案,但我尝试的所有方法都没有奏效。

在此先感谢您的帮助。

好吧,我发现错误了,我很惭愧。我忘了用

关闭 xml 正文
</ns3:endpoint>

所以它没有用。它现在工作得很好。