Java program to call REST service that needs authentication -
public class restclient { public static void main(string[] args) { try { jsonobject jsonobject = new jsonobject(); jsonobject.put("fromsku","none" ); jsonobject.put("tosku","asa5500-encr-k8" ); jsonobject.put("modelno", "asav10"); jsonobject.put("serialno", "asatstsn"); system.out.println(jsonobject); url url = new url("http://licruleswb- dev.cloudapps.cisco.com/licenserules/rest/invokeasarule"); urlconnection connection = url.openconnection(); connection.setdooutput(true); string encodedcredentials = new string( org.apache.commons.codec.binary.base64.encodebase64 (org.apache.commons.codec.binary.stringutils.getbytesutf8("username:password")) ); system.out.println(encodedcredentials); connection.setrequestproperty ("authorization", encodedcredentials); connection.setrequestproperty("content-type", "application/json"); connection.setconnecttimeout(5000); connection.setreadtimeout(5000); outputstreamwriter out = new outputstreamwriter(connection.getoutputstream()); out.write(jsonobject.tostring()); out.close(); bufferedreader in = new bufferedreader(new inputstreamreader( connection.getinputstream())); system.out.println(in.tostring()); while (in.readline() != null) { system.out.println(in.readline()); } system.out.println("\nrest service invoked successfully.."); in.close(); } catch (exception e) { system.out.println("\nerror while calling rest service"); system.out.println(e); } } catch(exception e) { e.printstacktrace(); } } }
output:
<html><head> </head><body> <p>the document has moved <a href="https://wwwin-sso-nprd.cisco.com/obrareq.cgi?wh%3dlicruleswbintrtpdev-licensingrules.cloudapps.cisco.com%20wu%3d%2flicenserules%2frest%2finvokeasarule%20wo%3d2%20rh%3dhttp%3a%2f%2flicruleswb-dev.cloudapps.cisco.com%20ru%3d%252flicenserules%252frest%252finvokeasarule">here</a>.</p> null rest service invoked successfully..
can me out resolve issue. link working fine if used in rest client ad on java program it's throwing 302 error.
i came across thread when trying similar. me problem solved concatenating "basic " encodedcredentials
variable, making "basic encodedvalue".
Comments
Post a Comment