shaiksha shaiksha - 22 days ago 8
Apache Configuration Question

How to Generate a Http Post request and print the request before sending it using Java

I need to generate HTTP POST request using java the request format is below

<request version="3.9" principal="c1Prov" credentials="c1Prov">
<target name="UserAPI" operation="createUser">
<parameter>
<user>
<name><SUBSCRIBER_ID></name>
<login-name><SUBSCRIBER_LOGIN_ID></login-name>
<password>
<value><SUBSCRIBER_PASSWORD></value>
</password>
<organization>
<qualified-name>/BTCL_FIXED/<WHOLESALE_ORG_NAME></qualified-name>
</organization>
<account><name><SUBSCRIBER_ACCOUNT></name></account>
<profile-set>
<qualified-name>/BTCL_FIXED/<FIXED_PROFILE_SET_NAME></qualified-name>
</profile-set>
</user>
</parameter>
</target>
</request>


I tried the below piece of code but it doesnt print anything.
1. Am i doing it the correct way?
2. why is it not printing anything?

HttpClient httpClient = new HttpClient();
HttpPost post = new HttpPost("http://www.baidu.com");
String xml = "<target name=\"DomainAPI\" operation=\"createDomain\"><parameter><domain><name><DOMAIN_NAME></name><owning-organization><qualified-name>/BTCL_FIXED</qualified-name></owning-organization><profile-set><qualified-name>/BTCL_FIXED/Wholesale Fixed Dom PS</qualified-name><type>domain</type></profile-set></domain></parameter></target>";
HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
post.setEntity(entity);
System.out.println(post.Entity());

Answer

entity = post.getEntity().getContent() - and you'll get an InputStream for an entity. Get a string from it:

String str = EntityUtils.toString(entity);

Use a StringEntity instead of ByteArrayEntity:

new StringEntity(xml, Charset.defaultCharset())