我访问的这个URL、只能用管理员的权限登陆才能访问,如果用cookie或者session能实现吗?该怎么实现?
public BufferedReader send(String content) {
try {
URLConnection conn = new URL(url).openConnection();
conn.setRequestProperty("Content-Type", contentType);
BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = "admin" + ":" + "admin";
String encodedAuthorization = enc.encode( userpassword.getBytes() );
conn.setRequestProperty("Authorization", "Basic "+ encodedAuthorization);
if (content != null) {
conn.setRequestProperty("Content-Length", String.valueOf(content.getBytes(charset).length));
}
conn.setDoInput(true);
conn.setDoOutput(true);
if (conn instanceof HttpURLConnection) {
((HttpURLConnection) conn).setRequestMethod(requestMethod_POST);
}
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), Charset.forName(charset)));
try {
w.write(content);
} finally {
w.flush();
w.close();
}
return new BufferedReader(new InputStreamReader(conn.getInputStream(), Charset.forName(charset)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
这种方法会报Exception in thread "main" java.lang.RuntimeException: java.io.IOException: Server returned HTTP response code: 422 for URL: http://192.168.0.18/redmine/time_entries.xml 这个错误,我这是哪里写错了呢
422 - Unprocessable Entity 请求格式正确,但是由于含有语义错误,无法响应.这个错误怎么解决呢 ?请大神们指点!
这个xml文件能访问的到吗,,,
你这个要看服务器是如何验证管理员权限的,比如是cookie,那么你提交的HTTP请求的时候,就要在header中添加对应的cookie信息来进行。不然服务器就会不处理
或者你提交的管理员帐号和密码是否正确,总之需要保证请求的格式是否符合服务器的处理