2012-11-21 48 views
0

爲了清楚起見,我幾乎沒有HTTP的使用經驗。這個項目對我來說非常有雄心,但我願意爲了能夠完成而學習。我在網上搜索了一些例子,但我似乎找不到一個合適的解決方案。我知道像GET和POST這樣的術語,並理解以編程方式與網站進行交互的基本方式。登錄到HTTPS ASPX的Android應用程序頁面

基本上,我正在使用的公司有一個網站,其中包含可登錄的客戶端數據庫。對於初學者,我只想編寫一個Android應用程序,能夠使用我的用戶名和密碼登錄到主頁面。該網站的登錄URL爲https://「app.companysite.com」/Security/Login.aspx?ReturnUrl=%2fHome%2fDefault.aspx,並具有用於以下目的的證書:「確保證書的身份遠程計算機「。

我在做什麼?最終,我希望能夠打開一個客戶端頁面並編輯他們的數據並重新提交,但一次只能一步。

如果您可以指向某些相關閱讀材料或可幫助我實現目標的源代碼的方向,那將會非常棒。

在此先感謝!

回答

0

我不知道這是否有幫助,但我做我的登錄的方式只是爲了正當理由。所以我做了什麼(因爲我假設驗證是通過MySQL數據庫完成的)是創建一個php文件,它只是驗證登錄用戶名和密碼是否正確,並打印出「正確」或「是」,否則就是「否」 「或」無效「。事情是這樣的:

php 
//Connects to your Database 

     $username = $_POST['username']; 
     $password = $_POST['password']; 

     //make your mysql query if the username and password are in the database 
     //if there is a an approval 
    $approval = 1 
    //otherwise 
    $approval = 0 

    if ($approval > 0) { 
     echo "correct"; 
    } else { 
     echo "invalid"; 
    } 

    ?> 

現在在Android中,你能做出這樣的請求來調用這個網站,並返回輸出類似如下:

HttpParams httpParameters = new BasicHttpParams(); 
//make a timeout for the connections in milliseconds so 4000 = 4 seconds     httpParameters.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 
int timeoutConnection = 4000; 
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
int timeoutSocket = 4000; 
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

HttpClient httpclient = new DefaultHttpClient(httpParameters); 
HttpPost httppost = new HttpPost("your website URL"); 

// Add your data 
String username = "your username"; 
String password = "your password"; 

List<NameValuePair> nameValuePairs; 
nameValuePairs = new ArrayList<NameValuePair>(2); 
nameValuePairs.add(new BasicNameValuePair("username", username)); 
nameValuePairs.add(new BasicNameValuePair("password", password)); 

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
// Execute HTTP Post Request 
HttpResponse response = httpclient.execute(httppost); 

BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

String lines = ""; 
String data = null; 
ArrayList<String> al = new ArrayList<String>(); 

while((lines = in.readLine()) != null){ 
    data = lines.toString(); 
    al.add(data); 
} 
in.close(); 

//To get the response 
if(al.get(0).equals("correct")){ 
     //Your login was successful 
} 
else { 
    //Your login was unsuccessful 
} 

我希望這有助於你一點,並指出您在正確的方向。