目的是为了不依赖第三方的jar包进行网络请求(如:commons-httpclient.jar)
1. 建立一个连接配置类
2. 封装请求类
3. 调用
GET请求
POST请求
1. 建立一个连接配置类
- class UserAgentConfig {
- public String host;
- public String scheme = "http";
- public int port = 80;
- public int timeoutConnection = 3000;
- public int timeoutSocket = 20000;
- public String username = "";
- public String password = "";
- }
2. 封装请求类
- public class HttpRequest {
- /**
- * get "Stream" as response
- *
- * @param url request URL
- * @param config connection config, can be null
- * @param params parameter for post method, can be null
- * @return response in stream
- */
- public static InputStream getStream(String url, UserAgentConfig config,
- List<BasicNameValuePair> params) {
- HttpEntity entity = httprequest(url, config, params);
- InputStream ret = null;
- if (entity != null) {
- try {
- byte[] b = EntityUtils.toByteArray(entity);
- ret = new ByteArrayInputStream(b);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- release(entity);
- }
- }
- return ret;
- }
- /**
- * get "String" as response
- *
- * @param url request URL
- * @param config connection config, can be null
- * @param params parameter for post method, can be null
- * @return response in string
- */
- public static String getString(String url, UserAgentConfig config,
- List<BasicNameValuePair> params) {
- HttpEntity entity = httprequest(url, config, params);
- String ret = null;
- if (entity != null) {
- try {
- ret = EntityUtils.toString(entity);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- release(entity);
- }
- }
- return ret;
- }
- private static void release(HttpEntity entity) {
- try {
- entity.consumeContent();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * get "HttpEntity" as response
- *
- * @param url request URL
- * @param config connection config, can be null
- * @param params parameter for post method, can be null
- * @return
- */
- private static HttpEntity httprequest(String url, UserAgentConfig config,
- List<BasicNameValuePair> params) {
- DefaultHttpClient client = null;
- HttpEntity entity = null;
- try {
- BasicHttpParams httpParameters = new BasicHttpParams();
- if (config != null) {
- // set connection timeout
- HttpConnectionParams.setConnectionTimeout(httpParameters,
- config.timeoutConnection);
- HttpConnectionParams.setSoTimeout(httpParameters,
- config.timeoutSocket);
- }
- client = new DefaultHttpClient(httpParameters);
- DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(
- 3, true);
- client.setHttpRequestRetryHandler(retryHandler);
- // set username & password if available
- if (config != null
- && !("".equals(config.username) && ""
- .equals(config.password))) {
- AuthScope as = new AuthScope(config.host, config.port);
- UsernamePasswordCredentials upc = new UsernamePasswordCredentials(
- config.username, config.password);
- client.getCredentialsProvider().setCredentials(as, upc);
- }
- // check get or post method by params
- HttpRequestBase method = null;
- if (params == null) {
- method = new HttpGet(url);
- } else {
- method = new HttpPost(url);
- ((HttpPost) method).setEntity(new UrlEncodedFormEntity(params,
- "utf-8"));
- }
- method.setHeader("Content-Type", "application/xml");
- // get response
- HttpResponse response = null;
- if (config == null) {
- // only url is available
- response = client.execute(method);
- } else {
- BasicHttpContext localContext = new BasicHttpContext();
- BasicScheme basicAuth = new BasicScheme();
- localContext.setAttribute("preemptive-auth", basicAuth);
- HttpHost targetHost = new HttpHost(config.host, config.port,
- config.scheme);
- response = client.execute(targetHost, method, localContext);
- }
- entity = response.getEntity();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return entity;
- }
- }
3. 调用
GET请求
- String url = "...";
- test(url, null, null);
- String url = "...";
- UserAgentConfig config = new UserAgentConfig();
- config.host = "...";
- test(url, config, null);
POST请求
- String url = "...";
- UserAgentConfig config = new UserAgentConfig();
- config.host = "...";
- config.username = "...";
- config.password = "...";
- ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
- params.add(new BasicNameValuePair("...", "..."));
- ...
- test(url, config, params);