授权用户阅读 URL 正文内容时出错
Error In Authorizing a User for Reading a URL body Content
我不知道发生了什么事请指教:
为了点击 URL 并阅读它的内容 returns。我首先在一个单独的函数中授权用户 ID 和密码,然后我使用这个函数来访问我定义的 URL。我做了以下功能:
函数名称和特性
- AQMAuthorize() - 用于授权,成功登录后 returns 用户详细信息
- AQM_Search_Api() - 获取方法用于读取URL
给出的所有数据(Json)
- AQMApplyMetaData - POST 在点击时添加一些元数据的方法 URL
函数的工作
A. The authorize function is works perfect if run alone, or called in
AQMApplyMetaData().
B. If authorize function is called in AQM_Search_Api(), I get the
Authentication Error:java.io.IOException: Server returned HTTP
response code: 401 for URL .
Dont know why it appear?
C. The GoogleChrome Postman Echo works fine for all my URL and returns JSON as expected.
函数1和3的代码为:
public String AQMAuthorize() {
try {
ResourceBundle bundle = ResourceBundle.getBundle("com.EF.apps.trqueue.resources.config");
String Server_URL = bundle.getString("SERVER_ADDRESS");
String userName = bundle.getString("ADMIN_ID");
String userPassword = bundle.getString("ADMIN_PWD");
String url = "http://" + Server_URL + "/api/rest/authorize";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
String auth = userName + ":" + userPassword;
byte[] encoded = Base64.encodeBase64(auth.getBytes()); //encoding to base64
String encodedString = new String(encoded);
con.setDoInput(true);
con.setDoOutput(true);
//con.setRequestProperty("Authorization", "Basic " + encodedString);
con.setRequestMethod("POST");
JSONArray jsonParent = new JSONArray();
JSONObject jSonOBJ=new JSONObject();
jSonOBJ.put("id","recording");
jSonOBJ.put("userId", userName);
jSonOBJ.put("password", userPassword);
jsonParent.add(jSonOBJ);
OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream());
wr.write(jsonParent.toString());
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + jSonOBJ);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
return response.toString();
}
catch(Exception e){
e.printStackTrace();
return e.toString();
}
}
public String AQM_Search_Api(String sessionID, String agentID) {
try{
//Authorize Custom method
AQMAuthorize();
ResourceBundle bundle = ResourceBundle.getBundle("com.EF.apps.trqueue.resources.config");
String Server_URL = bundle.getString("SERVER_ADDRESS");
String userName = bundle.getString("ADMIN_ID");
String userPassword = bundle.getString("ADMIN_PWD");
URLConnection urlConn = null;
InputStream is;
String credentials = userName + ":" + userPassword;
byte[] authEncBytes = Base64.encodeBase64(credentials.getBytes());
String authStringEnc = new String(authEncBytes);
String url = "http://" + Server_URL + "/api/rest/recording/contact?beginTime=2009-01-01&metadata=SessionID~44Test_Session_ID44";
URL obj = new URL(url);
urlConn = obj.openConnection();
// ERROR IS THROWN ON ABOVE STATEMENT
return "dummyData";
}
catch (Exception ex){
return ex.toString();
}
}
我有一个类似的问题,我认为你 运行 和我有同样的问题 (注意:我使用的是 Postman,没有 Java 代码示例)。我实现的内容与您的代码示例中的内容存在三个差异。
第一个区别是您的 JSON 有效载荷应类似于以下内容:
[
{
"id":"recording",
"userId": "importUser",
"password": "PCS997#62",
"data": {
"qm.service":true
}
}
]
此处的区别在于您缺少 JSON object 的 data
和 qm.service
部分。由于使用 Active Directory,我们还有一个域。这解决了我的问题。
第二个区别是 base64 编码请求 header 对于 AQM 端点不是必需的。这些端点都使用授权消息中提供的 JSESSIONID
cookie。这适用于所有 API 调用。我过去使用的其他 Cisco 端点已完成 base64 编码请求 header,但这些端点似乎没有。
第三个区别是我在搜索 API 请求中不需要 metadata
查询参数。我只是确保我的 JSESSIONID
cookie 包含在请求中。
例子可以看Cisco提供的"Cisco Unified Workforce Optimization Quality Management API and Database Schema Reference Guide Version 11.5" (PDF)
我不知道发生了什么事请指教:
为了点击 URL 并阅读它的内容 returns。我首先在一个单独的函数中授权用户 ID 和密码,然后我使用这个函数来访问我定义的 URL。我做了以下功能:
函数名称和特性
- AQMAuthorize() - 用于授权,成功登录后 returns 用户详细信息
- AQM_Search_Api() - 获取方法用于读取URL 给出的所有数据(Json)
- AQMApplyMetaData - POST 在点击时添加一些元数据的方法 URL
函数的工作
A. The authorize function is works perfect if run alone, or called in AQMApplyMetaData().
B. If authorize function is called in AQM_Search_Api(), I get the Authentication Error:java.io.IOException: Server returned HTTP response code: 401 for URL . Dont know why it appear?
C. The GoogleChrome Postman Echo works fine for all my URL and returns JSON as expected.
函数1和3的代码为:
public String AQMAuthorize() {
try {
ResourceBundle bundle = ResourceBundle.getBundle("com.EF.apps.trqueue.resources.config");
String Server_URL = bundle.getString("SERVER_ADDRESS");
String userName = bundle.getString("ADMIN_ID");
String userPassword = bundle.getString("ADMIN_PWD");
String url = "http://" + Server_URL + "/api/rest/authorize";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
String auth = userName + ":" + userPassword;
byte[] encoded = Base64.encodeBase64(auth.getBytes()); //encoding to base64
String encodedString = new String(encoded);
con.setDoInput(true);
con.setDoOutput(true);
//con.setRequestProperty("Authorization", "Basic " + encodedString);
con.setRequestMethod("POST");
JSONArray jsonParent = new JSONArray();
JSONObject jSonOBJ=new JSONObject();
jSonOBJ.put("id","recording");
jSonOBJ.put("userId", userName);
jSonOBJ.put("password", userPassword);
jsonParent.add(jSonOBJ);
OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream());
wr.write(jsonParent.toString());
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + jSonOBJ);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
return response.toString();
}
catch(Exception e){
e.printStackTrace();
return e.toString();
}
}
public String AQM_Search_Api(String sessionID, String agentID) {
try{
//Authorize Custom method
AQMAuthorize();
ResourceBundle bundle = ResourceBundle.getBundle("com.EF.apps.trqueue.resources.config");
String Server_URL = bundle.getString("SERVER_ADDRESS");
String userName = bundle.getString("ADMIN_ID");
String userPassword = bundle.getString("ADMIN_PWD");
URLConnection urlConn = null;
InputStream is;
String credentials = userName + ":" + userPassword;
byte[] authEncBytes = Base64.encodeBase64(credentials.getBytes());
String authStringEnc = new String(authEncBytes);
String url = "http://" + Server_URL + "/api/rest/recording/contact?beginTime=2009-01-01&metadata=SessionID~44Test_Session_ID44";
URL obj = new URL(url);
urlConn = obj.openConnection();
// ERROR IS THROWN ON ABOVE STATEMENT
return "dummyData";
}
catch (Exception ex){
return ex.toString();
}
}
我有一个类似的问题,我认为你 运行 和我有同样的问题 (注意:我使用的是 Postman,没有 Java 代码示例)。我实现的内容与您的代码示例中的内容存在三个差异。
第一个区别是您的 JSON 有效载荷应类似于以下内容:
[
{
"id":"recording",
"userId": "importUser",
"password": "PCS997#62",
"data": {
"qm.service":true
}
}
]
此处的区别在于您缺少 JSON object 的 data
和 qm.service
部分。由于使用 Active Directory,我们还有一个域。这解决了我的问题。
第二个区别是 base64 编码请求 header 对于 AQM 端点不是必需的。这些端点都使用授权消息中提供的 JSESSIONID
cookie。这适用于所有 API 调用。我过去使用的其他 Cisco 端点已完成 base64 编码请求 header,但这些端点似乎没有。
第三个区别是我在搜索 API 请求中不需要 metadata
查询参数。我只是确保我的 JSESSIONID
cookie 包含在请求中。
例子可以看Cisco提供的"Cisco Unified Workforce Optimization Quality Management API and Database Schema Reference Guide Version 11.5" (PDF)