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();
}
}
}
输出:
<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..
任何人都可以帮我解决这个问题。如果在REST客户端广告中使用此链接正常工作但是使用此Java程序会导致302错误。
答案 0 :(得分:0)
我尝试类似的东西时遇到了你的线程。对我来说,问题是通过连接&#34; Basic&#34;使用encodedCredentials
变量,使其成为&#34; Basic EncodedValue&#34;。