gpt4 book ai didi

java - 如何使用内容类型 XML 和查询参数触发 HTTP post?

转载 作者:可可西里 更新时间:2023-11-01 16:26:54 25 4
gpt4 key购买 nike

我正在尝试触发 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();
}
}

非常感谢!

最佳答案

好的,您要处理两个不同的问题:

  1. 正文的内容类型。在这种情况下,如果您的正文是 XML 正文,那么它应该是:

    Content-Type: application/xml; charset="utf-8"
  2. 您的查询参数被剥离(或忽略)的事实。这是一个更复杂的问题。当通过 HTTP Post 提交表单时,它通常作为查询字符串参数发送,这看起来像是可能发生的情况。你能得到正在发送的内容的线路跟​​踪吗?您可能需要自己解析参数,而不是尝试将它们解析为表单主体。

如果您需要发送两种不同类型的数据,您可以考虑发送类似多部分表单的内容,或者发送一种主体类型,并添加额外的数据作为标题。

关于java - 如何使用内容类型 XML 和查询参数触发 HTTP post?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18272154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com