我正在尝试使用 JAX-RS(确切地说是 RESTEasy)解析来自PRTG HTTP REST API的响应。
我遇到的问题是来自 API 的结果作为附件附加到响应中,而不是在正文中。它还被赋予了 HTML 的内容类型,而不是 XML(就像它一样)。我似乎无法让 JAX-RS 将 XML 从附件中提取出来并最好传递给 JAXB 进行编组。
我尝试了很多东西,但我最新的是:
client = ClientBuilder.newBuilder()
.sslContext(this.getNonCertificationValidationSSLContext())
.hostnameVerifier(this.getNonHostnameVerifier())
.build();
UriBuilder builder = UriBuilder.fromUri("https://").host(this.getPRTGHost()).path(DEFAULT_QUERY_TABLE_PATH);
final WebTarget baseTarget = client.target(builder.build()).queryParam("username", this.getQueryUserName())
.queryParam("passhash", this.getPasswordHash()).queryParam("output", "xml").queryParam("count", 2000);
devicesTarget = baseTarget.queryParam("content", "devices").queryParam("columns", this.getDevicesCols());
Response response = devicesTarget.request().get();
try ( final InputStream inputStream = response.readEntity(InputStream.class) ) {
StringBuilder prtgXmlStrBuilder = new StringBuilder();
byte[] buffer = new byte[1024];
int bytesRead;
while ( ( bytesRead = inputStream.read(buffer) ) != -1 ) {
prtgXmlStrBuilder.append(new String(buffer, StandardCharsets.UTF_8));
}
System.out.println(prtgXmlStrBuilder.toString());
}
我也试过:
try ( BufferedReader br = new BufferedReader(
new InputStreamReader(
response.readEntity(InputStream.class))) ) {
String output;
while ( ( output = br.readLine() ) != null ) {
prtgXmlStrBuilder.append(output);
}
}
理想情况下,它只是:
final JAXBContext context = JAXBContext.newInstance(PRTGDeviceResult.class, PRTGItem.class);
final Unmarshaller um = context.createUnmarshaller();
final PRTGDeviceResult deviceResult = (PRTGDeviceResult) um
.unmarshal(response.readEntity(InputStream.class));
我发送的请求如下所示:
GET /api/table.xml? username=prtgadmin&passhash=xxxxxx&content=sensors&columns=device%2C+type%2C+sensor%2C+status%2C+lastvalue%2C+group%2C+lastdown%2C+lastcheck%2C+lastup,message&output=xml
HTTP/1.1
Host: 10.xx.xxx.xxx
Accept: application/xml
user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) advanced-rest-client/12.1.4 Chrome/61.0.3163.100 Electron/2.0.2 Safari/537.36
回复看起来像:
connection:
close
content-type:
text/html; charset=UTF-8
content-length:
134918
date:
Fri, 12 Oct 2018 21:37:25 GMT
expires:
0
cache-control:
cache, must-revalidate
x-content-type-options:
nosniff
x-xss-protection:
1; mode=block
content-disposition:
attachment; filename=table.xml
pragma:
public
server:
PRTG/18.3.44.2059
任何想法将不胜感激,谢谢。
编辑:
对我的 InputStream 有一些帮助
try ( final InputStream inputStream = response.readEntity(InputStream.class) ) {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
byte[] block = new byte[1024];
int bytesRead;
while ( ( bytesRead = inputStream.read(block) ) != -1 ) {
buf.write(block, 0, bytesRead);
}
String xml = buf.toString("UTF-8");
System.out.println(xml);
}
这是我收到的日志消息(到目前为止有帮助)
18024 [Thread-2] DEBUG org.apache.http.impl.conn.BasicClientConnectionManager - Get connection for route {s}->https://10.XX.XXX.XXX:443
18025 [Thread-2] DEBUG org.apache.http.impl.conn.DefaultClientConnectionOperator - Connecting to 10.XX.XXX.XXX:443
18041 [Thread-2] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: default
18041 [Thread-2] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
18041 [Thread-2] DEBUG org.apache.http.client.protocol.RequestTargetAuthentication - Target auth state: UNCHALLENGED
18042 [Thread-2] DEBUG org.apache.http.client.protocol.RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
18042 [Thread-2] DEBUG org.apache.http.impl.client.DefaultHttpClient - Attempt 1 to execute request
18042 [Thread-2] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Sending request: GET /api/table.xml?username=prtgadmin&passhash=XXXXXXXXXX&output=xml&count=2000&content=devices&columns=objid%2Cgroup%2Cdevice%2Chost%2Cstatus HTTP/1.1
18042 [Thread-2] DEBUG org.apache.http.wire - >> "GET /api/table.xml?username=prtgadmin&passhash=XXXXXXXXX&output=xml&count=2000&content=devices&columns=objid%2Cgroup%2Cdevice%2Chost%2Cstatus HTTP/1.1[\r][\n]"
18042 [Thread-2] DEBUG org.apache.http.wire - >> "Accept-Encoding: gzip, deflate[\r][\n]"
18042 [Thread-2] DEBUG org.apache.http.wire - >> "Host: 10.XX.XXX.XXX[\r][\n]"
18042 [Thread-2] DEBUG org.apache.http.wire - >> "Connection: Keep-Alive[\r][\n]"
18042 [Thread-2] DEBUG org.apache.http.wire - >> "[\r][\n]"
18042 [Thread-2] DEBUG org.apache.http.headers - >> GET /api/table.xml?username=prtgadmin&passhash=XXXXXXX&output=xml&count=2000&content=devices&columns=objid%2Cgroup%2Cdevice%2Chost%2Cstatus HTTP/1.1
18042 [Thread-2] DEBUG org.apache.http.headers - >> Accept-Encoding: gzip, deflate
18042 [Thread-2] DEBUG org.apache.http.headers - >> Host: 10.XX.XXX.XXX
18042 [Thread-2] DEBUG org.apache.http.headers - >> Connection: Keep-Alive
18085 [Thread-2] DEBUG org.apache.http.wire - << "HTTP/1.1 200 OK[\r][\n]"
18085 [Thread-2] DEBUG org.apache.http.wire - << "Connection: close[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.wire - << "Content-Encoding: deflate[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.wire - << "Content-Type: text/html; charset=UTF-8[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.wire - << "Content-Length: 489[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.wire - << "Date: Mon, 15 Oct 2018 15:47:09 GMT[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.wire - << "Expires: 0[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.wire - << "Cache-Control: cache, must-revalidate[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.wire - << "X-Content-Type-Options: nosniff[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.wire - << "X-XSS-Protection: 1; mode=block[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.wire - << "Content-Disposition: attachment; filename=table.xml[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.wire - << "Pragma: public[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.wire - << "Server: PRTG/18.3.44.2059[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.wire - << "[\r][\n]"
18086 [Thread-2] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Receiving response: HTTP/1.1 200 OK
18086 [Thread-2] DEBUG org.apache.http.headers - << HTTP/1.1 200 OK
18086 [Thread-2] DEBUG org.apache.http.headers - << Connection: close
18086 [Thread-2] DEBUG org.apache.http.headers - << Content-Encoding: deflate
18087 [Thread-2] DEBUG org.apache.http.headers - << Content-Type: text/html; charset=UTF-8
18087 [Thread-2] DEBUG org.apache.http.headers - << Content-Length: 489
18087 [Thread-2] DEBUG org.apache.http.headers - << Date: Mon, 15 Oct 2018 15:47:09 GMT
18087 [Thread-2] DEBUG org.apache.http.headers - << Expires: 0
18087 [Thread-2] DEBUG org.apache.http.headers - << Cache-Control: cache, must-revalidate
18087 [Thread-2] DEBUG org.apache.http.headers - << X-Content-Type-Options: nosniff
18087 [Thread-2] DEBUG org.apache.http.headers - << X-XSS-Protection: 1; mode=block
18087 [Thread-2] DEBUG org.apache.http.headers - << Content-Disposition: attachment; filename=table.xml
18087 [Thread-2] DEBUG org.apache.http.headers - << Pragma: public
18087 [Thread-2] DEBUG org.apache.http.headers - << Server: PRTG/18.3.44.2059
18092 [Thread-2] DEBUG org.apache.http.wire - << "[0xc5][0x96]]O[0xc2]0[0x14][0x86][0xef]M[0xfc][0xf][0xcd][0xae][0xf0]b[0x1f][0x1d]3@RgD[0xd1][0x1b][0x85]e`[0xbc]4skF[\r][0xac]d[0xeb]@[0xff][0xbd][0xfd]"[0xa2][0xe9][0xa6][0x12][0xc5][0xab][0xb5]{O[0xb7]>}[0xcf]9):Y.[0xc0][0x1a][0x97][0x15][0xa1][0xc5][0x99][0x5][0x1d][0xcf][0x2][0xb8]HiF[0x8a][0xfc][0xcc][0xba][0x9f]][0xdb]}[0xeb]<<>[0x2][0x0]exMR\[0x1]FY[0xb2]Hi]0[0x1e][0xf]-[0xb0] [0x15][0xc3]E[0xc6]'[0x96][0xc][0x4]hU[0xb2][0xdc][0xd6][0xdf][0xc]a[0xdf][0xe9]:A[0xe0][0xf8][0xde][0xe9][0x0][0xb9][0x1f]$[0xf1]Y[0x80][0x8][0xc3]K[0xb5][0x10] [0xfa][0xf4]L[0xb2][0xd0][0x87]A[0xf][0xb9]j,c[0x0][0xca]KZ[0xaf][0xc2][[0x9a]&[0xb][0x10][0x95][0xf4][0x9]#W[0xbd][0xd2][0xba][0xda]\[0x18][0xc5][0xb3][0x9b]([0x9e][0xc]G[0xc8][0xd5]o[0xb4]>[0xa7][0x15][0xdb]U[0xe5]\k[0x15]KX][0x85][0xf7]+[0x80]\=[0xfe][0xa0]<[0x96][0xc9]&[0xec]n59[0x93]:r[0xdf][0xb7]n[0xa4][0x10][0xc0][0x6][\n]"
18093 [Thread-2] DEBUG org.apache.http.wire - << "[0xe8]9[0xfc][0x9c][0xcd][0x0][0xc3][0xf1][0xe4][0xd2][0x8e]J[0xb2]L[0xca]W[0xfb][0xe6][0x1]tT[0xb0][0x3]O[0x8c]D[[0x15][0xb9][0x87][0x0][0xf2]![0xdf]u[0x3][0xd0][0xcf][0x88]xt[0x1b][0x92][0x94][0x8d]LQRW8[0x3][0x9d][0x95]|[0xf2]Ci4[0x8c]g[0x90][0xd2][0xbe]o[0x98][0xf][0xe1][0x9e]|[0x17])#kl[0x8f][0xb2][0x1c]+[0xcb][0x4][0x81][0xdf][0xec][0x99][0x94][0xff][0x3][0x90][0xa7][0xf1]^[0x6][0x1a][0x1][0xa1][0xd7]N([0xd2][0xc5][0x90][0x97][0xeb][0xe1][0xa9][0xb9]u[0x88]J[0xe1][0xa7]n[0xae]:-:[0x83][0xc6]"[0x13][0x86]q[0xf5][0xd0]4[0xdd][0xbe]o4,[0xa6]5[0xe3]=[0xbb][0x99]F[0xd4][0x17][0xe8]|[0xea]&[0xcd]n[0x89]p#[0xdd][0xaf]7[0xc5][0xee][0xc0][0xec][0xcf]tCX:[0xc7]-H[0xc2][0x2][0xe8]i[0xa8][0x9d]|l[0xa6]R+[0xe][0xc3][0x15][0xf4][0xcc][0xbd][0xb1][0x9d]K[0x1a][0xa4]Y[0xc6]wS[\r][0xc7]G[0xb6]Z[0xd7][0xc6][0x6]{[0xe]O[0x8e]Cgd[0xd0][0xf][0xf6][0xcb]HYA[0x1a]p:[0xba][0xb4][0xf3][0x84][0xe1]M[0xf2]j[0xc7][0xb3][0xb8][0x8d][0xb2][0xa9][0xec]~;1[0x3][0xb3]}_[0xdf]8[0xc4]e[0x4]\[0xc9][0xbb][0x91][0xb9]{[0xf8]=[0x9e][0xb8]Q_[0xdb][0xdf]U[0xe1][0x1b]"
18093 [Thread-2] DEBUG org.apache.http.impl.conn.BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@2a3a6aca
18093 [Thread-2] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Connection 0.0.0.0:58650<->10.XX.XXX.XXX:443 shut down
?]O?0??M???b3@RgD??e`?4skF
?d?@???"????{O?>}?9):Y.??????HiF????]?}?<<>