每一天爲明天 2016-05-17 01:10 采纳率: 14.3%
浏览 1143

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 这个错误,我这是哪里写错了呢
422 - Unprocessable Entity 请求格式正确,但是由于含有语义错误,无法响应.这个错误怎么解决呢 ?请大神们指点!

  • 写回答

2条回答 默认 最新

  • Royal_lr 2016-05-17 01:16
    关注

    这个xml文件能访问的到吗,,,

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么eprime输出的数据会有缺失?
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题