开发者

HTTP Requests on aspx page - Problem with VIEWSTATE

My problem is that i want to create a simple application for Android. I want to go this link -> http://publico.agcp.ipleiria.pt/Paginas/AvaliacoesPublico.aspx (which is from my university where i can see the tests dates), and automatically get the html code with my tests and then parse it to a list in my android application. Parsing and put it on a list is easy. The problem is out to post the correct information on the form. I checked with fiddler and firebug the post and responses to know what i have to send. So i came with this code:

hm.put("MSO_PageHashCode", "941-263790804");
        hm.put("__SPSCEditMenu", "true");
        hm.put("MSOWebPartPage_PostbackSource", "");
        hm.put("MSOTlPn_SelectedWpId", "");
        hm.put("MSOTlPn_View", "0");
        hm.put("MSOTlPn_ShowSettings", "False");
        hm.put("MSOGallery_SelectedLibrary", "");
        hm.put("MSOGallery_FilterString", "");
        hm.put("MSOTlPn_Button", "none");
        hm.put("MSOAuthoringConsole_FormContext", "");
        hm.put("MSOAC_EditDuringWorkflow", "");
        hm.put("MSOSPWebPartManager_DisplayModeName", "Browse");
        hm.put("__EVENTTARGET", "");
        hm.put("__EVENTARGUMENT", "");
        hm.put("__SCROLLPOSITIONX", "0");
        hm.put("__SCROLLPOSITIONY", "0");
        hm.put("MSOWebPartPage_Shared", "");
        hm.put("MSOLayout_LayoutChanges", "");
        hm.put("MSOLayout_InDesignMode", "");
        hm.put("MSOSPWebPartManager_OldDisplayModeName", "Browse");
        hm.put("MSOSPWebPartManager_StartWebPartEditingName", "false");
        hm.put("__LASTFOCUS", "");
        hm.put("__REQUESTDIGEST", "0x27AFB1B27830DB0B954C6852D518E1F7D3A125761B5C738CD230D1A51E007B8EAF3D77D049713E4E45460482B1E8AF3398225C845AEBB2F16279B3526DCC4A6F,04 
    Sep 2011 03:05:11 -0000");
        hm.put("__VIEWSTATE", this.getInitialViewstate());
        hm.put("__EVENTVALIDATION", "");
        hm.put("ctl00_ctl12$ctl00", "http://spserver:7250");
        hm.put("ctl00_PlaceHolderAGCPUO_ddlUO", "2");
        hm.put("ctl00_PlaceHolderAGCPUO_ddlAnosLectivos", "34");
        hm.put("ctl00_PlaceHolderAGCPUO_ddlSemestres", "S2");
        hm.put("ctl00_PlaceHolderMain_txtCodCurso", "");
        hm.put("ctl00_PlaceHolderMain_ddlCursos", "9099");
        hm.put("ctl00_PlaceHolderMain_ddlAnosCurr", "3");
        hm.put("ctl00_PlaceHolderMain_ddlPlanos", "1");
        hm.put("ctl00_PlaceHolderMain_ddlRamos", "0");
        hm.put("ctl00_PlaceHolderMain_ddlTurmas", "TESTEG1D");
        hm.put("ctl00_PlaceHolderMain_ddlDisciplina", "0");
        hm.put("__spDummyText1", "__spDummyText1");
        hm.put(开发者_C百科"__spDummyText2", "__spDummyText2");

    try {
                doSubmit(sUrl, hm);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            //function

        public void doSubmit(String url, HashMap<String, String> data) throws Exception {
        URL siteUrl = new URL("http://publico.agcp.ipleiria.pt/Paginas/AvaliacoesPublico.aspx");
        HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        DataOutputStream out = new DataOutputStream(conn.getOutputStream());

        Set keys = data.keySet();
        Iterator keyIter = keys.iterator();
        String content = "";
        for(int i=0; keyIter.hasNext(); i++) {
            Object key = keyIter.next();
            if(i!=0) {
                content += "&";
            }
            content += key + "=" + URLEncoder.encode(data.get(key), "UTF-8");
        }
        System.out.println(content);
        out.writeBytes(content);
        out.flush();
        out.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = "";
        while((line=in.readLine())!=null) {
            System.out.println(line);
        }
        in.close();
    }

So far so good. The problem is the VIEWSTATE. Which ever value i use on the other parameters of the form i cannot get what i want. But if i put the VIEWSTATE value obtained with fidler when passing all those comboboxes to get to my course, i get the html that i want.

How can i solve this? I cannot hardcode the viewstate value, cause i want to get dynamically the course test dates. Sorry for my english.


The easiest way to solve this is to issue a GET request prior to the post. You can then extract the VIEWSTATE from the response and use that value in your POST request.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜