网络编程一:网络请求

我常用的网络请求:
1、使用HttpURLConnection获取指定地址的网页信息
2、使用HttpURLConnection带参数对指定地址进行网络请求

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {

    //从网络获取数据
    public static String getRequest(String url){
        String content = "";
        HttpURLConnection connection = null;
        try{
            URL u = new URL(url);
            connection = (HttpURLConnection)u.openConnection();
            connection.setRequestMethod("GET");
            int code = connection.getResponseCode();
            if(code == 200){
                InputStream in = connection.getInputStream();
                InputStreamReader is = new InputStreamReader(in,"utf-8");
                BufferedReader reader = new BufferedReader(is);
                String line = null;
                while((line = reader.readLine()) != null){
                    content += line;
                }
                reader.close();
                is.close();
                in.close();
            }
        }catch(IOException e){
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(connection != null){
                connection.disconnect();
            }
        }
        return content;
    }

    //带数据请求网络
    public static String postRequest(String url_str,String date_str){
        URL url = null;
        String content = "";
        try {
            url = new URL(url_str);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
            httpURLConnection.setReadTimeout(2000);//读取超时 单位毫秒
            // 设置通用的请求属性
            httpURLConnection.setRequestProperty("accept", "*/*");  
            httpURLConnection.setRequestProperty("connection", "Keep-Alive");  
            httpURLConnection.setRequestProperty("user-agent",  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            OutputStream outStream = httpURLConnection.getOutputStream();
            // 发送请求参数
            outStream.write(date_str.getBytes());
            // flush输出流的缓冲
            outStream.flush();

            //开始获取数据
            InputStream in = httpURLConnection.getInputStream();
            InputStreamReader is = new InputStreamReader(in,"utf-8");
            BufferedReader reader = new BufferedReader(is);
            String line = null;
            while((line = reader.readLine()) != null){
                content += line;
            }
            reader.close();
            is.close();
            in.close();

            return content;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main (String[] args)
    {
        //从网络获取数据
        String contentget = getRequest("https://www.baidu.com");
        System.out.println(contentget);

        //带数据请求网络
        String urls = "https://api.mch.weixin.qq.com/pay/micropay";
        String dates = "<xml><auth_code>120269300684844649</auth_code></xml>";
        String contentpost =postRequest(urls,dates);
        System.out.println(contentpost);

    }
}

基本步骤:
1、实例化URL,并且传入要访问的地址
2、用openConnection()打开连接
3、用setRequestMethod设置提交请求的模式(GET/POST)
4、最好设置一下setRequestProperty,部分服务器要求请求的时候设置这个
5、用getInputStream()来获取请求后返回的数据
6、用InputStream、InputStreamReader、BufferedReader承接并输出获取的返回数据
7、while((line = reader.readLine()) != null)用这个while循环输出获得的数据并且赋值到一个result(String)里面储存(line为String)
8、关闭输入、输出流、网络连接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值