2014-04-30 12 views
1

我有下面的代碼是使用jsoup來解析html。如何在android中使用jsoup發送POST?

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, 
         "[email protected]", "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; 
     } 

    } 

} 

應用清單

<?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的嘗試我得到這裏

int responseCode = conn.getResponseCode(); 

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()); 

    } 

問題發生

line 在一般的個人電腦上,這工作正常,我得到的響應代碼爲200.但是,當在Android上運行,這給了我400.我不知道這裏有什麼錯。任何幫助非常感謝和要求。 我發現有同樣的問題link enter image description here

+0

我建議你比較Android <->服務器和服務器<->桌面之間交換的標頭。否則,服務器可能不明白Android向他發送的內容。 – Stephan

回答

0

我發現周圍的工作Here類似的問題。這是非常相同的,我需要