Ioa*_*oan 2 java http xmlhttprequest jaxb http-headers
我正在尝试触发POST请求。我发送的数据是XML格式的(我通过JAXB进行此操作),但是我必须发送请求参数。我的问题与Content-Type有关(我在代码中添加了一些注释)。我应该使用哪一个?
见下面我的代码:
private V callAndGetResponse(K request, Class<K> requestClassType, Class<V> responseClassType) throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(requestClassType, responseClassType);
Marshaller marshaller = jaxbContext.createMarshaller();
// set properties on marshaller
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, charset);
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", String.format(XML_HEADER_FORMAT, dtd));
marshaller.marshal(request, System.out);
URL wsUrl = new URL(primaryEndpointUrl);
HttpURLConnection connection = openAndPrepareConnection(wsUrl);
tryToMarshallWsRequestToOutputStream(request, marshaller, connection);
printDebug(connection);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object response = unmarshaller.unmarshal(connection.getInputStream());
// cleanup
connection.disconnect();
return responseClassType.cast(response);
}
private HttpURLConnection openAndPrepareConnection(URL wsUrl) throws IOException {
HttpURLConnection connection = (HttpURLConnection) wsUrl.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Accept", "application/xml");
connection.setRequestProperty("Accept-Charset", charset);
// Both types ? Doesn't work
connection.setRequestProperty("Content-Type", "application/xml;application/x-www-form-urlencoded");
// Only app/xml ? it seems that query param is not added to request
connection.setRequestProperty("Content-Type", "application/xml");
// Only app/query param ? it seems that xml is not added to request
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
return connection;
}
private void tryToMarshallWsRequestToOutputStream(K request, Marshaller jaxbForRequestMarshaller,
HttpURLConnection connection) throws JAXBException, IOException {
OutputStream outputStream = null;
try {
outputStream = connection.getOutputStream();
jaxbForRequestMarshaller.marshal(request, outputStream);
addQueryParameters(outputStream);
}
finally {
tryToClose(outputStream);
}
}
private void addQueryParameters(OutputStream outputStream) throws IOException {
String value = "none";
String query = String.format("xmlmsg=%s", URLEncoder.encode(value, charset));
outputStream.write(query.getBytes(charset));
}
protected void tryToClose(OutputStream outputStream) throws IOException {
if (outputStream != null) {
outputStream.close();
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢!
好的,您要处理两个不同的问题:
1)内容的内容类型。在这种情况下,如果您的主体是XML主体,则它应该是:
Content-Type: application/xml; charset="utf-8"
Run Code Online (Sandbox Code Playgroud)
2)您的查询参数被剥离(或忽略)的事实。这是一个更复杂的问题。通过HTTP Post提交表单时,通常将其作为查询字符串参数发送,这看起来可能正在发生。您能获得所发送内容的电报吗?可能是您需要自己解析参数,而不是尝试将其解析为表单主体。
如果需要发送两种不同类型的数据,则可以考虑发送类似多部分形式的内容,或者发送一种正文类型,并添加额外的数据作为标头。
| 归档时间: |
|
| 查看次数: |
21048 次 |
| 最近记录: |