2013-02-14 63 views
0

我在那裏連接到授權頁面,進入主菜單並加載主頁面。 如何在不失去連接的情況下下載帳戶中的其他頁面? 是不是?通過安全的SSL連接保留會話

主要類:

HttpParams httpParameters = new BasicHttpParams(); 
int timeoutConnection = 10000; 
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
int timeoutSocket = 10000; 
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

HttpClient client = new MyHttpClient(httpParameters, getApplicationContext()); 
HttpPost request = new HttpPost("https://some.com/Login.aspx"); 

BufferedReader in = null; 
try { 

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("uname", "name")); 
    nameValuePairs.add(new BasicNameValuePair("upass", "pass")); 
    request.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    HttpResponse response = client.execute(request); 
    in = new BufferedReader(new InputStreamReader(response.getEntity() .getContent())); 

    StringBuffer sb = new StringBuffer(""); 
    String line = ""; 
    String NL = System.getProperty("line.separator"); 
    while ((line = in.readLine()) != null) { 
     sb.append(line + NL); 
    } 
    in.close(); 
    String page = sb.toString(); 

    textview.setText(page); 

} catch (ClientProtocolException e) {...} 

和類MyHttpClient:

public class MyHttpClient extends DefaultHttpClient 
{  
    final Context context; 
    public MyHttpClient(HttpParams hparms, Context context) 
    { 
    super(hparms); 
    this.context = context;  
    } 

    @Override 
    protected ClientConnectionManager createClientConnectionManager() { 
    SchemeRegistry registry = new SchemeRegistry(); 
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
    registry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); 
    return new SingleClientConnManager(getParams(), registry); 
    } 
} 

回答

0

使用相同的請求對象(HttpPost),應該使用相同的連接,除非你明確地調用它的releaseconnection。參考http://hc.apache.org/httpcomponents-client-ga/quickstart.html

+0

我做到了! 在登錄我使用HttpPost,並認爲它neet用於去其他頁面,但正如我看到我與'魔術插件'HttpFox第二頁與HttpGet和configureg頭申請打開Firefox。 和Rohit謝謝 – Ivvan 2013-02-14 21:47:39