URLConnection想实现不用登陆去访问URL
我访问的这个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 这个错误,我这是哪里写错了呢