Cisco ISE REST HttpUrlConnection 400错误请求,POST和xml正文

时间:2015-07-22 08:23:56

标签: java xml httpurlconnection cisco bad-request

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

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

首先,我正在检查文件中给出的IdentityGroup是否通过可正常工作的GET请求有效,并返回映射到组名的组ID映射。

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

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)
....

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

我尝试过的另一件事是检查请求是否正确,使用Postman并将具有完全相同的标题和完全相同的主体的POST请求发送到服务器并且它有效(获得201创建并显示在DB)。

我希望有人可以帮助我,因为我不知道该怎么做,我正在谷歌搜索解决方案,但我尝试过的所有功能都没有用。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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

关闭xml体
var data = (from t in ctx.ObjectContext.Widgets
       where t.CampaignId == campaignId && 
             t.CalendarEventId == calendarEventId
             (t.RecurringEventId IS NULL OR t.RecurringEventId = recurringEventId)
       select t);

data = data
    .OrderBy(t => t.Id);
    .Where(t => t.Id >= rows * (page - 1) && t.Id < rows * page )
    .ToList();

所以它不起作用。它现在工作得很好。