如何使用jsoup在android中发送POST?

时间:2014-04-30 21:38:32

标签: java android jsoup

我有以下代码,它使用jsoup来解析html。

package com.example.descov2;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

    private List<String> cookies;
    private HttpsURLConnection conn;

    private final String USER_AGENT = "Mozilla/5.0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            new DownloadFilesTask().execute();
        } catch (Exception e) {
            // TODO: handle exception
            Log.v("mango", e.getMessage());
        }
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

    private class DownloadFilesTask extends AsyncTask<Void, Integer, String> {
        protected String doInBackground(Void... urls) {
            String rt = "";
            try {

                String url = "https://www.desco.org.bd/ebill/login.php";
                String gmail = "https://www.desco.org.bd/ebill/homepage.php";

                HttpUrlConnectionExample http = new HttpUrlConnectionExample();

                // make sure cookies is turn on
                CookieHandler.setDefault(new CookieManager());

                // 1. Send a "GET" request, so that you can extract the form's
                // data.
                String page = http.GetPageContent(url);
                System.out.println(page);
                String postParams = http.getFormParams(page,
                        "r@gmail.com", "777777");

                // 2. Construct above post's content and then send a POST
                // request
                // for
                // authentication
                http.sendPost(
                        "https://www.desco.org.bd/ebill/authentication.php",
                        postParams);

                // 3. success then go to gmail.
                String result = http.GetPageContent(gmail);
                System.out
                        .println(http
                                .GetPageContent("https://www.desco.org.bd/ebill/homepage.php"));
                System.out.println(result);
                System.out
                        .println(http
                                .GetPageContent("https://www.desco.org.bd/ebill/billinformation.php"));
                rt = http
                        .GetPageContent("https://www.desco.org.bd/ebill/billinformation.php");

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return rt;

        }

        protected void onProgressUpdate(Integer... progress) {

        }

        protected void onPostExecute(String result) {

        }
    }

    public class HttpUrlConnectionExample {
        public List<String> cookies;
        public HttpsURLConnection conn;

        public final String USER_AGENT = "Mozilla/5.0";

        public void sendPost(String url, String postParams) throws Exception {

            URL obj = new URL(url);
            conn = (HttpsURLConnection) obj.openConnection();

            // Acts like a browser
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Host", "https://www.desco.org.bd");
            conn.setRequestProperty("User-Agent", USER_AGENT);
            conn.setRequestProperty("Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            if (this.cookies != null) {
                for (String cookie : this.cookies) {
                    conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                }
            }
            conn.setRequestProperty("Connection", "keep-alive");
            conn.setRequestProperty("Referer",
                    "https://www.desco.org.bd/ebill/login.php");
            conn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length",
                    Integer.toString(postParams.length()));

            conn.setDoOutput(true);
            conn.setDoInput(true);

            // Send post request
            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
            wr.writeBytes(postParams);
            wr.flush();
            wr.close();

            int responseCode = conn.getResponseCode();
            System.out.println("\nSending 'POST' request to URL : " + url);
            System.out.println("Post parameters : " + postParams);
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            // System.out.println(response.toString());

        }

        public String GetPageContent(String url) throws Exception {

            URL obj = new URL(url);
            conn = (HttpsURLConnection) obj.openConnection();

            // default is GET
            conn.setRequestMethod("GET");

            conn.setUseCaches(false);

            // act like a browser
            conn.setRequestProperty("User-Agent", USER_AGENT);
            conn.setRequestProperty("Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            if (cookies != null) {
                for (String cookie : this.cookies) {
                    conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                }
            }
            int responseCode = conn.getResponseCode();
            System.out.println("\nSending 'GET' request to URL : " + url);
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // Get the response cookies
            setCookies(conn.getHeaderFields().get("Set-Cookie"));
            Document tdoc = Jsoup.parse(response.toString());
            Elements ele = tdoc
                    .select("table table tr:nth-child(4) td:nth-child(2) span");
            System.out.println(ele.text());
            return response.toString();

        }

        public String getFormParams(String html, String username,
                String password) throws UnsupportedEncodingException {

            System.out.println("Extracting form's data...");

            Document doc = Jsoup.parse(html);

            // Google form id
            Element loginform = doc.select("form").first();// getElementById("body");
            Elements inputElements = loginform.getElementsByTag("input");
            List<String> paramList = new ArrayList<String>();
            for (Element inputElement : inputElements) {
                String key = inputElement.attr("name");
                String value = inputElement.attr("value");

                if (key.equals("username"))
                    value = "22006500";
                else if (key.equals("login"))
                    value = "Login";
                paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
            }

            // build parameters list
            StringBuilder result = new StringBuilder();
            for (String param : paramList) {
                if (result.length() == 0) {
                    result.append(param);
                } else {
                    result.append("&" + param);
                }
            }
            return result.toString();
        }

        public List<String> getCookies() {
            return cookies;
        }

        public void setCookies(List<String> cookies) {
            this.cookies = cookies;
        }

    }

}

App Manifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.descov2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.descov2.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

在正常的java版本中,代码运行正常,但是当我在android中尝试时,我到了这里

public void sendPost(String url, String postParams) throws Exception {

        URL obj = new URL(url);
        conn = (HttpsURLConnection) obj.openConnection();

        // Acts like a browser
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Host", "https://www.desco.org.bd");
        conn.setRequestProperty("User-Agent", USER_AGENT);
        conn.setRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        if (this.cookies != null) {
            for (String cookie : this.cookies) {
                conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
            }
        }
        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("Referer",
                "https://www.desco.org.bd/ebill/login.php");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length",
                Integer.toString(postParams.length()));

        conn.setDoOutput(true);
        conn.setDoInput(true);

        // Send post request
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.writeBytes(postParams);
        wr.flush();
        wr.close();

        int responseCode = conn.getResponseCode();// this line is throws exception
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + postParams);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        // System.out.println(response.toString());

    }

问题发生在

int responseCode = conn.getResponseCode();

线 在普通的PC上,这工作正常,我得到响应代码为200.但是当在Android上运行时,这给了我400.我不知道这里有什么不对。非常感谢和要求任何帮助。  我发现了一个类似的问题有同样的问题link enter image description here

1 个答案:

答案 0 :(得分:0)

我找到了Here的工作。这与我需要的非常相似