如果我理解正确(如果我没有理解,请告诉我):
如果它受用户/密码保护,您是否应该发出 Http Post(例如,您从浏览器中执行的操作)并从该帖子中获取响应?像这样的东西:
http://www.informit.com/guides/content.aspx?g=java&seqNum=44
编辑:这是一个示例
我有一个看起来像这样的页面(它过于简单,但还是这样):
<form action="../../j_spring_security_check" method="post" >
<input id="j_username" name="j_username" type="text" />
<input id="j_password" name="j_password" type="password"/>
<input type="image" class="submit" id="login" name="login" />
</form>
如果它是网页,则必须提供用户名/密码才能在此登录页面“之后”获取实际内容。你真正的问题是这里的 HTTP POST(我敢打赌你的情况是一样的)。
现在以编程方式获得相同的功能...
您将需要 apache http 客户端库(您可能不需要它,但这是最简单的方法)。这是它的 Maven 依赖项。你要为Android做这个,对吧?从我读过的内容来看,apache http 客户端是 Android 中的默认客户端。
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class HttpPost {
public static void main(String[] args) {
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod("http://localhost:20000/moika/moika/j_spring_security_check");
postMethod.addParameter("j_username", "ACTUAL_USER");
postMethod.addParameter("j_password", "ACTUAL_PASSWORD");
try {
int status = httpClient.executeMethod(postMethod);
System.out.println("STATUS-->" + status);
if(status == 302){
Header header = postMethod.getResponseHeader("location");
String location = header.getValue();
System.out.println("HEADER_VALUE-->" + location);
GetMethod getMethod = new GetMethod(location);
httpClient.executeMethod(getMethod);
String content = getMethod.getResponseBodyAsString();
System.out.println("CONTENT-->" + content);
}
String contentInCaseOfNoRedirect = postMethod.getResponseBodyAsString();
} catch (Exception exception){
exception.printStackTrace();
}
}
}
这可能看起来有点奇怪,但我执行了重定向 (302),在 RCF 中似乎存在问题,因此这是一个小的解决方法。
如果您不在服务器端执行任何重定向,那么您可以忽略我检查 302 的部分。
看看什么对你有用。
干杯,
尤金。