Java Http POST具有基本授权和重定向功能 [英] Java Http POST with basic authorization and redirect

查看:399
本文介绍了Java Http POST具有基本授权和重定向功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序执行带有基本授权的http帖子很好但是当帖子完成后,页面会被重定向到成功页面。由401授权导致的重定向失败失败。

The program does a http post with basic authorization just fine but when the post is complete the page is redirected to a success page. The redirect failes due to 401 authorization failed.

        final URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestProperty("Authorization", "basic " +base64);
        wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
        rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

该行

rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

因401授权失败而失败...

fails due to 401 authorization failed...

我还尝试添加

conn.setRequestProperty("Authorization", "basic " +base64);

wr.flush();

我收到已经连接错误。显然,我设置的授权不会影响重定向。非常感谢任何解决这个问题的方法。

I get the error of "Already connected". Evidently the authorization that I set doesn't follow over to the redirect. Any solutions to this problem is greatly appreciated.

推荐答案

您可以尝试2个选项:


  1. 使用 setDefaultRequestProperty (参见 http://download.oracle.com/javase/1.5.0/docs/api/java /net/URLConnection.html#setDefaultRequestProperty%28java.lang.String,%20java.lang.String%29 )设置授权标头的方法。

  2. 禁用自动重定向: http:// download.oracle.com/javase/1.5.0/docs/api/java/net/HttpURLConnection.html#setInstanceFollowRedirects%28boolean%29 并手动完成。

  1. Use the setDefaultRequestProperty (see http://download.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html#setDefaultRequestProperty%28java.lang.String,%20java.lang.String%29) method to set the Authorization header.
  2. Disable automatic redirect following: http://download.oracle.com/javase/1.5.0/docs/api/java/net/HttpURLConnection.html#setInstanceFollowRedirects%28boolean%29 and do it manually.

这篇关于Java Http POST具有基本授权和重定向功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆