我现在尝试从服务中下载xml文件以查找事件列表,并使用以下代码:
public static void main(String[] args) {
try {
String webPage = "https:/ServicnowURL//incident_list.do?XML&sysparm_query=u_im_record_type...";
String name = "uname";
String password = "Pwd";
String authString = name + ":" + password;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes("UTF-8"));
String authStringEnc = new String(authEncBytes);
System.out.println("Base64 encoded auth string: " + authStringEnc);
URL url = new URL(webPage);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic %s" + authStringEnc);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty( "Content-type", "text/xml");
urlConnection.setRequestProperty( "Accept", "*/*" );
urlConnection.setRequestProperty( "Accept-Encoding", "gzip" );
System.out.println(urlConnection.getResponseCode());
InputStream is;
if(urlConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
is = urlConnection.getInputStream();
System.out.println(is);
}
else{
is=urlConnection.getErrorStream();
System.out.println(is);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
上述代码获得401未经授权的错误
如果我通过删除网址中的“XML”将网址设为“https:/ServicnowURL//incident_list.do?sysparm_query = u_im_record_type ...”,那么我的回复就会成功。