Java 如何在一个会话中发送GET和POST请求?

Java 如何在一个会话中发送GET和POST请求?,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,我发送GET请求并解析响应以设置VIEWSTATE和EVENTVALIDATION。 然后我发送带有参数的POST请求,但变量无效! 那么,如何在一个会话中发送GET和POST请求呢?页面在这里 这是我的密码 HttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()) .build(); private String sendGe

我发送
GET
请求并解析响应以设置
VIEWSTATE
EVENTVALIDATION
。 然后我发送带有参数的
POST
请求,但变量无效! 那么,如何在一个会话中发送
GET
POST
请求呢?页面在这里

这是我的密码

HttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy())
                .build();
private String sendGet() throws IOException {
        HttpGet httpget = new HttpGet(url+"Login.aspx");
        httpget.addHeader("User-Agent",USER_AGENT);
        try (CloseableHttpResponse response = httpclient.execute(httpget)) {
            HttpEntity entity = response.getEntity();
            BufferedReader br=new BufferedReader(new InputStreamReader(entity.getContent()));
            StringBuilder result=new StringBuilder();
            String line;
            while ((line=br.readLine())!=null){
                result.append(line);
            }
            EntityUtils.consume(entity);
            updateViewState(result.toString());
            return result.toString();
        }
    }

    private String sendPost() throws IOException, URISyntaxException {
        HttpRequest login = RequestBuilder.post()
                .setUri(new URI(url+"Login.aspx"))
                .addParameter("DWC$DWMessages", "")
                .addParameter("__VIEWSTATE", viewState)
                .addParameter("__EVENTVALIDATION", eventValidation)
                .addParameter("DWC_NotificationToolTip_ClientState", "")
                .addParameter("LoginWebPart$LoginTypes", "Guest")
                .addParameter("LoginWebPart$TextBoxUserName", "")
                .addParameter("LoginWebPart$TextBoxPassword", "")
                .addParameter("LoginWebPart$ButtonLogin", "Login")
                .addParameter("LoginWebPart_LanguageContextMenu_ClientState", "")
                .addHeader("User-Agent",USER_AGENT)
                .build();
        try (CloseableHttpResponse response2 = httpclient.execute((HttpUriRequest)login)) {
            HttpEntity entity = response2.getEntity();
            BufferedReader br=new BufferedReader(new InputStreamReader(entity.getContent()));
            StringBuilder result=new StringBuilder();
            String line;
            while ((line=br.readLine())!=null){
                result.append(line);
            }
            EntityUtils.consume(entity);
            return result.toString();
        }
    }

在服务器上的一个HTTP会话中,您的意思是我认为?您在哪里解析响应?在哪里设置变量?你说的“但是变量无效”到底是什么意思?是的。服务器在连接后分配您的子会话。所以,我的代码连接了两次,子会话和变量是不同的。但是服务器是如何做到的呢?使用cookie?我在updateViewState()方法中设置变量,这很容易实现Jsoup.parse(result)。