繁体   English   中英

如何在使用HttpURLConnection(android / java)发布之前获取VIEWSTATE?

[英]How to get VIEWSTATE before posting using HttpURLConnection (android/java)?

我正在编写一个Android应用程序,并且一直在寻找一种从要发布到的服务器上获取_VIEWSTATE的方法,以便可以将其放在Http发布内容中。 少数人推荐了正则表达式,但随后其他一些专业人士强烈反对使用正则表达式解析HTML。 那么,如何解析_VIEWSTATE? 我在AsyncTask中使用HttpURLConnection / HttpsURLConnection。 另外,我不需要先放置InputStream阅读器,然后先获取_VIEWSTATE吗? 所有的android示例都将输入流放在输出流之后。 这是到目前为止我的代码的样子(发布到一个站点,该站点具有必须“单击”的三个页面):

在我的活动中,我这样调用Async任务:

//execute AsyncTask for all three reports
    submit_report.execute(report1, report2, report3); 

我的异步任务doInBackground方法:

class UploadReportTask extends AsyncTask<HashMap<String,String>, ProgressBar, Void> {

//this is called on task.execute
    protected Void doInBackground(HashMap<String,String>...maps) {
        System.out.println("Report is being uploaded"); 

        URL url = null;
        try {
            url = new URL(getString(R.string.url_dnc));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HttpURLConnection urlConnection = null;
        try {
            urlConnection = (HttpURLConnection) url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Accept-Charset", utf);
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + utf);
            urlConnection.setChunkedStreamingMode(0);


            //For each map in maps, encode the map,
            //get the headers, add the headers to the map, convert to bytes,
            //then post the bytes, 
            //get response.
            for (HashMap<String,String> map : maps){
                byte[] payload = makePayload(map);
                OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
                //urlConn.connect //I think this happens here
                out.write(payload);

                InputStream in = new BufferedInputStream(urlConnection.getInputStream());

                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                int length = in.read();
                String result, line = reader.readLine();
                result = line;
                while (length != -1){
                    result+=line;
                }
                System.out.println(result);
                out.flush();
                out.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            urlConnection.disconnect();
        }
        return null;    
    }
protected String parseViewstate(String response){
        int i = 0;
        String viewstate = "";
        while (true){
            int found = response.indexOf("\"__VIEWSTATE\"", i);
            if (found == -1) break;
            int start = found + 38; //check numbers from start of "__V"
            int end  = (response.indexOf("/>", start)) -2;
            viewstate = response.substring(start, end);
            i = end + 1;    
        }return viewstate;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM