如何使用带有Jsoup的HttpURLConnection获取重定向页面的HTMl页面

时间:2014-09-16 18:15:16

标签: android

我正在尝试使用HttpURLConnection获取重定向页面的html页面。初始页面是登录,我将从那里重定向到下一页。我也获得了新定向页面的URL,但是当我获得新定向页面的html内容时,它返回登录页面html视图。代码低于plz帮助。

 protected Object doInBackground(Object... params) {
        try {
 old URL--->URL url = new URL(UrlLink);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            List<NameValuePair> login = new ArrayList<NameValuePair>();
            login.add(new BasicNameValuePair("login", "xxxxx"));
            login.add(new BasicNameValuePair("password", "yyyyyy"));
            login.add(new BasicNameValuePair("Login", "Login"));
            login.clear();
            int responseCode = urlConnection.getResponseCode();
            System.out.println(responseCode);
            urlConnection.connect();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
New Url---> URL newURL = urlConnection.getURL();
            String urlNew=   newURL.toString();
Returns-->  doc = Jsoup.connect(urlNew).get();
Login page  Elements docEle = doc.select("#header a");
 HTML       Log.v("Document", docEle.toString() );
        }

        catch (Exception e) {
            Log.v("Error", e.toString());
        }

1 个答案:

答案 0 :(得分:0)

要设置VM范围的cookie(在内存中),请使用以下代码:

CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);

然后在登录之后,您需要将cookie传递给Jsoup,类似于下面的示例(根据需要进行修改)

Jsoup.connect("https://need.authentication.com").cookie(urlNew, cookie).get()