【问题标题】:How to make multiple request in one HTTPconnection?如何在一个 HTTP 连接中发出多个请求?
【发布时间】:2013-06-16 05:06:03
【问题描述】:

我有一个服务器,我必须先登录(第一个 URL),然后我会发送另一个 POST 请求(第二个 URL),但是这个 post 请求只有在我仍然登录的情况下才能完成。如何仍然记录使用 URL 2 的 POST 请求?

代码如下:

  HttpURLConnection con=(HttpURLConnection) new URL("http://Login(URL1)").openConnection();
            String headerName=null; String cookie=null; 
            for (int i=1; (headerName = con.getHeaderFieldKey(i))!=null; i++) {
                if (headerName.equals("Set-Cookie"))               
                 cookie = con.getHeaderField(i);            
            }
            cookie = cookie.substring(0, cookie.indexOf(";"));
            String cookieName = cookie.substring(0, cookie.indexOf("="));
            String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
            System.out.println(cookieName);
            System.out.println(cookieValue);
            con=(HttpURLConnection) new URL("http://Login(URL1)").openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("Cookie", "session_id=" + cookieValue);
            con.setDoInput(true);
            con.setDoOutput(true);
            PrintWriter writer = new PrintWriter(con.getOutputStream());
            String account = "sdfsdf";
            String password = "sddfdsf";
            writer.println("&username=" + account + "&password=" + password + "&action=do_login&http=1");
            writer.close();

            System.err.println(con.getResponseCode());
            BufferedReader reader=new BufferedReader(new InputStreamReader(con.getInputStream()));
            String response;
            while ((response=reader.readLine())!=null) {
                System.out.println(response);
            }
            reader.close();


            con=(HttpURLConnection) new URL("http://POST Request URL").openConnection();
            con.setRequestMethod("POST")

【问题讨论】:

  • 没有HTTPConnection 这样的东西。你的意思是HttpURLConnection

标签: java httpconnection


【解决方案1】:

你不需要做任何事情。

  1. HttpURLConnection 在后台进行 TCP 连接池。
  2. HTTP 会话通过 cookie 保留,即使跨多个连接也是如此。

【讨论】:

    【解决方案2】:

    您是否尝试将凭据添加到对 URL 2 的请求?

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
                username, password);
    request.addHeader(new BasicScheme().authenticate(creds, request));
    HttpResponse response = httpClient.execute(request);
    

    【讨论】:

    • 非常感谢您的回复,登录在 URL 1 中工作......即使我再次这样做,我也必须为 POST 请求建立不同的连接......我该怎么做确保我处于登录状态的 POST?顺便说一句,我使用的是 HttpURLConnection 而不是 HttpClient。
    • 所以第一个 URL 是服务器上的一些手动登录页面?当您登录网站或服务器时,您的凭据将被存储,以供您导航后发送的后续 HTTP 请求使用。因此,当您通过 java 或任何其他语言发送请求时,您需要将凭据附加到每个请求。
    • 是的,第一个 URL 只是一个 UN 和 PW 页面......登录后出现服务页面......那是我无法获得服务的地方。如何在不执行 POST 请求的情况下发送凭据。
    • 凭据是标题字段。您可以将它们附加到任何请求,无论是 POST 还是 GET,就像 PandaBearSoup 描述的那样。
    • 谢谢你们的帮助。但我有一个问题。我在第二个请求中发送的 cookie 是否足以让我的请求被识别为已登录?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多