Android 使用三种方式获取网页(通过Post,Get进行表单的提交)

// 直接获取信息
    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 NAME= "NAME="+URLEncoder.encode("fly_binbin", "UTF-8");

        String PWD="PWD="+URLEncoder.encode("123456","UTF-8");

        String content = NAME+"&"+PWD;

        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);


    }

// post方式第二种

private static boolean sendPostRequest(String path, Map<String, String> params, String encoding) throws Exception{
		// method=save&name=liming&timelength=78
		StringBuilder sb = new StringBuilder("");
		if(params!=null && !params.isEmpty()){
			for(Map.Entry<String, String> entry : params.entrySet()){
				sb.append(entry.getKey()).append('=')
				.append(URLEncoder.encode(entry.getValue(), encoding)).append('&');
			}
			sb.deleteCharAt(sb.length() - 1);
		}
		byte[] data = sb.toString().getBytes();//得到实体数据
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setRequestMethod("POST");
		conn.setConnectTimeout(5*1000);
		conn.setDoOutput(true);//能过post方式提交数据,必须要允许输出
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		conn.setRequestProperty("Content-Length", String.valueOf(data.length));
		OutputStream outStream = conn.getOutputStream();
		outStream.write(data);
		outStream.flush();
		outStream.close();
		if(conn.getResponseCode()==200){
			return true;
		}
		return false;
	}

转载自  http://www.cnblogs.com/fly_binbin/archive/2010/12/19/1910535.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值