如何捕获从URL重定向器生成的会话密钥

时间:2013-10-08 15:43:20

标签: java android html web-services web-scraping

http://testcloud.edu/apps/users/redirector.cfm?ID=8b5619c4-f137-45f1-8274-3fbb39b7225d&page=1&&lname=&fname=

http://testcloud.edu/apps/users/details.cfm?id=8b5619c4-f137-45f1-8274-3fbb39b7225d&key=4E2268588778333DC7BBA3C8B219E628&page=1&lname=&fname=

所以我有一个Web服务,当查询发送到URL时会生成密钥。如何发送查询 - 然后在Android中捕获返回的密钥?

有关如何在Android中完成此任务的任何工具,教程或平坦的建议表示赞赏。

来源:

io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView tv;
    String url = "http://testcloud.edu/apps/users/results.cfm?lname=FOO&fname=BAR";
    String tr;
    Document doc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.TextView01);
        new MyTask().execute(url);
    }

    private class MyTask extends AsyncTask<String, Void, String> {

        ProgressDialog prog;

        String title = "";

        @Override
        protected void onPreExecute() {
            prog = new ProgressDialog(MainActivity.this);
            prog.setMessage("Loading....");
            prog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                doc = Jsoup.connect(params[0]).get();
                Element tableElement = doc.select(".datagrid").first();

                Elements tableRows = tableElement.select("tr");
                for (Element row : tableRows) {
                    Elements cells = row.select("td");
                    if (cells.size() > 0) {
                        title = cells.get(0).child(0).attr("href") + " ; "
                                +cells.get(0).text() + "; "
                                + cells.get(1).text() + "; "
                                + cells.get(2).text() + "; "
                                + cells.get(3).text();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return title;
        }

        @Override
        protected void onPostExecute(String title) {
            super.onPostExecute(title);
            prog.dismiss();
            tv.setText(title);
        }
    }
}
编辑(第一次回答后):

package com.example.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView tv;
        String url = "http://testcloud.edu/apps/users/results.cfm?lname=FOO&fname=BAR";
    String tr;
    Document doc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.TextView01);
        new MyTask().execute(url);
    }

    private class MyTask extends AsyncTask<String, Void, String> {

        ProgressDialog prog;

        String title = "";

        @Override
        protected void onPreExecute() {
            prog = new ProgressDialog(MainActivity.this);
            prog.setMessage("Loading....");
            prog.show();

        }

        @Override
        protected String doInBackground(String... params) {

            ImageView img = (ImageView) findViewById(R.id.imageView1);
            {
                try {


                    manualRedirectHandler(url);

                    Document manualRedirectHandler(String url) throws IOException{
                        Response response = Jsoup.connect(url.replaceAll(" ", "%20")).followRedirects(false).execute();
                        int status = response.statusCode();

                    if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)
                    {
                        String redirectUrl = response.header("location");
                        System.out.println("Redirect to: " + redirectUrl);//key will be here
                        return manualRedirectHandler(redirectUrl);
                    }

                    return Jsoup.parse(response.body());
                    }

                    String imageURL = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg";
                    HttpGet httpRequest = null;

                    httpRequest = new HttpGet(URI.create(imageURL));

                    HttpClient httpclient = new DefaultHttpClient();
                    HttpResponse response = (HttpResponse) httpclient
                            .execute(httpRequest);

                    HttpEntity entity = response.getEntity();
                    BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
                    InputStream input = b_entity.getContent();

                    Bitmap bitmap = BitmapFactory.decodeStream(input);

                    img.setImageBitmap(bitmap);

                    doc = Jsoup.connect(params[0]).get();
                    Element tableElement = doc.select(".datagrid").first();

                    Elements tableRows = tableElement.select("tr");
                    for (Element row : tableRows) {
                        Elements cells = row.select("td");
                        if (cells.size() > 0) {
                            title = cells.get(0).child(0).attr("href") + " ; "
                                    + cells.get(0).text() + "; "
                                    + cells.get(1).text() + "; "
                                    + cells.get(2).text() + "; "
                                    + cells.get(3).text();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return title;
            }
        }

        @Override
        protected void onPostExecute(String title) {
            super.onPostExecute(title);
            prog.dismiss();
            tv.setText(title);

        }

    }
}

1 个答案:

答案 0 :(得分:0)

您可以在重定向中找到密钥....

manualRedirectHandler(url);

private static Document manualRedirectHandler(String url) throws IOException
{
    Response response = Jsoup.connect(url.replaceAll(" ", "%20")).followRedirects(false).execute();
    int status = response.statusCode();

if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)
{
    String redirectUrl = response.header("location");
    System.out.println("Redirect to: " + redirectUrl);//key will be here
    return manualRedirectHandler(redirectUrl);
}

return Jsoup.parse(response.body());
}

找到重定向代码on this post