Java本机浏览器登录提示

时间:2013-03-23 23:18:47

标签: java android https

我正在尝试使用Java向使用本机登录提示的浏览器进行HTTPS调用。

http://blog.stevensanderson.com/2008/08/25/using-the-browsers-native-login-prompt/

目前我正在使用下面的HTTP,它适用于其他网站,因为我知道要放入的参数...但是对于上述类型的登录失败(我不知道如何捕获参数。 ..这是一个登录弹出......如果这是正确的方法)....任何想法??谢谢

HttpUtility.sendPostRequest(requestURL, params);
String[] response = HttpUtility.readMultipleLinesRespone();

3 个答案:

答案 0 :(得分:0)

服务器应使用WWW-Authenticate标头和状态401响应您的第一个请求。标头将包含其预期的身份验证类型的详细信息。

然后,您可以在请求中以正确的格式添加Authorization标题后再试一次。

答案 1 :(得分:0)

@alex:好的......我设法按照您的建议建立HTTPS连接:

URL url = new URL("https://www.example.com/Login");
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authString);
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);

//then I read the input stream..

但是当我尝试在另一个方法中使用此代码创建另一个连接(比如在登录后转到另一个页面)时...将URLConnection作为参数:

//Below is in a method called account(URLConnection urlConnection)

URL url = new URL("https://www.example.com/account.aspx");
urlConnection = url.openConnection();
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);

//again I read the input stream..

...它会在登录之前抛出以下异常...同样的异常。我可以纠正吗?

Server returned HTTP response code: 401 for URL: https://www.example.com/account.aspx

答案 2 :(得分:0)

您可能已经解决了这个问题,但我最近遇到的问题涉及实现类似于浏览器本机登录提示的功能。我已经解决了它并写了一篇关于它的帖子。史蒂文桑德森的帖子对我也有帮助,帮助我了解某些概念。

http://captaindanko.blogspot.com.au/2015/04/how-does-browsers-native-login-prompt.html