简体   繁体   English

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

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

I am writing an Android application, and have been looking for a way to get the _VIEWSTATE from the server I want to post to so I can put it in my Http post content. 我正在编写一个Android应用程序,并且一直在寻找一种从要发布到的服务器上获取_VIEWSTATE的方法,以便可以将其放在Http发布内容中。 A few people recommended regex, but then some other pros were strongly opposed to parsing HTML with regex. 少数人推荐了正则表达式,但随后其他一些专业人士强烈反对使用正则表达式解析HTML。 So, how to parse the _VIEWSTATE ? 那么,如何解析_VIEWSTATE? I am using HttpURLConnection/HttpsURLConnection in an AsyncTask. 我在AsyncTask中使用HttpURLConnection / HttpsURLConnection。 Also, don't I need to put the InputStream reader first, to get the _VIEWSTATE first? 另外,我不需要先放置InputStream阅读器,然后先获取_VIEWSTATE吗? All the android examples put the input stream after the output stream. 所有的android示例都将输入流放在输出流之后。 Here is what my code looks like so far (posting to one site that has three pages that have to be "clicked through"): 这是到目前为止我的代码的样子(发布到一个站点,该站点具有必须“单击”的三个页面):

In my Activity, I call the Async task like this: 在我的活动中,我这样调用Async任务:

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

My Async task doInBackground method: 我的异步任务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