URLConnection想实现不用登陆去访问URL

每一天爲明天 2016-05-16 06:24:18
我访问的这个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 这个错误,我这是哪里写错了呢
...全文
120 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
每一天爲明天 2016-05-16
  • 打赏
  • 举报
回复
422 - Unprocessable Entity 请求格式正确,但是由于含有语义错误,无法响应.这个错误怎么解决呢

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧