0

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

主类:

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

1 回答 1

0

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

于 2013-02-14T16:59:56.267 回答