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();
}
posted @ 2017-02-13 13:42  SoulCode  阅读(239)  评论(0编辑  收藏  举报