/**
* 用 HttpClient 的 Get 请求访问服务器
*
* @param url_path
* @param userName
* @param userPass
* @return
*/
private String HttpClientGetMeth(String url_path, String userName,
String userPass) {
String result = "";
try {
url_path = url_path + "?username="
+ URLEncoder.encode(userName, "utf-8") + "&userpass="
+ URLEncoder.encode(userPass, "utf-8");
HttpGet get = new HttpGet(url_path);
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
HttpConnectionParams.setSoTimeout(params, 5 * 1000);
HttpClient httpClient = new DefaultHttpClient(params);
HttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity resEntity = response.getEntity();
result = EntityUtils.toString(resEntity, "utf-8");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 用 HttpClient 的 Post 请求访问服务器
*
* @param url_path
* @param userName
* @param userPass
* @return
*/
private String HttpClientPostMeth(String url_path, String userName,
String userPass) {
String result = "";
HttpPost post = new HttpPost(url_path);
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
HttpConnectionParams.setSoTimeout(params, 5 * 1000);
List<NameValuePair> pair = new ArrayList<NameValuePair>();
pair.add(new BasicNameValuePair("username", userName));
pair.add(new BasicNameValuePair("userpass", userPass));
try {
HttpEntity entity = new UrlEncodedFormEntity(pair, "utf-8");
post.setEntity(entity);
HttpClient httpClient = new DefaultHttpClient(params);
HttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity resEntity = response.getEntity();
result = EntityUtils.toString(resEntity, "utf-8");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 用 HttpURLConnection 的 Post 请求访问服务器
*
* @param url_path
* @param userName
* @param userPass
* @return
*/
private String URLConnectionPosttMeth(String url_path, String userName,
String userPass) {
String result = "";
// ?username=admin&userpass=123456
try {
URL url = new URL(url_path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setReadTimeout(5 * 1000);
conn.setRequestMethod("POST");// 设置请求方式为 Post 方式
conn.setDoInput(true);// 设置是否可以读取
conn.setDoOutput(true);// 设置是否可以写入
DataOutputStream dos = new DataOutputStream(
conn.getOutputStream());
// 把中文进行utf-8编码,服务器通过request.setCharacterEncoding("utf-8");解码
String params = "username="
+ URLEncoder.encode(userName, "utf-8") + "&userpass="
+ URLEncoder.encode(userPass, "utf-8");
dos.write(params.getBytes());
dos.flush();
dos.close();
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();// 获得输入流对象读取服务器响应结果
// 服务器需要通过response.setCharacterEncoding("utf-8");//设置服务器响应编码为中文编码,为了解决android端接收的数据能不乱码
// 因为有中文乱码,需要转码,通过把字节流转换为缓存字符流,同时设置编码,实现转码
InputStreamReader reader = new InputStreamReader(is,
"utf-8");
char[] buf = new char[1024];
reader.read(buf);
// Log.i("Bright", buf.length + "------post------");
result = new String(buf, 0, buf.length);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 用 HttpURLConnection 的 Get 请求访问服务器
*
* @param url_path
* @param userName
* @param userPass
* @return
*/
private String URLConnectionGetMeth(String url_path, String userName,
String userPass) {
String result = "";
// ?username=admin&userpass=123456
try {
// 把中文进行utf-8编码,服务器通过request.setCharacterEncoding("utf-8");解码
url_path = url_path + "?username="
+ URLEncoder.encode(userName, "utf-8") + "&userpass="
+ URLEncoder.encode(userPass, "utf-8");
URL url = new URL(url_path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setReadTimeout(5 * 1000);
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
// 服务器需要通过response.setCharacterEncoding("utf-8");//设置服务器编码为中文编码,为了解决android端接收的数据能不乱码
// 因为有中文乱码,需要转码,通过把字节流转换为缓存字符流,同时设置编码,实现转码
InputStreamReader reader = new InputStreamReader(is,
"utf-8");
char[] buf = new char[1024];
reader.read(buf);
// Log.i("Bright", buf.length + "------get------");
result = new String(buf, 0, buf.length);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result.trim();
}