java - How to fire HTTP post with content type XML along with query parameters? -
i trying fire post request. data sending xml format (i doing via jaxb), have send along request parameter. problem regarding content-type (i've added comments in code). 1 should use ?
see below code :
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"); // app/xml ? seems query param not added request connection.setrequestproperty("content-type", "application/xml"); // app/query param ? seems xml not added 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); } { 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(); } } thanks lot!
ok, there's 2 different issues you're dealing with:
1) content-type of body. in case, if body xml body, should be:
content-type: application/xml; charset="utf-8" 2) fact query parameters being stripped off (or ignored). more complicated problem. when form submitted via http post, sent query string params, , looks might happening. can wire trace of what's being sent? may you'll need parse parameters off instead of trying parse them form body.
if need send 2 different types of data, may consider sending multipart form, or sending 1 body type, , adding data header.
Comments
Post a Comment