aspx页面上的HTTP请求 - VIEWSTATE问题

时间:2011-09-04 13:58:15

标签: android asp.net html

我的问题是我想为Android创建一个简单的应用程序。我想去这个链接 - > http://publico.agcp.ipleiria.pt/Paginas/AvaliacoesPublico.aspx(来自我的大学,我可以看到测试日期),并自动获取我的测试的HTML代码,然后将其解析为我的Android应用程序中的列表。 解析并将其列入清单很容易。问题是在表单上发布正确的信息。我用小提琴手和萤火虫检查了帖子并回复了解我要发送的内容。 所以我带来了这段代码:

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("__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();
    }

到目前为止一切顺利。问题是VIEWSTATE。我在表单的其他参数上使用哪个值,我无法得到我想要的。但是如果我把fifler获得的VIEWSTATE值传递给所有那些组合框来到我的课程时,我会得到我想要的html。

我该如何解决这个问题?我无法对viewstate值进行硬编码,因为我想动态获取课程测试日期。 抱歉我的英文。

1 个答案:

答案 0 :(得分:1)

解决此问题的最简单方法是在发布之前发出GET请求。然后,您可以从响应中提取VIEWSTATE,并在POST请求中使用该值。