//直接获取信息voidDirectInfo()throwsIOException {
URL url=newURL(SRC);
HttpURLConnection httpConn=(HttpURLConnection) url.openConnection();
InputStreamReader inStreamReader=newInputStreamReader(httpConn
.getInputStream());
BufferedReader bufReader=newBufferedReader(inStreamReader);
String line="";
String Date="OK";while((line=bufReader.readLine())!=null) {
Date+=line+"\n";
}
edit1.setText(Date);
}//get方式获取信息voidgetInfo()throwsIOException {//将上面使用的方法直接修改一下即可。URL url=newURL(SRC+"/default.aspx?NAME="+URLEncoder.encode("abc","utf-8"));
HttpURLConnection httpconn=(HttpURLConnection) url.openConnection();
InputStreamReader inputReader=newInputStreamReader(httpconn
.getInputStream());
BufferedReader bufReader=newBufferedReader(inputReader);
String line="";
String Date="";while((line=bufReader.readLine())!=null) {
Date+=line;
}
edit1.setText(Date);
}//Post方式获取信息voidpostInfo()throwsMalformedURLException, IOException {//Post 方法比Get方法需要设置的参数更多HttpURLConnection httpconn=(HttpURLConnection)newURL(SRC)
.openConnection();//post 方式,输入输出需要设置为truehttpconn.setDoInput(true);
httpconn.setDoOutput(true);
httpconn.setRequestMethod("POST");//设置为Post方式,默认为get方式httpconn.setUseCaches(false);//不使用缓存httpconn.setInstanceFollowRedirects(true);//重定向httpconn.setRequestProperty("Content-type","Application/x-www-form-urlencoded");//设置连接 的Content-type类型为://application/x-www-form-urlencodedhttpconn.connect();//连接DataOutputStream out=newDataOutputStream(httpconn.getOutputStream());//声明数据写入流String content="NAME="+URLEncoder.encode("fly_binbin","gb2312");
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader=newBufferedReader(newInputStreamReader(httpconn.getInputStream()));
String line="";
String resultDate="";while((line=reader.readLine())!=null)
{
resultDate+=line;
}
edit1.setText(resultDate);
}