java - PostMethod HttpPost HttpClient - post xml with url parameters -
is there way in java post xml using postmethod or httppost along url parameters? doing below, not working.
url - https://mytest.com?z=123&b=abc&c=%10 xml - <test> <data> test xml </data> </test> public string getresponse(string xml) { httpclient client = new httpclient(); // "https://mytest.com?z=123&b=abc&c=%10" string url="https://mytest.com"; postmethod pmethod = new pmethod(url); pmethod.addparameter("z","123"); pmethod.addparameter("b","abc"); pmethod.addparameter("c","%10"); post.setrequestentity(new stringrequestentity(xml, "application/xml", "utf-8")); client.executemethod(pmethod); }
i guess misunderstood question first time. want pass xml url parameter, via post rather including xml directly in url? can this:
import java.net.url; import java.net.httpurlconnection; import java.net.urlencoder; import java.io.bufferedwriter; import java.io.outputstreamwriter; public class xmlposter { public static void main(string[] argv) { try { string xmldata = argv[0]; url url = new url("http://posttestserver.com/post.php"); string myparam = "myparam=" + urlencoder.encode(xmldata, "utf-8"); httpurlconnection httpconn = (httpurlconnection)url.openconnection(); httpconn.setrequestmethod("post"); httpconn.setrequestproperty("content-type", "application/x-www-form-urlencoded"); httpconn.setrequestproperty("content-length", integer.tostring(myparam.length())); httpconn.setdooutput(true); bufferedwriter writer = new bufferedwriter(new outputstreamwriter((httpconn.getoutputstream()))); writer.write(myparam, 0, myparam.length()); writer.flush(); writer.close(); system.out.println(httpconn.getresponsecode()); } catch (exception e) { e.printstacktrace(); } } }
then if run this:
$ java -cp . xmlposter '<thisxmlisfake>but how know?</thisxmlisfake>' 200
and find corresponding file on posttestserver.com should contain this, want:
post params: key: 'myparam' value: '<thisxmlisfake>but how know?</thisxmlisfake>' empty post body. upload contains put data: myparam=%3cthisxmlisfake%3ebut+how+do+you+know%3f%3c%2fthisxmlisfake%3e
Comments
Post a Comment