public class HttpClientUtils {
/**
* 访问网络,将下载的数据存储到文件流中
*
* @param url
* :访问网络的url地址
* @return InputStream
*/
public static InputStream loadFileFromNet(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
return entity.getContent();
// int len = -1;
// byte[] buff = new byte[1024];
// while ((len = bis.read(buff)) != -1) {
// bos.write(buff, 0, len);
// bos.close();
// }
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 实现网络访问文件,将获取到的数据存在字节数组中
*
* @param url
* :访问网络的url地址
* @return byte[]
*/
public static byte[] loadByteFromNet(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
// HttpEntity httpEntity = response.getEntity();
// return EntityUtils.toByteArray(httpEntity);
return EntityUtils.toByteArray(response.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:实现网络访问文件,返回字符串
*
* @param url
* :访问网络的url地址
* @return String
*/
public static String loadTextFromURL(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
// HttpEntity httpEntity = response.getEntity();
// return EntityUtils.toString(httpEntity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:实现网络访问文件,先给服务器通过“GET”方式提交数据,再返回相应的数据
*
* @param url
* :访问网络的url地址
* @param params
* String url:访问url时,需要传递给服务器的参数。
* 第二个参数格式为:username=admin&password=123456
* @return byte[]
*/
public static byte[] doGetSubmit(String url, String params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url + "?" + params);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
/*
* try { URL url2 = new URL(url); HttpURLConnection connect =
* (HttpURLConnection) url2 .openConnection();
* connect.setRequestMethod("POST"); OutputStream os =
* connect.getOutputStream();
* os.write("username=admin&password=123456".getBytes());
* connect.setDoOutput(true);// 上传记得改为true,它默认为false
* ByteArrayOutputStream bos = new ByteArrayOutputStream(); if
* (connect.getResponseCode() == 200) { InputStream is =
* connect.getInputStream(); byte[] buff = new byte[1024]; int len = -1;
* while ((len = is.read(buff)) != -1) { bos.write(buff, 0, len);
* is.close(); } bos.close(); return bos.toByteArray(); } } catch
* (Exception e) { e.printStackTrace(); }
*/
return null;
}
/**
* 作用:实现网络访问文件,先给服务器通过“POST”方式提交数据,再返回相应的数据 使用HttpClient实现
*
* @param url
* :访问网络的url地址
* @param params
* String url:访问url时,需要传递给服务器的参数。 第二个参数为:List<NameValuePair>
* @return byte[]
*/
public static byte[] doPostSubmit(String url, List<NameValuePair> params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost requestPost = new HttpPost(url);
try {
requestPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
HttpResponse httpResponse = httpClient.execute(requestPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
// HttpClient client = new DefaultHttpClient();
// HttpPost post = new HttpPost(url);
// try {
// List<NameValuePair> parameters = new ArrayList<NameValuePair>();
// NameValuePair parametersname = new BasicNameValuePair("username",
// "admin");
// NameValuePair parameterspwd = new BasicNameValuePair("password",
// "123456");
// parameters.add(parametersname);
// parameters.add(parameterspwd);
// HttpEntity entity = new UrlEncodedFormEntity(parameters);
// post.setEntity(entity);
// HttpResponse response = client.execute(post);
// if (response.getStatusLine().getStatusCode() == 200) {
// // InputStream is = response.getEntity().getContent();
// return EntityUtils.toByteArray(response.getEntity());
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// return null;
}
/**
* 作用:实现网络访问文件,先给服务器通过“POST”方式提交数据,再返回相应的数据 使用HttpClient实现
*
* @param url
* :访问网络的url地址
* @param params
* String url:访问url时,需要传递给服务器的参数。 Map<String , Object>
* @return byte[]
*/
public static byte[] doPostSubmit(String url, Map<String, Object> params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost requestPost = new HttpPost(url);
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
try {
if (params != null) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().toString();
BasicNameValuePair nameValuePair = new BasicNameValuePair(
key, value);
parameters.add(nameValuePair);
}
}
requestPost
.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
HttpResponse httpResponse = httpClient.execute(requestPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
// try {
// HttpClient client = new DefaultHttpClient();
// HttpPost post = new HttpPost(url);
// Map<String, Object> params1 = new HashMap<String, Object>();
// String username = "admin";
// String password = "123456";
// params1.put("usernaem", username);
// params1.put("password", password);
//
// post.setEntity((HttpEntity) params1.entrySet());
// HttpResponse response = client.execute(post);
// if (response.getStatusLine().getStatusCode() == 200) {
// // InputStream is = response.getEntity().getContent();
// return EntityUtils.toByteArray(response.getEntity());
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// return null;
}
}
转载于:https://my.oschina.net/u/2541146/blog/625272