2011-10-07 71 views
0

我打算製作一個應用程序,讓用戶無需打開瀏覽器即可訪問互聯網,只需點擊一下鼠標鍵入登錄信息即可獲得權限。我不知道如何讓應用程序打開網頁並在不使用瀏覽器的情況下填充信息。任何人都可以給我一些建議嗎?如何在不打開網頁的情況下讓應用自動登錄?

+0

取決於你想要什麼類型的登錄。誰是您的登錄提供者? –

回答

1

訪問網站的登錄頁面並檢查登錄表單。然後使用DefaultHttpClient發送登錄請求。

這大約是我如何使用它:

public Boolean login() throws Exception { 

    DefaultHttpClient httpClient = new DefaultHttpClient(); 

    // add login data (edit this to fit your website) 
    List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
    nvps.add(new BasicNameValuePair("NAME_OF_USERNAME_FIELD", USERNAME)); 
    nvps.add(new BasicNameValuePair("NAME_OF_PASSWORD_FIELD", PASSWORD)); 

    // create the request 
    HttpPost httpost = new HttpPost(LOGIN_URL); 
    httpost.setEntity(new UrlEncodedFormEntity(nvps, DEFAULT_ENCODING)); 

    // execute the form 
    HttpResponse response = httpClient.execute(httpost); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity() 
      .getContent(), DEFAULT_ENCODING)); 

    // fetch the result of the http request and save it as a string 
    String line; 
    StringBuilder sb = new StringBuilder(); 
    while ((line = reader.readLine()) != null) { 
     sb.append(line).append("\n"); 
    } 
    String input = sb.toString(); 

    // check if the login worked. This depends on the response of your website 
    Pattern pattern = Pattern.compile(LOGIN_WORKED_PATTERN); 
    Matcher m = pattern.matcher(input); 

    if (m.find()) 
     return true; 

    return false; 
} 

調整代碼,以滿足您的需求。

+0

感謝您分享您的代碼 –

+0

但我不知道如何解析功能的HTML代碼 –

+0

它不..它爲什麼呢? – janoliver

相關問題