gpt4 book ai didi

java - 使用 ApacheHttpClient 处理身份验证相关的中间重定向

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:01:12 25 4
gpt4 key购买 nike

我正在尝试打开这个 uri

https://some-host/a/meta?  (this url is passed to proxi.jsp page)

.. 但它会重定向到身份验证服务(在 https 上),它会弹出一个用户名和密码框......如果在浏览器上......它需要你输入凭据......然后它会返回到我试图打开并显示内容的第一个链接......所以我想知道当这个中间重定向到身份验证服务发生时......我如何通过代码将我的用户名和密码输入到该弹出窗口......我正在尝试使用 apache HttpClient 来执行此操作...

这是我发出请求的 proxi.jsp 代码..

<%@ page language="java" import="
java.util.Collection,
org.apache.commons.httpclient.HttpClient,
org.apache.commons.httpclient.UsernamePasswordCredentials,
org.apache.commons.httpclient.auth.AuthScope,
org.apache.commons.httpclient.methods.GetMethod"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%
try {
String a_Url = request.getParameter( "url" ) ;

HttpClient client = new HttpClient();
client.getState().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm"),
new UsernamePasswordCredentials("test", "pass")
);

GetMethod get = new GetMethod(a_Url);
get.setDoAuthentication( true );
try {
int status = client.executeMethod( get );
out.println(status + "\n" + get.getResponseBodyAsString());
} finally {
get.releaseConnection();
}
} catch (Throwable t) {
t.printStackTrace();
}

%>

这是我得到的错误:-

org.apache.commons.httpclient.InvalidRedirectLocationException: Invalid redirect
location: https://login.xyz.com/13/smt
d.scc?TYPE=16&REALM=-SM-Documentum%20
uGVx6wOk1daI3&TARGET=-http%3A%2F%2Fsome-hid%3D10
at org.apache.commons.httpclient.HttpMethodDirector.processRedirectRespo
nse(HttpMethodDirector.java:619)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMe
thodDirector.java:179)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.jav
a:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.jav
a:323)
at org.apache.jsp.proxi_jsp._jspService(org.apache.jsp.proxi_jsp:64)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:389)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:4
86)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:380)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511
)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:3
90)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.jav
a:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:1
82)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:7
65)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)

at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:327)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(Normal
RewrittenUrl.java:213)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.j
ava:171)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:14
5)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewri
ter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewrit
eFilter.java:381)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(Servlet
Handler.java:1157)
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainI
nvocation.java:67)
at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilte
rPipeline.java:122)
at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:110)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(Servlet
Handler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:3
88)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.jav
a:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:1
82)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:7
65)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)

at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHand
lerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.
java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:1
52)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:53
6)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpCo
nnection.java:915)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:539)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:405)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.ja
va:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.j
ava:582)
Caused by: org.apache.commons.httpclient.URIException: Invalid query
at org.apache.commons.httpclient.URI.parseUriReference(URI.java:2049)
at org.apache.commons.httpclient.URI.<init>(URI.java:147)
at org.apache.commons.httpclient.HttpMethodDirector.processRedirectRespo
nse(HttpMethodDirector.java:601)
... 44 more

如果我尝试粘贴此 url

https://login.xyz.com/9]&AUTHREASON=0&METHOD=GET@Name

回到浏览器,然后我会看到一个用于身份验证的弹出窗口,如果我在该窗口中输入我的用户名和密码,我就会得到我正在寻找的实际内容。我必须在 proxi.jsp 页面中传递任何 header 吗?因为在页面中传递用户名和密码不是最好的方式......或任何其他方式。任何建议将不胜感激..

最佳答案

不确定站点是否使用基本身份验证——您可以尝试以下操作,其中 test 是用户名,pass 是您输入的站点的密码。

https://test:pass@some-host/a/getmeta?id=10

关于java - 使用 ApacheHttpClient 处理身份验证相关的中间重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6539905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com