Get请求

Get请求时,请求参数直接拼接在url地址后面。

如:String path = "http://169.254.244.136/Web2/servlet/Login?name=" + URLEncoder.encode(name) + "&pass=" pass;
            //URLEncoder.encode(name)对要提交的表单数据进行url编码
 
小例子部分代码:
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 
conn.setRequestMethod("GET");
conn.setConnectTimeout(8000);
conn.setReadTimeout(8000);
if(conn.getResponseCode() == 200){
InputStream is = conn.getInputStream();
String text = Tools.getTextFromStream(is);
 
Message msg = handler.obtainMessage();
msg.obj = text;
handler.sendMessage(msg);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
原文地址:https://www.cnblogs.com/SoulCode/p/6393439.html