开发者

VIEWSTATE value in the HttpPost

开发者 https://www.devze.com 2023-03-22 07:58 出处:网络
Here is the case, I want to post to a website, but before that I must retrieve the viewstate value and then make the post using this value, but the problem is that viewstate value is changing every ti

Here is the case, I want to post to a website, but before that I must retrieve the viewstate value and then make the post using this value, but the problem is that viewstate value is changing every time i make posts, so I am a little confused how can I use it's value in the second post if the value on the server will be already different. Is there any solution or am I doing everything wrong?

main with httppost

    try {
        HttpClient client = new DefaultHttpClient();

        HttpPost request = new HttpPost(
                "www.website.com/Login.aspx");

        String viewstate = getViewState(client, request,
                "www.website.com/Login.aspx");

        System.out.println(viewstate);

        request.getParams().setBooleanParameter(
                CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
        request.setHeader("Content-Type", "text/html; charset=utf-8");

        List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

        postParameters.add(new BasicNameValuePair("__VIEWSTATE",
                viewstate))
        postParameters.add(new BasicNameValuePair("__EVENTTARGET", ""));
        postParameters.add(new BasicNameValuePair("__EVENTARGUMENT", ""));
        postParameters.add(new BasicNameValuePair("ctl00$tbUsername",
                "name"));
        postParameters
                .add(new BasicNameValuePair("ctl00$tbPwd", "psw"));

        postParameters.add(new BasicNameValuePair("ctl00$chkRememberLogin",
                "0"));
     开发者_StackOverflow社区   postParameters
                .add(new BasicNameValuePair("ctl00$cmdLogin", "Login"));

        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
                postParameters);
        request.setEntity(formEntity);
        HttpResponse response = client.execute(request);

        String responseBody2 = EntityUtils.toString(response.getEntity());
        System.out.println(responseBody2);

        }
        // print page wap
        // System.out.println(responseBody2);
    }

and then send httpget

    String html = "";
    try {

        URL url1 = new URL("www.website.com/Login.aspx");
        URLConnection conn = url1.openConnection();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line+"\n");
        }
        rd.close();
        html = sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return findViewstate(html);

So what I was thinking, maybe I should reuse the same httpClient with the cookies or anything, so that the next request will be to the same page...


If I recall correctly ViewState values are encrypted by default and have information in them to prevent tampering, therefore, multiple requests WILL result in different values. But if you do a request, then make a post back to the page as the user would you should be ok, but you will need to make sure that all data goes back or you are going to hit issues with ASP.NET's event validation.

0

精彩评论

暂无评论...
验证码 换一张
取 消