您的位置:首页 > 理论基础 > 计算机网络

Android面HTTP协议发送get要求

2015-08-06 16:51 176 查看
/**
* 采纳get办法要求
*
* @param username
* @param password
* @return null表示求得的路径有问题,text返回请求得到的数据
*/
public static String getRequest(String username, String password) {
try {
String path = "http://172.22.64.156:8080/0001AndroidWebService/LoginServlet?username="
+ URLEncoder.encode(username)
+ "&password="
+ URLEncoder.encode(password);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setReadTimeout(500);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code == 200) {
// 请求成功
InputStream is = conn.getInputStream();
String text = StreamUtil.readStream(is);
return text;
} else {
// 请求失败
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: