使用安全SSL连接保留会话

时间:2013-02-14 16:53:19

标签: java android session ssl https

我连接到授权页面,进入帐户并加载主页面。 如何在不丢失连接的情况下下载帐户中的其他页面? 是不是?

主要课程:

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

1 个答案:

答案 0 :(得分:0)

使用相同的请求对象(HttpPost),它应该使用相同的连接,除非你明确地调用releaseconnection。请参阅http://hc.apache.org/httpcomponents-client-ga/quickstart.html

相关问题