简体   繁体   English

使用HttpClient - Java发送带有HTTP POST请求的XML有效负载

[英]Sending XML payloads with HTTP POST request using HttpClient--Java

So I've been doing a lot of looking on both stackoverflow and google in general to try and answer my following question, but I have been unable to find anything that can help me to get this work 100%. 所以我一直在做很多关于stackoverflow和谷歌一般的尝试回答我的下面的问题,但我一直无法找到任何可以帮助我100%完成这项工作的东西。 I'm pretty sure I have everything except a SMALL error correct, but obviously you guys probably have suggestions anyway, so go for it! 我很确定除了SMALL错误之外我还有其他一切,但显然你们可能还有建议,所以去吧!

And, here we go: I've been using HTTPClient to test an API on a few different environments, and I got HTTPPost methods to accept JSON payloads but now I'm trying to send payloads using XML and I'm running into some issues. 而且,我们开始:我一直在使用HTTPClient在几个不同的环境中测试API,我使用HTTPPost方法接受JSON有效负载,但现在我正在尝试使用XML发送有效负载而我遇到了一些问题。 It seems that the XML string that I'm creating (in the code below) is correct... so I'm stumped as to why this isn't working. 似乎我正在创建的XML字符串(在下面的代码中)是正确的...所以我很难过为什么这不起作用。 ALSO: got most of the DOM code from the internet (to build up the XML payload) so feel free to bring that into question as well... 另外:从互联网上获取大部分DOM代码(以构建XML有效负载),所以请随意提出这个问题......

My code is the following: 我的代码如下:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

Document doc = docBuilder.newDocument();
Element subscription = doc.createElement("subscription");
doc.appendChild(subscription);

subscription.setAttribute("email", "patricia@test.intershop.de");
etc....
etc....
etc....
etc....

DOMSource domSource = new DomSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);

String XMLpayload = writer.toString();

[name of my HttpRequest].setEntity(new StringEntity(XMLpayload));
[name of my HttpResponse] = client.execute(request);

now... I'm looking to achieve the payload seen BELOW: 现在......我正在寻找下面看到的有效载荷:

<subscription>
    <email>patricia@test.intershop.de</email>
    <firstName>Patricia</firstName>
    <lastName>Miller</lastName>
    <title>Ms.</title>
    <gender>Female</gender>
</subscription>

When I print out the payload that I am sending currently it looks like the following: 当我打印出我当前发送的有效负载时,它看起来如下所示:

?xml version="1.0" encoding="UTF-8" standalone="no"? ?xml version =“1.0”encoding =“UTF-8”standalone =“no”? subscription email="patricia@test.intershop.de" firstName="Patricia" gender="Female" lastName="Miller" title="Ms."/ 订阅电子邮件=“patricia@test.intershop.de”firstName =“Patricia”gender =“Female”lastName =“Miller”title =“Ms。”/

(NOTE: I REMOVED THE < AND > BRACKETS. THEY APPEAR WHERE THEY SHOULD!) (注意:我删除了<AND>支架。他们应该出现在哪里!)

But, I'm receiving a 400 error. 但是,我收到400错误。 Any ideas here? 这里有什么想法? I know I have the proper headers, the URL is correct, etc. It's absolutely something with what I'm doing with the payload. 我知道我有正确的标题,URL是正确的等等。这绝对与我正在使用的有效负载有关。 Any thoughts would be greatly appreciated!! 任何想法将不胜感激!!

Best! 最好!

In your expected payload, 'email','firstname',etc.. are child elements of Subscription element. 在您预期的有效负载中,“email”,“firstname”等是Subscription元素的子元素。 As per the code, they are added as attributes of your 'subscription' element. 根据代码,它们被添加为“订阅”元素的属性。 If you need 'email','firstname',etc.. as child elements, you should use appendChild() instead of setAttribute(). 如果你需要'email','firstname'等作为子元素,你应该使用appendChild()而不是setAttribute()。

Element email = doc.createElement("email");
email.appendChild(document.createTextNode("patricia@test.intershop.de"));
subscription.appendChild(email);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM