Android网络开发
2010-09-14 23:39:00 来源:WEB开发网// 设置 user agent
HttpProtocolParams.setUserAgent(params, userAgent);
// 创建一个 HttpClient 实例
// 注意 HttpClient httpClient = new HttpClient(); 是Commons HttpClient
// 中的用法,在 Android 1.5 中我们需要使用 Apache 的缺省实现 DefaultHttpClient
HttpClient httpClient = new DefaultHttpClient(params);
// 创建 HttpGet 方法,该方法会自动处理 URL 地址的重定向
HttpGet httpGet = new HttpGet ("http://www.test_test.com/");
HttpResponse response = client.execute(httpGet);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
// 错误处理,例如可以在该请求正常结束前将其中断
httpGet.abort();
}
// 读取更多信息
Header[] headers = response.getHeaders();
HttpEntity entity = response.getEntity();
Header header = response.getFirstHeader("Content-Type");
} catch (Exception ee) {
//
} finally {
// 释放连接
client.getConnectionManager().shutdown();
}
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.params. HttpConnectionParams;
import org.apache.http.client.params. HttpClientParams;
try {
// 创建 HttpParams 以用来设置 HTTP 参数(这一部分不是必需的)
HttpParams params = new BasicHttpParams();
// 设置连接超时和 Socket 超时,以及 Socket 缓存大小
HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
HttpConnectionParams.setSoTimeout(params, 20 * 1000);
更多精彩
赞助商链接