Java:如何进行HTTP浏览会话 [英] Java: How to make a HTTP browsing session

查看:169
本文介绍了Java:如何进行HTTP浏览会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个向服务器发送一些POST请求的Java应用程序。第一个请求是具有身份验证信息的请求。然后当我发送下一个请求时,我得到了我的会话过期的答案。但是我在同一秒内发送下一个请求,所以,它不能超时。

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.

所以我猜Java中的HTTP会话就像,我需要用它来发送我的请求,服务器知道它跟随上一个请求。

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.

我一直在搜索谷歌,但找不到任何东西。只有关于Servlets的东西。但我正在创建一个桌面应用程序。

I've been searching Google, but can't find anything. Only something about Servlets. But I'm creating a desktop application.

PS:我是新发送HTTP请求和此类事情。

PS: I'm new to sending HTTP requests and this kind of things.

提前致谢,

Martijn

Thanks in advance,
Martijn

编辑:这是代码我目前使用:

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.


推荐答案

这些将对您有所帮助:

  • Apache HttpComponents
  • Apache HttpClient

这篇关于Java:如何进行HTTP浏览会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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