url是要访问的地址,method是请求方式可以是POST或者GET,params是后面的参数比如?aa=11&bb=22
public String connctionURL_Params(String url,String method,String params){
StringBuffer bufferRes = new StringBuffer();
url = url + params;
try {
URL realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
// 请求方式
conn.setRequestMethod(method);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.connect();
InputStream in = conn.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in,"UTF-8"));
String valueString = null;
while ((valueString=read.readLine())!=null){
bufferRes.append(valueString);
}
read.close();
in.close();
in = null;
if (conn != null) {
// 关闭连接
conn.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
return bufferRes.toString();
}
具体要怎么用啊,能贴出代码示例吗?
广告 您可能关注的内容 |