/**
* 通过参数获取网络数据的方法
* @param url
* @param params
* @return
*/
public static String getNetValue(String url,List<NameValuePair> params) {
String webContentString = null;
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
try {
//设置请求参数
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
InputStream responseStream = client.execute(post).getEntity()
.getContent();
// 记得转换成gbk编码
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(responseStream, "gbk"));
webContentString = bufferedReader.readLine();
} catch (Exception e) {
// TODO: handle exception
}
return webContentString;
}验证用户名密码:
// 验证用户名密码是否正确
String result = null;
String urlPath = GlobalConstant.httpUrl+"/rights/user.do?method=login_android";
List<NameValuePair> params_t=new ArrayList<NameValuePair>();
params_t.add(new BasicNameValuePair("username", user.getLogin().trim()));
params_t.add(new BasicNameValuePair("pwd", user.getPassword().trim()));
result = HttpUtil.getNetValue(urlPath,params_t);//post请求
return result;
本文介绍了一种使用Java实现的HTTP请求方法,该方法可通过POST方式发送带有参数的请求来获取网络数据。此外,还提供了一个具体的示例,演示了如何利用此方法进行用户名和密码验证。

5511

被折叠的 条评论
为什么被折叠?



