2012-02-23 132 views
1

我需要使用提供會話Cookie的網站驗證用戶名和密碼。Android - 會話Cookie

我正在從窗體上的EditText收集用戶名和密碼,並將它傳遞到驗證會話。

@Override 
    public void onClick(View v) 
    { 
     String Username = username.getText().toString(); 
     String Password = password.getText().toString(); 
     String value = LoginAuthenticate.getSessionCookie(Username, Password); 
     //This is just check what session value is brought back 
     username.setText(value); 

    } 

然後檢查用戶名和密碼是否正確以返回會話cookie。

公共靜態字符串getSessionCookie(用戶名字符串,字符串密碼){

String login_url = "http://www.myexperiment.org/session/create"; 
    URLConnection connection = null; 
    String sessionXML = "<session><username>" + username + 
      "</username><passsword>" + password + 
      "</password></session>"; 
    String cookieValue = null; 
    try 
    { 

     URL url = new URL(login_url); 
     connection = url.openConnection(); 

     connection.setRequestProperty("Content-Type", "application/xml"); 

     connection.setDoOutput(true); 

     OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); 
     out.write(sessionXML); 

     out.close(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     return null; 
    } 
    String headerName = null; 
    for (int i =0; (headerName = connection.getHeaderFieldKey(i)) != null; i++) 
    { 
     if(headerName.equals("Set-Cookie")) 
     { 
      cookieValue = connection.getHeaderField(i); 
     } 
    } 

    //return connection.getHeaderField("Set-Cookie:"); 
    return cookieValue; 
} 

清單文件我有權限設置。

有沒有錯誤,但空是在年底重新調整。我已經檢查了(在調試中)在函數中正確傳遞的用戶名和密碼。

我希望有人能幫到這裏。

謝謝。

回答

1

對於任何有興趣的人;這是工作代碼。它的一個顯示方法的作用。

try 
{ 
    URL url = new URL(login_url); 
    connection = (HttpURLConnection) url.openConnection(); 

    connection.setDoOutput(true); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Content-Type", "application/xml"); 

    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); 
    out.write(sessionXML); 
    out.flush(); 
    out.close(); 

    String headerName = ""; 

    for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) 
    {  
     if(headerName.equals("Set-Cookie")) 
     { 
      cookieValue = connection.getHeaderField(i);   
     } 
    } 
} 
catch (Exception e) 
{ 
    e.printStackTrace(); 
} 
finally 
{ 
    if(connection != null) 
     connection.disconnect(); 
} 
1

檢查此鏈接了:How do I make an http request using cookies on Android?

我認爲這是設置和獲取餅乾,通過頭不循環,就像你(檢查函數的結尾)的首選方式:

import java.util.ArrayList; 
import java.util.List; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.cookie.Cookie; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.protocol.HTTP; 

/** 
* A example that demonstrates how HttpClient APIs can be used to perform 
* form-based logon. 
*/ 
public class ClientFormLogin { 

    public static void main(String[] args) throws Exception { 

     DefaultHttpClient httpclient = new DefaultHttpClient(); 

     HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt"); 

     HttpResponse response = httpclient.execute(httpget); 
     HttpEntity entity = response.getEntity(); 

     System.out.println("Login form get: " + response.getStatusLine()); 
     if (entity != null) { 
      entity.consumeContent(); 
     } 
     System.out.println("Initial set of cookies:"); 
     List<Cookie> cookies = httpclient.getCookieStore().getCookies(); 
     if (cookies.isEmpty()) { 
      System.out.println("None"); 
     } else { 
      for (int i = 0; i < cookies.size(); i++) { 
       System.out.println("- " + cookies.get(i).toString()); 
      } 
     } 

     HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" + 
       "org=self_registered_users&" + 
       "goto=/portal/dt&" + 
       "gotoOnFail=/portal/dt?error=true"); 

     List <NameValuePair> nvps = new ArrayList <NameValuePair>(); 
     nvps.add(new BasicNameValuePair("IDToken1", "username")); 
     nvps.add(new BasicNameValuePair("IDToken2", "password")); 

     httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 

     response = httpclient.execute(httpost); 
     entity = response.getEntity(); 

     System.out.println("Login form get: " + response.getStatusLine()); 
     if (entity != null) { 
      entity.consumeContent(); 
     } 

     System.out.println("Post logon cookies:"); 
     cookies = httpclient.getCookieStore().getCookies(); 
     if (cookies.isEmpty()) { 
      System.out.println("None"); 
     } else { 
      for (int i = 0; i < cookies.size(); i++) { 
       System.out.println("- " + cookies.get(i).toString()); 
      } 
     } 

     // When HttpClient instance is no longer needed, 
     // shut down the connection manager to ensure 
     // immediate deallocation of all system resources 
     httpclient.getConnectionManager().shutdown();   
    } 
} 
+0

讓我試試這種方法,但它並沒有說什麼關於內容類型! – 2012-02-23 16:42:46

+0

我需要以特定格式將用戶名和密碼作爲XML文檔發送。 String sessionXML =「」+ username +「」+ password +「」; – 2012-02-25 23:17:13

+0

我還需要將Content-Type設置爲「application/xml」;我如何使用NameValuePairs來完成這些操作?你能幫我嗎 ? – 2012-02-25 23:18:27