在这里把三种获取网页内容的信息进行了综合,在前面已经对通过表单提交上传文件进行了处理,现在把这三种方式进行了综合,放到一块,帮助大家进行一个比较,下面为三种方式 的部分代码:
一共三个函数,都可以直接调用,但是在访问网络的时候,记得要加上访问权限
代码
//
直接获取信息
void DirectInfo() throws IOException {
URL url = new URL(SRC);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
InputStreamReader inStreamReader = new InputStreamReader(httpConn
.getInputStream());
BufferedReader bufReader = new BufferedReader(inStreamReader);
String line = "" ;
String Date = " OK " ;
while ((line = bufReader.readLine()) != null ) {
Date += line + " \n " ;
}
edit1.setText(Date);
}
// get方式获取信息
void getInfo() throws IOException {
// 将上面使用的方法直接修改一下即可。
URL url = new URL(SRC + " /default.aspx?NAME= "
+ URLEncoder.encode( " abc " , " utf-8 " ));
HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
InputStreamReader inputReader = new InputStreamReader(httpconn
.getInputStream());
BufferedReader bufReader = new BufferedReader(inputReader);
String line = "" ;
String Date = "" ;
while ((line = bufReader.readLine()) != null ) {
Date += line;
}
edit1.setText(Date);
}
// Post方式获取信息
void postInfo() throws MalformedURLException, IOException {
// Post 方法比Get方法需要设置的参数更多
HttpURLConnection httpconn = (HttpURLConnection) new URL(SRC)
.openConnection();
// post 方式,输入输出需要设置为true
httpconn.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-urlencoded
httpconn.connect(); // 连接
DataOutputStream out = new DataOutputStream(httpconn.getOutputStream()); // 声明数据写入流
String content = " NAME= " + URLEncoder.encode( " fly_binbin " , " gb2312 " );
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader = new BufferedReader( new InputStreamReader(httpconn.getInputStream()));
String line = "" ;
String resultDate = "" ;
while ((line = reader.readLine()) != null )
{
resultDate += line;
}
edit1.setText(resultDate);
}
void DirectInfo() throws IOException {
URL url = new URL(SRC);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
InputStreamReader inStreamReader = new InputStreamReader(httpConn
.getInputStream());
BufferedReader bufReader = new BufferedReader(inStreamReader);
String line = "" ;
String Date = " OK " ;
while ((line = bufReader.readLine()) != null ) {
Date += line + " \n " ;
}
edit1.setText(Date);
}
// get方式获取信息
void getInfo() throws IOException {
// 将上面使用的方法直接修改一下即可。
URL url = new URL(SRC + " /default.aspx?NAME= "
+ URLEncoder.encode( " abc " , " utf-8 " ));
HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
InputStreamReader inputReader = new InputStreamReader(httpconn
.getInputStream());
BufferedReader bufReader = new BufferedReader(inputReader);
String line = "" ;
String Date = "" ;
while ((line = bufReader.readLine()) != null ) {
Date += line;
}
edit1.setText(Date);
}
// Post方式获取信息
void postInfo() throws MalformedURLException, IOException {
// Post 方法比Get方法需要设置的参数更多
HttpURLConnection httpconn = (HttpURLConnection) new URL(SRC)
.openConnection();
// post 方式,输入输出需要设置为true
httpconn.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-urlencoded
httpconn.connect(); // 连接
DataOutputStream out = new DataOutputStream(httpconn.getOutputStream()); // 声明数据写入流
String content = " NAME= " + URLEncoder.encode( " fly_binbin " , " gb2312 " );
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader = new BufferedReader( new InputStreamReader(httpconn.getInputStream()));
String line = "" ;
String resultDate = "" ;
while ((line = reader.readLine()) != null )
{
resultDate += line;
}
edit1.setText(resultDate);
}
网址的话,可以自己做一个测试服务器。我这个测试服务器是我自己写的,进行测试用的,用Asp.net写的,用其它的方法写的结果是一样的。包括使用Web服务结果也是一样的!