Java:如何进行 HTTP 浏览会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6306225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: How to make a HTTP browsing session
提问by Martijn Courteaux
I'm trying to make a Java application that sends some POST requests to a server. The first request is the one with authentication information. Then when I send the next request, I'm getting the answer that my session is expired. But I'm sending the next request within the same second, so, it can't be timed out.
我正在尝试制作一个向服务器发送一些 POST 请求的 Java 应用程序。第一个请求是带有身份验证信息的请求。然后当我发送下一个请求时,我得到了我的会话已过期的答案。但是我在同一秒内发送下一个请求,所以,它不能超时。
So I guess there is something like a HTTP Session in Java, which I need to use for sending my request that way the server knows its following the previous request.
所以我想在 Java 中有一个类似于 HTTP 会话的东西,我需要用它来发送我的请求,这样服务器就知道它跟随上一个请求。
I've been searching Google, but can't find anything. Only something about Servlets. But I'm creating a desktop application.
我一直在谷歌搜索,但找不到任何东西。只有关于 Servlet 的一些东西。但我正在创建一个桌面应用程序。
PS: I'm new to sending HTTP requests and this kind of things.
PS:我是发送 HTTP 请求和此类事情的新手。
Thanks in advance,
Martijn
提前致谢,
Martijn
Edit: This is the code I use currently:
编辑:这是我目前使用的代码:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author martijn
*/
public class PostRequest {
private String _url;
private List<PostParameter> params;
public PostRequest(String url) {
this._url = url;
this.params = new ArrayList<PostParameter>();
}
public void addParam(String key, String value)
{
params.add(new PostParameter(key, value));
}
public String getURL()
{
return _url;
}
public InputStream request() throws IOException {
URL url = new URL(this._url);
URLConnection conn = url.openConnection();
if (params.size() > 0) {
conn.setDoOutput(true);
StringBuilder data = new StringBuilder();
for (int i = 0; i < params.size(); i++) {
if (i > 0) {
data.append("&");
}
String key = params.get(i).key;
String value = params.get(i).value;
data.append(URLEncoder.encode(key, "UTF-8"));
data.append("=");
data.append(URLEncoder.encode(value, "UTF-8"));
}
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data.toString());
wr.flush();
}
return conn.getInputStream();
}
public class PostParameter {
public String key;
public String value;
public PostParameter(String key, String value) {
this.key = key;
this.value = value;
}
public PostParameter() {
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
public void setKey(String key) {
this.key = key;
}
public void setValue(String value) {
this.value = value;
}
}
}
And then send two request after each other:
然后依次发送两个请求:
PostRequest postAuthentication = new PostRequest("http://blahblah.com");
postAuthentication.addParam("user", user);
postAuthentication.addParam("password", pass);
Utils.dumpReader(postAuthentication.request());
PostRequest postDoSomething = new PostRequest("http://blahblah.com/function.php");
postDoSomething.addParam("func", "blah");
postDoSomething.addParam("value", "14");
Utils.dumpReader(postDoSomething.request());
// Here I'm getting the session is expired.
回答by PeterMmm
These will help you:
这些将帮助您:
回答by Michael Spector
If the session ID is stored in cookies, you have to provide a way of storing cookies and passing them to the second request. Refer to this examplefor cookie handling in Apache's HTTP client.
如果会话 ID 存储在 cookie 中,您必须提供一种存储 cookie 并将它们传递给第二个请求的方法。请参阅此示例以了解Apache 的 HTTP 客户端中的cookie 处理。
回答by Vineet Reynolds
The server is probably relying on HTTP session Cookies (only) to track requests. You'll need to analyze the HTTP traffic to the server (when using a browser) to see if this is the case. The server provides cookies to the client, via the Set-Cookie
response header.
服务器可能依赖 HTTP 会话 Cookie(仅)来跟踪请求。您需要分析到服务器的 HTTP 流量(使用浏览器时)以查看是否是这种情况。服务器通过Set-Cookie
响应头向客户端提供 cookie 。
If the assumption is true, that cookies are required, then the cookie must be supplied on every request by the client to the server. This is done by the client setting a Cookie
request header.
如果假设为真,即需要 cookie,则必须在客户端向服务器的每次请求时提供 cookie。这是通过客户端设置Cookie
请求标头来完成的。
I assume that you are using the HttpUrlConnection class provided by the Java API. A very comprehensive answer by Balusexists on this topic at StackOverflow. Refer to the section on "Maintaining the session".
我假设您使用的是 Java API 提供的 HttpUrlConnection 类。在 StackOverflow 上,Balus对这个主题有一个非常全面的回答。请参阅“维护会话”部分。
回答by gabba
When you receive answer to authentication request you need to store information about session When you send next request you need to add session information to request.
当您收到身份验证请求的答复时,您需要存储有关会话的信息 当您发送下一个请求时,您需要将会话信息添加到请求中。