Sunday, 15 September 2013

java - Sending POST to servlet with data -



java - Sending POST to servlet with data -

i have used curl submit post request web application written in java using gwt/smartgwt , smartclient. curl command follows:

`curl -v --header "content-type: application/x-www-form-urlencoded; charset=utf-8" --header "custom-userid: ghettosamson-curl1" -d respheaders1.txt -o response-1.txt --data _transaction=%3ctransaction%20xmlns%3axsi%3d%22http%3a%2f%2fwww.w3.org%2f2000%2f10%2fxmlschema-instance%22%20xsi%3atype%3d%22xsd%3aobject%22%3e%3ctransactionnum%20xsi%3atype%3d%22xsd%3along%22%3e16%3c%2ftransactionnum%3e%3coperations%20xsi%3atype%3d%22xsd%3alist%22%3e%3celem%20xsi%3atype%3d%22xsd%3aobject%22%3e%3cappid%3emyapplication%3c%2fappid%3e%3cclassname%3emyservlet%3c%2fclassname%3e%3cmethodname%3emymethodname%3c%2fmethodname%3e%3carguments%20xsi%3atype%3d%22xsd%3alist%22%3e%3celem%3emydatasource%3c%2felem%3e%3celem%3ebirttemplate.rptdesign%3c%2felem%3e%3celem%3ereporttype%3c%2felem%3e%3celem%3ereporttype%20data%20set%3c%2felem%3e%3celem%3e1%3c%2felem%3e%3celem%3e%3c%2felem%3e%3celem%3eghettosamson%20curl%20report%3c%2felem%3e%3celem%3estart_timestamp%20is%20not%20null%20and%20\(start_timestamp%20%26lt%3b%3d%20to_date\(%26apos%3b2013-01-04%2023%3a59%3a00%26apos%3b%2c%26apos%3byyyy-mm-dd%20hh24%3ami%3ass%26apos%3b\)%20or%20start_timestamp%20is%20null\)\)\)\)%0a%20%20%20%20%09%09%3c%2felem%3e%3celem%3e%3c%2felem%3e%3c%2farguments%3e%3cis_isc_rpc_dmi%20xsi%3atype%3d%22xsd%3aboolean%22%3etrue%3c%2fis_isc_rpc_dmi%3e%3c%2felem%3e%3c%2foperations%3e%3c%2ftransaction%3e --data isc_tnum=10 --data protocolversion=1.0 myipaddress:myport/myapplication/myapplication/sc/idacall?isc_rpc=1&isc_v=v8.2p_2012-08-28&isc_xhr=1 &`

the response received written file , follows:

`<html> <body onload='var results = document.formresults.results.value;null'><br> <br><br><br> <br><br><br><br><br><br><br><br><br><br><br><br><br><br> <form name='formresults'><textarea readonly name='results'> //isc_rpcresponsestart-->[{status:0,data:"http://myipaddress:myport/myapplication /reports/ghettosamson-curl1.pdf"}]//isc_rpcresponseend</textarea></form> </body></html>`

i trying duplicate request java , have run testng test. have tried several different examples here , nil works. here code examples try:

`@autowired @qualifier("contentheader") private header thecontentheader; @autowired @qualifier("userheader") private header theuserheader; @autowired @qualifier("transactionpair") private namevaluepair thetransactionpair; @autowired @qualifier("iscpair") private namevaluepair theiscpair; @autowired @qualifier("protocolpair") private namevaluepair theprotocolpair; @test public void testconcurrentcreateandopenreportrequests() throws clientprotocolexception, ioexception { assert.notnull(thetestpost); assert.notnull(thecontentheader); assert.notnull(theuserheader); assert.notnull(thetransactionpair); httpclient httpclient = new defaulthttpclient(); thetestpost.addheader(thecontentheader); thetestpost.addheader(theuserheader); arraylist<basicnamevaluepair> parameters = new arraylist<basicnamevaluepair>(); parameters.add(thetransactionpair); parameters.add(new basicnamevaluepair("isc_tnum", "10")); parameters.add(new basicnamevaluepair("protocolversion", "1.0")); thetestpost.setentity(new urlencodedformentity(parameters, "utf-8")); httpresponse response = httpclient.execute(thetestpost); httpentity entity = response.getentity(); assert.notnull(entity); inputstream instream = entity.getcontent(); system.out.println(instream.tostring()); instream.close(); } @test public void testsimplepost() throws httpexception, ioexception { httpclient client = new httpclient(); postmethod method = new postmethod("http://myipaddress:myport/ myapplication/myapplication/sc/idacall?isc_rpc=1&isc_v=v8.2p_2012-08-28& isc_xhr=1"); method.addrequestheader(thecontentheader); method.addrequestheader(theuserheader); method.addparameter(thetransactionpair); method.addparameter("isc_tnum", "10"); method.addparameter("protocolversion", "1.0"); int statuscode = client.executemethod(method); if (statuscode != -1) { inputstream instream = method.getresponsebodyasstream(); system.out.println(instream); } } @test public void testanomalypost() throws ioexception { string urlparameters = string.format("%s=%s&%s=%s&%s=%s", thetransactionpair.getname(), thetransactionpair.getvalue(), theiscpair.getname(), theiscpair.getvalue(), theprotocolpair.getname(), theprotocolpair.getvalue()); string request = "http://myipaddress:myport/myapplication/ myapplication/sc/idacall?isc_rpc=1&isc_v=v8.2p_2012-08-28&isc_xhr=1"; url url = new url(request); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setdooutput(true); connection.setdoinput(true); connection.setinstancefollowredirects(false); connection.setrequestmethod("post"); connection.setrequestproperty("content-type", "application/x-www-form-urlencoded; charset=utf-8"); connection.setrequestproperty("custom-userid", "ghettosamson-testng"); connection.setusecaches (false); dataoutputstream wr = new dataoutputstream(connection.getoutputstream ()); wr.writebytes(urlparameters); wr.flush(); wr.close(); connection.disconnect(); }`

java servlets testng smartgwt smartclient

No comments:

Post a Comment