java - Send PUT, DELETE HTTP request in HttpURLConnection -
i have created web service phone call using java below code. need create delete , set operations perform.
url url = new url("http://example.com/questions"); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setdooutput(true); conn.setrequestmethod( "post" ); conn.setrequestproperty("content-type", "application/json"); outputstream os = conn.getoutputstream(); os.write(jsonbody.getbytes()); os.flush();
when add together below code perform delete action gives errors saying:
java.net.protocolexception: http method delete doesn't back upwards output.
conn.setrequestmethod( "delete" );
so how perform delete , set requests?
put
illustration using httpurlconnection
:
url url = null; seek { url = new url("http://localhost:8080/putservice"); } grab (malformedurlexception exception) { exception.printstacktrace(); } httpurlconnection httpurlconnection = null; dataoutputstream dataoutputstream = null; seek { httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setrequestproperty("content-type", "application/x-www-form-urlencoded"); httpurlconnection.setrequestmethod("put"); httpurlconnection.setdoinput(true); httpurlconnection.setdooutput(true); dataoutputstream = new dataoutputstream(httpurlconnection.getoutputstream()); dataoutputstream.write("hello"); } grab (ioexception exception) { exception.printstacktrace(); } { if (dataoutputstream != null) { seek { dataoutputstream.flush(); dataoutputstream.close(); } grab (ioexception exception) { exception.printstacktrace(); } } if (httpsurlconnection != null) { httpsurlconnection.disconnect(); } }
delete
illustration using httpurlconnection
:
url url = null; seek { url = new url("http://localhost:8080/deleteservice"); } grab (malformedurlexception exception) { exception.printstacktrace(); } httpurlconnection httpurlconnection = null; seek { httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setrequestproperty("content-type", "application/x-www-form-urlencoded"); httpurlconnection.setrequestmethod("delete"); system.out.println(httpurlconnection.getresponsecode()); } grab (ioexception exception) { exception.printstacktrace(); } { if (httpurlconnection != null) { httpurlconnection.disconnect(); } }
java web-services http-post httpurlconnection http-delete
No comments:
Post a Comment